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

发布于 2024-09-08 18:49:03 字数 263 浏览 5 评论 0原文

我刚刚继承了一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

平定天下 2024-09-15 18:49:03

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

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

等等。

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

list all the other cpp files after main.cpp.

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.

是伱的 2024-09-15 18:49:03

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

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

To compile separately without linking you need to add -c option:

g++ -c myclass.cpp
g++ -c main.cpp
g++ myclass.o main.o
./a.out
愁以何悠 2024-09-15 18:49:03

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

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

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

# "all" is the 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

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

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?

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:

# "all" is the 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 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.

抚笙 2024-09-15 18:49:03

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

  1. 假设您有 5 个 cpp 文件,您所要做的就是使用 * 而不是键入每个 cpp 文件名,例如 g++ -c *.cpp -o myprogram
  2. 这将生成 "myprogram"
  3. 运行程序 ./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.

  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

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.

十秒萌定你 2024-09-15 18:49:03

.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 type g++ 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

没有心的人 2024-09-15 18:49:03

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

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

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

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

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.

岛徒 2024-09-15 18:49:03

正如 rebenvp 所说,我使用:

g++ *.cpp -o output

然后对输出执行此操作:

./output

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

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

As rebenvp said I used:

g++ *.cpp -o output

And then do this for output:

./output

But a better solution is to use make file. Read here to know more about make files.

Also make sure that you have added the required .h files in the .cpp files.

彼岸花ソ最美的依靠 2024-09-15 18:49:03

您可以使用多个 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).

夜吻♂芭芘 2024-09-15 18:49:03

如果您想在 cpp 文件中使用 #include ,您可以使用:

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

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

g++ *.cpp -I. -o out
静若繁花 2024-09-15 18:49:03

您可以使用单个命令来完成此操作,假设所有需要的 .cpp.h 文件都位于同一文件夹中。

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

它将同时编译和执行。

You can do that using a single command assuming all the needed .cpp and .h files are in the same folder.

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

It will compile and execute at the same time.

风柔一江水 2024-09-15 18:49:03

我曾经使用自定义 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.

往日 2024-09-15 18:49:03

在命令行中使用编译器时,应注意以下事项:
您不需要编译头文件,因为头文件在使用 include 指令的脚本中被替换。
您将需要编译并链接实现和脚本文件。
例如,让cow.h为头文件,cow.cpp为实现文件,cow.cc(c++文件可以具有扩展名.cpp,.cc,.cxx,.C,.CPP,.cp)为脚本文件。
由于 c++ 文件的 gcc 编译器表示法是 g++,因此我们可以使用选项“-g”和“-Wall”编译和链接文件,

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

用于调试信息并获取错误警告。这里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

$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

大姐,你呐 2024-09-15 18:49:03

〜/ 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!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文