我最近开始使用CMake(2.8.12.2,CentOS 6.8中包含的那个),我认为它足以帮助实现我想要的功能,但我无法弄清楚如何:-)所以我呼吁你的智慧帮助我找到缺失点。
我有一个像这样的项目布局:
BaseDir
|
+-->bin (generated by the process)
| |
| +-->Debug
| +-->Release
|
+-->lib (generated by the process)
| |
| +-->Debug
| +-->Release
|
+-->CMakeLists.txt
|
+-->Library_A
| |
| +-->CMakeLists.txt
| +-->include
| +-->src
| | |
| | +-->CMakeLists.txt
| |
| +-->test # Small binary to test solely the library functions
| |
| +-->CMakeLists.txt
|
+-->Library_B (depends on Library_A)
| |
| +-->CMakeLists.txt
| +-->include
| +-->src
| | |
| | +-->CMakeLists.txt
| |
| +-->test # Small binary to test solely the library functions
| |
| +-->CMakeLists.txt
|
+-->Application_1 (depends on Library_B, hence transitivitely depends on Library_A)
| |
| +-->CMakeLists.txt
| +-->include
| +-->src
| |
| +-->CMakeLists.txt
|
+-->Application_2 (depends on Library_A)
|
+-->CMakeLists.txt
+-->include
+-->src
|
+-->CMakeLists.txt
当我把自己置于BaseDir并运行“cmake”时,它就像一个魅力。 Application_1,Application_2,Library_A和Libray_B都以正确的顺序构建,链接等等。
但是,我的想法是也能够站在任何子目录(Application_ ,Library _ )下构建,并且在这种情况下,只构建与其相关的代码(意味着自己,它的测试和依赖)。例如,站在Library_A内部时,只构建该文件夹,而在Library_B中,还构建了Library_A,以及站在Application_1或Application_2下时发生的等效项。另外,独立于我正在触发cmake进程的位置,构建结果(libs或bin)必须始终放在BaseDir / {lib | bin} / {Debug / Release / etc}下,永远不要放在库或应用程序子目录下。例如,它意味着Library_B(依赖于Library_A)或Application_1(依赖于Library_A和B)的链接必须查看BaseDir / lib / {Debug / Release}。
我的目标是在BaseDir下有很多应用程序,所以我想避免每次都要构建它们,如果这不是真的有必要,只需要我想要的单个应用程序。
我已经调查了CMakeLists.txt files for multiple libraries and executables和CMake and finding other projects and their dependencies,但这与我在这里尝试实现的情况并不完全相同。
我尝试过以下内容:
BaseDir/CMakeLists.txt
|
| cmake_minimum_required (VERSION 2.8)
| project (BASE_DIR)
|
| add_subdirectory (Library_A) # No local dependencies
| add_subdirectory (Library_B) # Depends on A
| add_subdirectory (Application_1) # Depends on A and B
| add_subdirectory (Application_2) # Depends on A
|
| # I want all binary outputs (executables and libraries) placed under BaseDir/{lib|bin}/{Debug|Release}
| set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE}") # For the executables
| set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib/${CMAKE_BUILD_TYPE}") # For the static libraries
| set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib/${CMAKE_BUILD_TYPE}") # For the dynamic libraries
|
+-->BaseDir/Library_A/CMakeLists.txt (again, no dependencies)
| |
| | cmake_minimum_required (VERSION 2.8)
| | project (LIB_A)
| |
| | # In case CMake is run from within BaseDir/Library_A and not from BaseDir, I still want the outputs being placed under BaseDir/{lib|bin}/{Debug|Release}
| | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${CMAKE_BUILD_TYPE}") # For the test executables
| | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../lib/${CMAKE_BUILD_TYPE}") # For the static libraries
| | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../lib/${CMAKE_BUILD_TYPE}") # For the dynamic libraries
| |
| | include_directories(include)
| | add_subdirectory (src)
| | add_subdirectory (test)
| | set(${PROJECT_NAME}_INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR}/include CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)
| |
| +-->BaseDir/Library_A/src/CMakeLists.txt
| |
| | cmake_minimum_required (VERSION 2.8)
| |
| | # add all files in the current directory
| | file(GLOB LIB_A_SRCS "*.h" "*.cpp")
| |
| | # Create a library called libA.a
| | add_library(A ${LIB_A_SRCS})
| |
| +-->BaseDir/Library_A/test/CMakeLists.txt
|
| cmake_minimum_required (VERSION 2.8)
|
| # add all files in the current directory
| file(GLOB TEST_A_SRCS "*.h" "*.cpp")
|
| # Create an executable file from sources
| add_executable(TEST_A ${TEST_A_SRCS})
|
| # Link this executable to the library it's testing
| target_link_libraries(TEST_A A)
|
+-->BaseDir/Library_B/CMakeLists.txt (dependency on A)
| |
| | cmake_minimum_required (VERSION 2.8)
| | project (LIB_B)
| |
| | # In case CMake is run from within BaseDir/Library_B and not from BaseDir, I still want the outputs being placed under BaseDir/{lib|bin}/{Debug|Release}
| | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${CMAKE_BUILD_TYPE}") # For the test executables
| | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../lib/${CMAKE_BUILD_TYPE}") # For the static libraries
| | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../lib/${CMAKE_BUILD_TYPE}") # For the dynamic libraries
| |
| | include_directories(include)
| | add_subdirectory (src)
| | add_subdirectory (test)
| | set(${PROJECT_NAME}_INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR}/include CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)
| |
| +-->BaseDir/Library_B/src/CMakeLists.txt
| |
| | cmake_minimum_required (VERSION 2.8)
| |
| | # add all files in the current directory
| | file(GLOB LIB_B_SRCS "*.h" "*.c")
| |
| | # Create a library called libB.a
| | add_library(B ${LIB_B_SRCS})
| |
| | # Add a dependency to Library_A
| | find_library(LIBRARY_A A PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../../Library_A)
| | include_directories("$LIB_A_INCLUDE_DIRECTORIES")
| | target_link_libraries(B ${LIBRARY_A})
| |
| +-->BaseDir/Library_B/test/CMakeLists.txt
|
| cmake_minimum_required (VERSION 2.8)
|
| # add all files in the current directory
| file(GLOB TEST_B_SRCS "*.h" "*.cpp")
|
| # Create an executable file from sources, for both versions of the library
| add_executable(TEST_B ${TEST_B_SRCS})
|
| # Link this executable to the library it's testing
| target_link_libraries(TEST_B B)
|
+-->BaseDir/Application_1/CMakeLists.txt
| |
| | cmake_minimum_required (VERSION 2.8)
| | project (APP_1)
| |
| | # In case CMake is run from within BaseDir/Application_1 and not from BaseDir, I still want the outputs being placed under BaseDir/{lib|bin}/{Debug|Release}
| | # In this case, only executables are generated.
| | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${CMAKE_BUILD_TYPE}")
| |
| | include_directories(include)
| | add_subdirectory (src)
| |
| +-->BaseDir/Application_1/src/CMakeLists.txt
|
| cmake_minimum_required (VERSION 2.8)
|
| # add all files in the current directory
| file(GLOB APP_1_SRCS "*.cpp")
|
| # Create an executable file from sources
| add_executable(EXE_1 ${APP_1_SRCS})
|
| # This should automatically bring Library_A
| find_library(LIBRARY_B B PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../../Library_B)
| include_directories(${LIB_B_INCLUDE_DIRECTORIES})
| target_link_libraries(EXE_1 ${LIBRARY_B})
|
+-->BaseDir/Application_2/CMakeLists/CMakeLists.txt
|
| cmake_minimum_required (VERSION 2.8)
| project (APP_2)
|
| # In case CMake is run from within BaseDir/Application_2 and not from BaseDir, I still want the outputs being placed under BaseDir/{lib|bin}/{Debug|Release}
| # In this case, only executables are generated.
| set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${CMAKE_BUILD_TYPE}")
|
| include_directories(include)
| add_subdirectory (src)
|
+-->BaseDir/Application_2/src/CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
# add all files in the current directory
file(GLOB APP_2_SRCS "*.cpp")
# Create an executable file from sources
add_executable(EXE_2 ${APP_2_SRCS})
# Link this executable to the library it needs
find_library(LIBRARY_A A PATHS ${CMAKE_CURRENT_SOURCE_DIR}../../Library_A)
include_directories(${LIB_A_INCLUDE_DIRECTORIES})
target_link_libraries(EXE_2 ${LIBRARY_A})
但没有运气,因为当我从子目录运行时(例如,BaseDir / Application_1),我得到:
CMake错误:此项目中使用了以下变量,但它们设置为NOTFOUND。 请设置它们或确保它们在CMake文件中正确设置和测试: 的 LIBRARY_B 的
我想“find_library()”调用不足以满足我的需要,这基本上是加载库项目中包含的所有设置,不仅包括库本身,还包括该库添加的include目录。 find_library()的使用方法是什么,将其指向创建库的项目的位置,或实际生成的库(“ .a”或“ .so”)?< / p>
所以,我的主要疑问是:这种布局是否可行?如果是这样,我错过了什么?我应该使用 find_package()或 find_path()之类的内容吗?如何触发从另一个“相同级别”CMakeLists.txt解析CMakeLists.txt配置文件?
PS:我真的需要考虑在任何时候使用“add_dependencies()”吗?如果没有那个命令有什么意义呢?
答案 0 :(得分:0)
首先提出一条建议:在CMake中,您总是希望尝试严格地将构建目录与源目录分开。特别是,作为构建过程的一部分,您永远不应将文件写回源目录。编写脚本,以便它们也可以使用挂载在只读文件系统上的源目录。虽然这看起来似乎是一种任意的限制,但从长远来看,它实际上会帮助你避免许多麻烦。
至于你的实际问题:find_library
是一个解决依赖问题的非常低级的工具。这里的想法基本上是你在系统的某个地方安装了第三方库(它本身对CMake一无所知)的二进制文件,你知道需要通过检查文件系统的内容来找到它们。这不是这种情况,因为您将依赖关系构建为同一CMake配置运行的一部分。
您要做的事情直接取决于依赖项的目标。所以不要这样做:
find_library(LIBRARY_A A)
target_link_libraries(B ${LIBRARY_A})
你会直接写
target_link_libraries(B A)
这当然只有在A
是此时的已知目标时才有效,这是您应该关注的问题。
只要您在同一个CMake运行中继续构建库,这非常简单:如果目标由父目录或兄弟目录添加,您应该能够立即使用它。在您的情况下使事情变得复杂的是,您还希望能够独立构建不同的库。这意味着您需要一种机制,将目标导入您的构建中,使其看起来好像该目标是作为同一CMake运行的一部分再次生成的。
您可以通过添加imported target并设置其界面属性来手动执行此操作,也可以使用CMake的packaging mechanism以自动方式完成此操作。请注意,这些都不是很容易实现的,因此您可能希望从一个CMake运行中发生所有事情的简单情况开始,然后在您更熟悉使用CMake后再添加对分离构建的支持。