【发布时间】:2018-04-05 12:10:08
【问题描述】:
我的文件夹结构是这样的……
Project2
CMakeLists.txt
lib
--include
--src
proj
--include
--src
test
--include
--src
我有以下 CMakeLists.txt。如您所见,所有 3 个子文件夹的 cmake 命令都包含在此文件夹中。我不认为这是最好的方法。我认为最好让 CMakeLists.txt 只关注一个文件夹。
我想将其分解,以便 lib、proj 和 test 文件夹各有一个 CMakeLists.txt,但仍是整个项目的一部分。这可能吗?
当我使用 Visual Studio(cmake“Visual Studio 2017”)生成我的项目时,我希望看到 lib、proj 和 test 拥有自己的项目并成为解决方案的一部分。
cmake_minimum_required (VERSION 3.9)
project (Project2)
include (CTest)
# The version number.
set (Project2_VERSION_MAJOR 1)
set (Project2_VERSION_MINOR 0)
# add the binary tree to the search path for include files
# so that we will find Project1Config.h
include_directories ("${PROJECT_BINARY_DIR}")
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
${CMAKE_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
set (LIB_SRCS lib/include/example_lib.h lib/src/example_lib.cpp)
set (PROJECT_SRCS proj/include/example.h proj/src/example.cpp)
set (TEST_SRCS test/src/example_add.cpp test/src/example_subtract.cpp)
# add library
add_library (lib ${LIB_SRCS})
# add the executable
add_executable (Project2 proj/src/main.cpp ${PROJECT_SRCS})
target_link_libraries (Project2 lib)
target_link_libraries (Project2 ${EXTRA_LIBS})
# add test project
add_executable (UnitTests ${PROJECT_SRCS} ${TEST_SRCS})
target_link_libraries (UnitTests lib)
target_link_libraries (UnitTests gtest_main)
#
#
# INSTALL
#
#
# add the install targets
#install (TARGETS Project2 DESTINATION bin)
#
#
# TESTS
#
#
add_test (NAME unit COMMAND ${CMAKE_BINARY_DIR}/UnitTests)
【问题讨论】: