CMake子目录依赖

编程入门 行业动态 更新时间:2024-10-26 19:24:05
本文介绍了CMake子目录依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是CMake的新手。事实上,我通过Kdevelop4 widh C ++尝试。

我有为每个我创建的命名空间创建子目录的习惯,即使所有的源必须编译和链接转换为单个可执行文件。好吧,当我在kdevelop下创建一个目录,它用一个add_subdirectory命令更新CMakeLists.txt,并在它下面创建一个新的CMakeLists.txt,但单独不会将其下的源码添加到编译列表。

我有根CMakeLists.txt如下:

项目(gear2d) add_executable(gear2d object.cc main.cc) add_subdirectory(component)

在组件/我有我想要编译和链接的来源,以产生gear2d可执行文件。

CMake常见问题有这个条目,但如果这是答案,我宁愿留在纯makefile。

有办法吗?

<添加一个子目录不能指定CMake进入目录,并在那里寻找另一个CMakeLists.txt。您仍然需要使用 add_library

在子文件夹CMakeLists.txt

set(component_SOURCES ...)#为此处的组件添加源文件#可以使用文件glob(取消注释下一行)#file(GLOB component_SOURCES * .cpp) add_library(component $ {component_SOURCES})

Top-dir CMakeLists.txt

project(gear2d) add_subdirectory(component) add_executable(gear2d object.cc main.cc) target_link_libraries(gear2d component)

I am very new to CMake. In fact, I am trying it through Kdevelop4 widh C++.

I have the habit of creating subdirs for every namespace I create, even if all the sources must be compiled and linked into a single executable. Well, when i create a directory under kdevelop, it updates CMakeLists.txt with a add_subdirectory command and creates a new CMakeLists.txt under it, but that alone does not add the sources under it to the compilation list.

I have the root CMakeLists.txt as follows:

project(gear2d) add_executable(gear2d object.cc main.cc) add_subdirectory(component)

Under component/ I have the sources I want to be compiled and linked to produce the gear2d executables. How can I accomplish that?

CMake FAQ have this entry but if thats the answer I'd rather stay with plain Makefiles.

Is there a way of doing this?

解决方案

Adding a subdirectory does not do much more than specify to CMake that it should enter the directory and look for another CMakeLists.txt there. You still need to create a library with the source files with add_library and link it to your executable with target_link_libraries. Something like the following:

In the subdir CMakeLists.txt

set( component_SOURCES ... ) # Add the source-files for the component here # Optionally you can use file glob (uncomment the next line) # file( GLOB component_SOURCES *.cpp )below add_library( component ${component_SOURCES} )

Top-dir CMakeLists.txt

project( gear2d ) add_subdirectory( component ) add_executable( gear2d object.cc main.cc ) target_link_libraries( gear2d component )

更多推荐

CMake子目录依赖

本文发布于:2023-10-04 16:21:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467192.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子目录   CMake

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!