CMake子文件夹结构?

时间:2018-04-05 12:10:08

标签: c++ cmake

我的文件夹结构就是这样......

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)

1 个答案:

答案 0 :(得分:1)

您可以使用add_subdirectory,将单个相对路径传递到包含另一个CMakeLists.txt的子目录。

对于谷歌测试,已经有一个电话,在你自己的CMakeLists.txt,顺便说一句。

请参阅:https://cmake.org/cmake/help/latest/command/add_subdirectory.html