Linux 你如何在 Geany 中编译/构建/执行 C++ 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4475362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:00:26  来源:igfitidea点击:

How do you compile/build/execute a C++ project in Geany?

c++linuxgeany

提问by returneax

I really didn't think it would be this difficult. Geany clearly has the ability to create projects, add files to the projects, compile the individual files, but then even after googling it I could not find a clear description of how to build and execute the project... It's pretty annoying because I really like the simplicity of Geany and its clean, uncluttered workspace, but this could be a deal breaker.

我真的没想到会这么难。Geany 显然具有创建项目、向项目添加文件、编译单个文件的能力,但是即使在谷歌搜索之后我也找不到关于如何构建和执行项目的清晰描述......这很烦人,因为我真的就像 Geany 的简单性及其干净整洁的工作区一样,但这可能会破坏交易。

采纳答案by weberc2

Geany doesn't compile projects. You can use a makefile to serve the same purpose; however, you have to make it manually or use an external command that can figure out dependencies. Geany's "make" command will use the make file called "makefile" by default, so you can simply give your make file that name and all should be well.

Geany 不编译项目。您可以使用 makefile 来达到同样的目的;但是,您必须手动制作或使用可以确定依赖关系的外部命令。Geany 的“make”命令将默认使用名为“makefile”的 make 文件,因此您可以简单地为您的 make 文件命名,一切都应该很好。

all: hello

hello: main.o factorial.o hello.o
    g++ main.o factorial.o hello.o -o hello

main.o: main.cpp
    g++ -c main.cpp

factorial.o: factorial.cpp
    g++ -c factorial.cpp

hello.o: hello.cpp
    g++ -c hello.cpp

clean:
    rm -rf *o hello

Example taken from here. You can find more detailed information on that page as well.

示例取自此处。您也可以在该页面上找到更多详细信息。

回答by moinudin

According to this, hit F8 to compile and F5 to run the project. You first have to setup the compiler though, as mentioned in the article.

根据,按F8来编译和F5运行项目。不过,您首先必须设置编译器,如文章中所述。

回答by kriss

To build project just open a file of the project, then choose Make in the Build menu (shift+F9).

要构建项目,只需打开项目文件,然后在 Build 菜单中选择 Make (shift+F9)。

For executing menu Build and Execute (F5).

用于执行菜单 Build and Execute (F5)。

If the project does not compile using make (as it usually does on Linux), you will also have to edit properties of the project in the menu Project entry Properties.

如果项目没有使用 make 编译(就像在 Linux 上通常那样),您还必须在菜单项目条目属性中编辑项目的属性。

If you want details you could also read the manual, it could seems dumb compared to googling, but it looks quite clear to me... Just hit F1 key.

如果您想要详细信息,您也可以阅读手册,与谷歌搜索相比,它可能看起来很愚蠢,但对我来说看起来很清楚......只需按 F1 键。

回答by joe

Assuming you set up your paths(right-click my computer > properties > advanced system settings > environment variables, just google search what to do next) and Mingw correctly, click on "set build menu commands" and enter the following. including the "".

假设您正确设置了路径(右键单击my computer > properties > advanced system settings > environment variables,只需谷歌搜索下一步做什么)和 Mingw,单击“设置构建菜单命令”并输入以下内容。包括 ””。

compile = g++ -O0 -g3 -Wall -c -o"%e.o" "%f" 

Build = g++ -o"%e" ./%e.o

Execute = "./%e"

this is what worked for me, if you get an error when trying to build (after you compiled) that says something about some permissions issue, that is b/c of windows UAC is blocking Geany from building. You just need to run geany as admin to resolve this.

这对我有用,如果您在尝试构建(编译后)时遇到错误,说明某些权限问题,那就是 Windows UAC 的 b/c 阻止了 Geany 构建。您只需要以管理员身份运行 geany 即可解决此问题。

回答by None

Geany builds projects using external commands. This is flexible and allows the IDE to be language-agnostic, thus being able to build large and heterogeneous projects.

Geany 使用外部命令构建项目。这是灵活的,并允许 IDE 与语言无关,从而能够构建大型异构项目。

Regarding C++, it's very simple to create a basic Makefile (much simpler than the example above). Supose your project builds a program called "my_program", consists of the files my_program.cpp and bar.cpp, and links with the foo library. All you need is this:

关于 C++,创建一个基本的 Makefile 非常简单(比上面的例子简单得多)。假设您的项目构建了一个名为“my_program”的程序,由文件 my_program.cpp 和 bar.cpp 组成,并与 foo 库链接。你只需要这个:

LDLIBS += -lfoo

my_program: my_program.cpp bar.cpp

LDLIBS += -lfoo

my_program: my_program.cpp bar.cpp

Save this with the name "Makefile" in the same directory of the sources.Now you have to create the Geany project proper, indicating that the base directory is where the code (and Makefile) are stored.

将其以名称“Makefile”保存在源代码的同一目录中。现在您必须正确创建 Geany 项目,表明基本目录是存储代码(和 Makefile)的地方。

That's it! you can now compile you program with a keypress (shift+F9). For also running it with a key just enter your program name (my_program in the example) in the Geany's project properties.

就是这样!您现在可以使用按键 (shift+F9) 编译您的程序。也可以使用密钥运行它,只需在 Geany 的项目属性中输入您的程序名称(在示例中为 my_program)。

Note that it's important that one of your source files has the same name as the target binary, otherwise you cannot user Make's implicit rules, wich complicates the Makefile a bit.

请注意,您的源文件之一与目标二进制文件具有相同的名称很重要,否则您无法使用 Make 的隐式规则,这会使 Makefile 有点复杂。

回答by Damion Lunin

Compiling a multi-file C++ project using Geany's F-keys requires that you first setup a Makefile and the corresponding settings in Geany (as described in previous answers); after such setup is completed the F-keys in Geany's Builddrop-down menu become useful for that particular multi-file project.

使用 Geany 的 F 键编译多文件 C++ 项目需要您首先在 Geany 中设置一个 Makefile 和相应的设置(如之前的答案所述);完成此类设置后,Geany 的Build下拉菜单中的 F 键对于该特定的多文件项目变得有用。

If however you just want to quickly compile a multi-file C++ project without having to setup a Makefile as well as Geany's settings, use the terminal at the bottom of Geany to type the command-line instruction for compiling a multi-file project:

但是,如果您只想快速编译多文件 C++ 项目,而无需设置 Makefile 以及 Geany 的设置,请使用 Geany 底部的终端键入用于编译多文件项目的命令行指令:

uberstudent@uberstudent:~$ g++ my_source1.cpp my_source2.cpp -o my_executable

You can then execute your executable with:

然后,您可以使用以下命令执行您的可执行文件:

uberstudent@uberstudent:~$ ./my_executable

(Note that the above applies to Geany on Linux; I have not tested the above commands on other operating systems.)

(请注意,以上适用于 Linux 上的 Geany;我没有在其他操作系统上测试过上述命令。)