使用 G++编译多个.cpp和.h文件
我刚刚继承了一些 C++ 代码,这些代码用一个包含 main 函数和一堆其他函数的 cpp 文件编写得很糟糕。还有包含类及其函数定义的 .h
文件。
到目前为止,该程序是使用命令 g++ main.cpp
编译的。现在我已将类分离为 .h
和 .cpp
文件,我是否需要使用 makefile,或者仍然可以使用 g++ main.cpp< /代码> 命令?
I've just inherited some C++ code that was written poorly with one cpp file which contained the main and a bunch of other functions. There are also .h
files that contain classes and their function definitions.
Until now the program was compiled using the command g++ main.cpp
. Now that I've separated the classes to .h
and .cpp
files do I need to use a makefile or can I still use the g++ main.cpp
command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
列出 main.cpp 之后的所有其他 cpp 文件。
即
等等。
或者您可以单独编译它们。然后将所有生成的“.o”文件链接在一起。
list all the other cpp files after main.cpp.
ie
and so on.
Or you can compile them all individually. You then link all the resulting ".o" files together.
要单独编译而不链接,您需要添加
-c
选项:To compile separately without linking you need to add
-c
option:如果要将多个文件放入 Makefile 中,那么一次编译多个文件是一个糟糕的选择。
通常在 Makefile 中(对于 GNU/Make),它应该这样写就足够了:
这样
make
将仅正确地重新编译需要重新编译的内容。人们还可以添加一些调整来生成头文件依赖项 - 这样 make 也可以正确重建由于头文件更改而需要重建的内容。Compiling several files at once is a poor choice if you are going to put that into the Makefile.
Normally in a Makefile (for GNU/Make), it should suffice to write that:
That way
make
would 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.我知道这个问题几年前就被问过,但仍然想分享我通常如何编译多个 C++ 文件。
g++ -c *.cpp -o myprogram
。"myprogram"
./myprogram
就是这样!
我使用 * 的原因是,如果您有 30 个 cpp 文件,您会输入所有这些文件吗?或者只使用 * 符号并节省时间:)
ps 仅当您不关心 makefile 时才使用此方法。
I know this question has been asked years ago but still wanted to share how I usually compile multiple c++ files.
g++ -c *.cpp -o myprogram
."myprogram"
./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 :)
p.s Use this method only if you don't care about makefile.
.h
文件与编译无关...您只关心 cpp 文件...所以输入g++ filename1.cpp filename2.cpp main.cpp -o myprogram
意味着您正在编译每个 cpp 文件,然后将它们链接到
myprgram
中。然后运行你的程序
./myprogram
.h
files will nothing to do with compiling ... you only care about cpp files... so typeg++ filename1.cpp filename2.cpp main.cpp -o myprogram
means you are compiling each cpp files and then linked them together into
myprgram
.then run your program
./myprogram
如果需要,您仍然可以直接使用 g++:
其中 f1.cpp 和 f2.cpp 是包含函数的文件。有关如何使用 make 进行构建的详细信息,请参阅优秀的 GNU make 文档< /a>.
You can still use g++ directly if you want:
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.
正如 rebenvp 所说,我使用:
然后对输出执行此操作:
但更好的解决方案是使用
make
文件。请阅读此处了解有关make
文件的更多信息。另请确保您已在
.cpp
文件中添加了所需的.h
文件。As rebenvp said I used:
And then do this for output:
But a better solution is to use
make
file. Read here to know more aboutmake
files.Also make sure that you have added the required
.h
files in the.cpp
files.您可以使用多个 g++ 命令然后进行链接,但最简单的方法是使用传统的 Makefile 或其他构建系统:例如 Scons(通常比 Makefile 更容易设置)。
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).
如果您想在 cpp 文件中使用
#include
,您可以使用:If you want to use
#include <myheader.hpp>
inside your cpp files you can use:您可以使用单个命令来完成此操作,假设所有需要的
.cpp
和.h
文件都位于同一文件夹中。它将同时编译和执行。
You can do that using a single command assuming all the needed
.cpp
and.h
files are in the same folder.It will compile and execute at the same time.
我曾经使用自定义 Makefile 来编译当前目录中的所有文件,但每次我都必须将其复制到我需要的每个目录中。
所以我创建了自己的工具 - 通用编译器 这使得这个过程变得更加容易编译很多文件时更容易。
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.
So I created my own tool - Universal Compiler which made the process much easier when compile many files.
在命令行中使用编译器时,应注意以下事项:
您不需要编译头文件,因为头文件在使用 include 指令的脚本中被替换。
您将需要编译并链接实现和脚本文件。
例如,让cow.h为头文件,cow.cpp为实现文件,cow.cc(c++文件可以具有扩展名.cpp,.cc,.cxx,.C,.CPP,.cp)为脚本文件。
由于 c++ 文件的 gcc 编译器表示法是 g++,因此我们可以使用选项“-g”和“-Wall”编译和链接文件,
用于调试信息并获取错误警告。这里cow.out是我们可以执行来运行程序的可执行二进制文件的名称。为可执行文件命名总是好的,否则系统会自动给出名称,这有时可能会令人困惑。
您也可以使用 makefile 来完成相同的操作,makefile 会自动检测、编译和链接指定的文件。
有很多使用命令行编译的资源
在此处输入链接说明
when using compiler in the command line, you should take of the following:
you need not compile a header file, since header file gets substituted in the script where include directive is used.
you will require to compile and link the implementation and the script file.
for example let cow.h be header file and cow.cpp be implementation file and cow.cc(c++ files can have extension .cpp, .cc, .cxx, .C, .CPP, .cp) be script file.
Since gcc compiler notation for c++ file is g++, we can compile and link the files using
options '-g' and '-Wall' are for debugging info and getting warning for errors. Here cow.out is the name of the executable binary file that we can execute to run the program. it is always good to name your executable file otherwise name will be automatically given which might be confusing at times.
you can also do the same by using makefiles, makefiles will detect, compile and link automatically the specified files.
There are great resources for compilation using command line
enter link description here
〜/ In_ProjectDirectory $ g ++ coordin_main.cpp coordin_func.cpp coordin.h〜
/ In_ProjectDirectory $ ./a.out
...工作!
将 Linux Mint 与 Geany IDE 结合使用
当我将每个文件保存到同一目录时,有一个文件未正确保存在该目录中; coordin.h 文件。因此,重新检查后,它被保存为 coordin.h,并且没有错误地保存为 ->坐标.h.gch。小东西。
嗯!!
~/In_ProjectDirectory $ g++ coordin_main.cpp coordin_func.cpp coordin.h
~/In_ProjectDirectory $ ./a.out
... Worked!!
Using Linux Mint with Geany IDE
When I saved each file to the same directory, one file was not saved correctly within the directory; the coordin.h file. So, rechecked and it was saved there as coordin.h, and not incorrectly as -> coordin.h.gch. The little stuff.
Arg!!