我只是继承了一些c++代码,这些代码是用一个包含main和其他一些函数的cpp文件编写的。还有一些.h文件包含类及其函数定义。

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


当前回答

您可以使用一个命令,假设所有需要的.cpp和.h文件都在同一个文件夹中。

g++ *.cpp *.h -Wall && ./a.out

它将同时编译和执行。

其他回答

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

ie

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

等等。

或者您可以单独编译它们。然后链接所有结果”。O”文件一起。

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

$g++ -g -Wall cow.cpp cow.cc -o cow.out

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

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

就像rebenvp说的那样:

g++ *.cpp -o output

然后对输出执行如下操作:

./output

但是更好的解决方案是使用make文件。阅读这里了解更多关于make文件的信息。

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

~/In_ProjectDirectory $ g++ coordin_main.cpp coordin_funcc .cpp coordinator .h . cn

~/In_ProjectDirectory $ ./ a.t out .

... 工作! !

使用Linux Mint和Geany IDE

当我将每个文件保存到同一目录时,一个文件没有在该目录中正确保存;坐标文件。h。重新检查,它被保存为坐标。h,而不是->坐标。h。gch。那些小事。 Arg ! !