使用g ++编译多个.cpp和.h文件。我做对了吗? [英] Compiling multiple .cpp and .h files using g++. Am I doing it right?
问题描述
可以说我有这些文件:
main.cpp
, ClassA.cpp
, ClassA.h
, ClassB.cpp
, ClassB.h
p>
- 主要包含
#includeClassA.h
和#includeClassB.h
和每个.cpp
文件包含它们各自的.h
文件。这是正确的?
现在我正在使用 g ++ * .cpp
编译我得到一个可执行文件 a.exe
(Windows)
- 我的问题是这是正确的方式?并且可以说,如果我只对一个文件进行更改(cpp或h),此命令是否也会重新编译未更改的文件(因为除了a.exe之外,文件夹中没有新文件)?请解释。另外我该如何防止这种情况?
PS我不熟悉make,也不想使用它,所以请不要参考在答案中,我读了使用G ++编译多个。 cpp和.h文件,但我需要更多解释我的问题。 解决方案
如果您想手动执行,你可以将所有.cpp文件编译成目标文件。
g ++ -c * .cpp
并链接所有的目标文件
g ++ * .o -o a.out
如果ClassA.cpp被更改,您可以重新编译ClassA.cpp
g ++ -c ClassA.cpp
,然后再次链接它们。
g ++ * .o -o a.out
至少,您不需要重新编译未更改的.c pp文件。
Lets say I have these files:
main.cpp
, ClassA.cpp
, ClassA.h
, ClassB.cpp
, ClassB.h
- The main has
#include "ClassA.h"
and#include "ClassB.h"
and each.cpp
file includes its respective.h
file. Is this correct?
Now I am compiling using g++ *.cpp
after which I get an executable a.exe
(Windows)
- And my question is that is this the right way? And lets say if I only make changes in one file (cpp or h)will this command also recompile the unchanged files(because I see no new files in the folder except a.exe)? Please explain. Also how do I prevent that?
P.S I am not familiar with make and do not want to use that either so please do not refer to that in the answers and I read Using G++ to compile multiple .cpp and .h files but I need more explanation regarding my questions.
If you want to do it manually, you can compile all your .cpp files into object files
g++ -c *.cpp
and link all the object files
g++ *.o -o a.out
If ClassA.cpp is changed, you can just recompile ClassA.cpp
g++ -c ClassA.cpp
and link them all again
g++ *.o -o a.out
At least, you do not need to recompile the unchanged .cpp files.
这篇关于使用g ++编译多个.cpp和.h文件。我做对了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!