可移植地编译整个目录

发布于 2024-11-12 01:20:25 字数 127 浏览 4 评论 0原文

是否有一种干净/可移植的方法可以从给定目录递归下降,将所有找到的 .cpp 文件编译到单个输出文件中?我不确定 makefile 是否能够完成这类事情,或者它是否是某种构建脚本的工作,但我想避免将各种 IDE 的项目文件与我的代码一起维护。

Is there a clean/portable way to descend recursively from a given directory, compiling all found .cpp files into a single output file? I'm not sure if makefiles are capable of this sort of thing, or if it's a job for some kind of build script, but I'd like to avoid maintaining various IDEs' project files along with my code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

君勿笑 2024-11-19 01:20:25

您可以在这里做不同的事情。我建议您使用多平台构建系统,并遵循它的文档。我过去使用过 CMake,但我不知道如何告诉它编译目录中的所有 文件。

优点是用户可以使用 CMake 为大多数常见 IDE 生成项目文件,因此它允许 VisualStudio 用户生成 VS 解决方案、MacOSX 用户生成 Xcode 项目、几乎任何环境中的 Eclipse CDK 项目、Makefile...

There are different things that you can do here. I would suggest that you use a multiplatform build system, and follow the documentation for it. I have used CMake in the past, but I wouldn't know how to tell it to compile all files in a directory.

The advantage is that the user can use CMake to generate project files for most common IDEs, so it would allow VisualStudio users to generate VS solutions, MacOSX users to generate Xcode projects, Eclipse CDK projects in pretty much any environment, Makefiles...

初心未许 2024-11-19 01:20:25

通配符函数,可以是用于匹配这样的模式:

CXX_FILES = $(wildcard src/*.cpp)   # All .cpp files in the directory

这不是递归的,但至少可以让您不必手动指定某个目录中的文件。构建它们的规则看起来像这样:

CXX_FILES = $(wildcard src/*.cpp)   # All .cpp files in the directory
OBJ_FILES = $(CXX_FILES:src/%.cpp=$(OBJ_DIR)/%.o)   # Corresponding .o files

# Rules
all: $(OBJ_FILES)
    g++ $(OBJ_FILES) -o output_filename

$(OBJ_DIR)/%.o: src/%.cpp
    g++ -c 
lt; -o $@

哦,回答你的问题,这个方法是完全可移植的。

There's the wildcard function which can be used to match a pattern like so:

CXX_FILES = $(wildcard src/*.cpp)   # All .cpp files in the directory

This is not recursive, but will at least save you from having to manually specify the files in a certain directory. The rule for building them would look something like this:

CXX_FILES = $(wildcard src/*.cpp)   # All .cpp files in the directory
OBJ_FILES = $(CXX_FILES:src/%.cpp=$(OBJ_DIR)/%.o)   # Corresponding .o files

# Rules
all: $(OBJ_FILES)
    g++ $(OBJ_FILES) -o output_filename

$(OBJ_DIR)/%.o: src/%.cpp
    g++ -c 
lt; -o $@

Oh, and to answer your question, this method is completely portable.

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