我如何告诉 cmake 在哪里输出其构建数据?

发布于 2024-12-14 20:13:08 字数 827 浏览 0 评论 0原文

我如何告诉 cmake 应该在哪里输出其构建数据?

假设我有一个名为 src/ 的源代码目录, 然后由于 cmake 输出了很多文件,我希望他将所有这些文件放入 一个名为 build/ 的目录。 但我希望他将生成的 Makefile 放在项目根目录中, 所以我不必进入构建目录来构建应用程序。

这可以用 cmake 实现吗?

如果我像这样在构建目录中使用cmake,我已经设法得到了cmake:

cd build/
cmake ../src/
make
./hello 

但是最好留在项目根目录中并输入类似的内容

cmake
make 
./hello

我想我需要在项目中放置一个CMakeList.txt root 使用一些神奇的命令告诉他可以在哪里放置目标文件以及在哪里可以找到源代码。

谢谢


更新: 因为我的问题有点模糊。

运行 cmake 命令后,我希望我的树看起来像这样:

src/CMakeLists.txt
src/hello.c 
src/hello.h

build/CMakeCache.txt  
build/CMakeFiles/
build/cmake_install.cmake  

CMakeLists.txt
Makefile

所以问题是 CMakeLists.txt 在这个设置中应该是什么样子。

  • CMakeLists.txt
  • src/CMakeLists.txt

但也许这是不可能的?

How do I tell cmake where it should output its build data?

Let's say I have a dir with the source code called src/,
and then since cmake outputs a lot of files I would like him to put all of that in
a dir called build/.
BUT I would like him to put the generated Makefile in the project root,
so I don't have to go into the build dir to build the application.

Is this possible with cmake?

I have managed to get the cmake out put if I fun cmake in the build dir like this:

cd build/
cmake ../src/
make
./hello 

But it would be nice to stay in the project root and type something like this

cmake
make 
./hello

I guess that I need to put a CMakeList.txt in the project root with some magic commands telling him where he could put the object files and where he can find the source code.

Thanks


Update:
Since my question is a little bit vague.

After I have run the cmake commands this is how I would like my tree to look like:

src/CMakeLists.txt
src/hello.c 
src/hello.h

build/CMakeCache.txt  
build/CMakeFiles/
build/cmake_install.cmake  

CMakeLists.txt
Makefile

So the question is how should the CMakeLists.txt look like in this setup.

  • CMakeLists.txt
  • src/CMakeLists.txt

But maybe that is not possible?

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

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

发布评论

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

评论(4

最美的太阳 2024-12-21 20:13:08

但我希望他将生成的 Makefile 放在项目根目录中,这样我就不必进入构建
构建应用程序的目录。

据我所知,cmake 不是为此设计的,
但您可以留在项目根目录中并输入:
make -C 构建
。/你好
使用自定义构建规则或 set_target_properties,
您可以强制 cmake 将结果可执行文件放入
源目录或者你可以使用
./build/hello

输入“cd build && cmake ..”您只需要一次,
之后 make 会自动启动 cmake,如果有的话
改变了。

BUT I would like him to put the generated Makefile in the project root, so I don't have to go into the build
dir to build the application.

cmake not designed for that, as I know,
BUT you can stay in the project root and type:
make -C build
./hello
with custom build rules or set_target_properties,
you can force cmake to put result executable to
sources directory or you can use
./build/hello

Type "cd build && cmake .." you need only once,
after that make will automaticaly start cmake, if something
changed.

三寸金莲 2024-12-21 20:13:08

cmake 希望您有一个新的构建目录。

cmake wants you to have a fresh build directory.

梦过后 2024-12-21 20:13:08

好吧,我明白你想要的了。我认为,您可以通过 CMakeLists.txt 中的一些机制来实现这一点。当然,如果您不是项目开发人员,这不是选择。

在根 CMakeLists.txt 中,您可以添加 file(WRITE ...) 命令,该命令会将 Makefile 写入 ${CMAKE_SOURCE_DIR}。此 Makefile 将包含每个目标的以下命令:

<target>:
  cd ${CMAKE_BUILD_DIR} && ${CMAKE_MAKE_PROGRAM} <target>

因此,现在您可以从源目录运行 make ,它将在构建目录中构建您的项目。

Okay, i get what you want. I think, you can achieve this with some machinery in CMakeLists.txt. Of course, it's not option if you are not project developer.

In root CMakeLists.txt you can add file(WRITE ...) command, which would write Makefile into ${CMAKE_SOURCE_DIR}. This Makefile would contain these commands for every target:

<target>:
  cd ${CMAKE_BUILD_DIR} && ${CMAKE_MAKE_PROGRAM} <target>

So, now you can run make from source dir and it will build your project in build dir.

长不大的小祸害 2024-12-21 20:13:08

使用即可

cmake .
make

只需在 src 目录中 。 unix 系统上的 (.) 点表示当前目录。请记住,实际上不建议这样做,因为 src 目录中会有很多构建文件,您必须在之后或发布时清理。

Simply use

cmake .
make

in your src directory. The (.) dot on unix systems addresses the current directory. Keep in mind doing so is actually not recommended since there will be a lot of build files in your src directory you'll have to clean up afterwards or at release time.

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