本文介绍了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。



有办法吗?


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子目录依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 10:28