我对 CMake 很陌生。事实上,我正在通过 Kdevelop4 widh C++ 进行尝试。
我习惯为我创建的每个命名空间创建子目录,即使所有源代码都必须编译并链接到单个可执行文件中。好吧,当我在 kdevelop 下创建一个目录时,它会使用 add_subdirectory 命令更新 CMakeLists.txt 并在其下创建一个新的 CMakeLists.txt,但仅此一项不会将其下的源添加到编译列表中。
我的根 CMakeLists.txt 如下:
project(gear2d)
add_executable(gear2d object.cc main.cc)
add_subdirectory(component)
在 component/ 下,我有想要编译和链接以生成 gear2d 可执行文件的源代码。我怎样才能做到这一点?
CMake常见问题解答有this http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_add_a_dependency_to_a_source_file_which_is_generated_in_a_subdirectory.3F条目,但如果这就是答案,我宁愿保留简单的 Makefiles。
有办法做到这一点吗?
添加子目录只不过是向 CMake 指定它应该进入该目录并在其中查找另一个 CMakeLists.txt 而已。您仍然需要使用源文件创建一个库添加库 http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command%3aadd_library并将其链接到您的可执行文件目标链接库 http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command%3atarget_link_libraries。像下面这样:
在子目录 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} )
顶级目录 CMakeLists.txt
project( gear2d )
add_subdirectory( component )
add_executable( gear2d object.cc main.cc )
target_link_libraries( gear2d component )
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)