如何编译目录中的所有 cpp 文件? [英] How can you compile all cpp files in a directory?

查看:23
本文介绍了如何编译目录中的所有 cpp 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多个文件夹中有多个源文件.有没有一种方法可以一次性编译所有文件而无需命名?

I have a number of source files in a number of folders.. Is there a way to just compile all of them in one go without having to name them?

我知道我可以说

g++ -o out *.cpp 

但是当我尝试时

g++ -o out *.cpp folder/*.cpp

我收到一个错误.

这样做的正确方法是什么?我知道使用 makefile 是可能的,但可以直接使用 g++ 来完成吗?

What's the correct way to do this? I know it's possible with makefiles, but can it be done with just straight g++?

推荐答案

通过指定 folder/*.cpp,您是在告诉 g++ 编译 folder 中的 cpp 文件.没错.

By specifying folder/*.cpp you are telling g++ to compile cpp files in folder. That is correct.

您可能缺少的是告诉 g++ 在哪里可以找到那些 cpp 文件 #include 的附加文件.

What you may be missing is telling the g++ where to locate additional files that those cpp files #include.

为此,请告诉您的编译器也使用 -I include 该目录,如下所示:

To do this, tell your compiler to also include that directory with -I like this:

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

在某些情况下,我让编译器忘记了根/当前目录中的内容,因此我使用另一个 -I 手动将其指定到当前目录 .

In some circumstances I have had the compiler forget what was in the root/current directory, so I manually specified it with another -I to the current directory .

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

这篇关于如何编译目录中的所有 cpp 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆