C++ 使用 G++ 编译多个 .cpp 和 .h 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3202136/
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-28 12:18:32  来源:igfitidea点击:

Using G++ to compile multiple .cpp and .h files

c++compilationheadermakefile

提问by Meir

I've just inherited some C++ code which was written poorly with one cpp file which contained the main and a bunch of other functions. There are also .hfiles which contain classes and their function definitions.

我刚刚继承了一些 C++ 代码,这些代码用一个包含主函数和一堆其他函数的 cpp 文件编写得很差。还有一些.h文件包含类及其函数定义。

Until now the program was compiled using the command g++ main.cpp. Now that I've seperated the classes to .hand .cppfiles do I need to use a makefile or can I still use the g++ main.cppcommand?

到目前为止,程序是使用命令编译的g++ main.cpp。现在我已经将类.h.cpp文件分开了,我是否需要使用 makefile 或者我仍然可以使用g++ main.cpp命令吗?

回答by Goz

list all the other cpp files after main.cpp.

列出 main.cpp 之后的所有其他 cpp 文件。

ie

IE

g++ main.cpp other.cpp etc.cpp

and so on.

等等。

Or you can compile them all individually. You then link all the resulting ".o" files together.

或者您可以单独编译它们。然后将所有生成的“.o”文件链接在一起。

回答by FredAKA

To compile separately without linking you need to add -coption:

要单独编译而不链接,您需要添加-c选项:

g++ -c myclass.cpp
g++ -c main.cpp
g++ myclass.o main.o
./a.out

回答by Dummy00001

Now that I've seperated the classes to .h and .cpp files do I need to use a makefile or can I still use the "g++ main.cpp" command?

现在我已经将类分离为 .h 和 .cpp 文件,我是否需要使用 makefile 或者我仍然可以使用“g++ main.cpp”命令吗?

Compiling several files at once is a poor choice if you are going to put that into the Makefile.

如果您要将其放入 Makefile,一次编译多个文件是一个糟糕的选择。

Normally in a Makefile (for GNU/Make) it should suffice to write that:

通常在 Makefile 中(对于GNU/Make),这样写就足够了:

# "all" is name of the default target, running "make" without params would use it
all: executable1

# for C++, replace CC (c compiler) with CXX (c++ compiler) which is used as default linker
CC=$(CXX)

# tell which files should be used, .cpp -> .o make would do automatically
executable1: file1.o file2.o

That way makewould be properly recompiling only what needs to be recompiled. One can also add few tweaks to generate the header file dependencies - so that make would also properly rebuild what's need to be rebuilt due to the header file changes.

这样make就可以正确地重新编译需要重新编译的内容。还可以添加一些调整来生成头文件依赖项 - 这样 make 也可以正确重建由于头文件更改而需要重建的内容。

回答by Dummy00001

You can still use g++ directly if you want:

如果需要,您仍然可以直接使用 g++:

g++ f1.cpp f2.cpp main.cpp

where f1.cpp and f2.cpp are the files with the functions in them. For details of how to use make to do the build, see the excellent GNU make documentation.

其中 f1.cpp 和 f2.cpp 是其中包含函数的文件。有关如何使用 make 进行构建的详细信息,请参阅优秀的GNU make 文档

回答by Matthew

.hfiles will nothing to do with compiling ... you only care about cpp files... so type g++ filename1.cpp filename2.cpp main.cpp -o myprogram

.h文件与编译无关......你只关心cpp文件......所以输入 g++ filename1.cpp filename2.cpp main.cpp -o myprogram

means you are compiling each cpp files and then linked them together into myprgram.

意味着您正在编译每个 cpp 文件,然后将它们链接到myprgram.

then run your program ./myprogram

然后运行你的程序 ./myprogram

回答by zobair

I know this question has been asked years ago but still wanted to share how I usually compile multiple c++ files.

我知道这个问题多年前就有人问过,但仍然想分享我通常如何编译多个 C++ 文件。

  1. Let's say you have 5 cpp files, all you have to do is use the * instead of typing each cpp files name E.g g++ -c *.cpp -o myprogram.
  2. This will generate "myprogram"
  3. run the program ./myprogram
  1. 假设您有 5 个 cpp 文件,您所要做的就是使用 * 而不是键入每个 cpp 文件的名称,例如 g++ -c *.cpp -o myprogram.
  2. 这将产生 "myprogram"
  3. 运行程序 ./myprogram

that's all!!

就这样!!

The reason I'm using * is that what if you have 30 cpp files would you type all of them? or just use the * sign and save time :)

我使用 * 的原因是,如果您有 30 个 cpp 文件,您会键入所有文件吗?或者只是使用 * 符号并节省时间:)

p.s Use this method only if you don't care about makefile.

ps 仅当您不关心 makefile 时才使用此方法。

回答by Pritam Banerjee

As rebenvpsaid I used:

正如rebenvp所说,我使用了:

g++ *.cpp -o output

And then do this for output:

然后对输出执行此操作:

./output

But a better solution is to use makefile. Read hereto know more about makefiles.

但更好的解决方案是使用make文件。阅读此处以了解有关make文件的更多信息。

Also make sure that you have added the required .hfiles in the .cppfiles.

还要确保您已在.h文件中添加了所需的.cpp文件。

回答by gauteh

You can use several g++ commands and then link, but the easiest is to use a traditional Makefile or some other build system: like Scons (which are often easier to set up than Makefiles).

您可以使用多个 g++ 命令然后链接,但最简单的方法是使用传统的 Makefile 或其他一些构建系统:如 Scons(通常比 Makefile 更容易设置)。

回答by Matias Haeussler

If you want to use #include <myheader.hpp>inside your cpp files you can use:

如果要#include <myheader.hpp>在 cpp 文件中使用,可以使用:

g++ *.cpp -I. -o out

回答by Shubham Chaudhary

I used to use a custom Makefile that compiled all the files in current directory, but I had to copy it in every directory I needed it, everytime.

我曾经使用一个自定义 Makefile 来编译当前目录中的所有文件,但是每次我都必须将它复制到我需要的每个目录中。

So I created my own tool - Universal Compilerwhich made the process much easier when compile many files.

所以我创建了我自己的工具 -通用编译器,它使编译许多文件的过程变得更加容易。