I have a project with the following architecture:

. ├── CMakeLists.txt ├── include │ └── Core │ ├── FiniteStateMachine.h │ └── IFiniteStateMachine.h ├── source │ └── Core │ └── FiniteStateMachine.cpp └── tests ├── Core │ └── FiniteStateMachine_tests.cpp └── main_tests.cpp 

To compile in CLion, I wrote the file CMakeLists.txt, but the file turned out to be super crooked.

 cmake_minimum_required(VERSION 3.8) get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) project(${PROJECT_NAME} CXX) if (IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include) list(APPEND PATH_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/include) FILE(GLOB_RECURSE HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/*) endif() FILE(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/source/*) include_directories(${PATH_INCLUDES}) add_library(${PROJECT_NAME} ${SOURCES} ${HEADERS}) project(${PROJECT_NAME}_test CXX) if (IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include) list(APPEND PATH_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/include) FILE(GLOB_RECURSE TEST_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/*) endif() FILE(GLOB_RECURSE TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/source/* ${CMAKE_CURRENT_SOURCE_DIR}/tests/*) add_executable(${PROJECT_NAME} ${TEST_SOURCES} ${TEST_HEADRES}) target_link_libraries(${PROJECT_NAME} gtest gmock pthread) 

Immediately make a reservation that I had no business with CMake before, but I was apparently banned in Google, please do not scold too much!)

Can anyone share their experience as with such an architecture of a project should CMake be used?

For testing, I use GTest (gmock), it is desirable that these libraries are not downloaded from GitHub, but located on the user's computer.

Also, for the future, I would like to be able to exclude some files from certain assemblies (let's say for testing) (also for an example: main.cpp (when building a binary, not a library)) because they can initialize objects that need " to lock "in the case of testing (roughly speaking various singletons, etc.)

Thanks a lot in advance!

  • There are no "right" ways to use, use as you like. When there are specific problems, then you will change the structure and ask for advice. Personally, I would not include source code in this way - I prescribe each source manually, but this is what I came to myself, it is so convenient for me . - ixSci
  • I don’t ask for the “right” path, in my case CLion swears and does not allow to build a library normally, although if I manually use CMake, everything seems ok. I rather want to get a certain set of "functions / macros" from which you need to dance. Ideally, achieve at least 2 assembly configurations. 1 - build the library, 2 - build the tests, so that at the same time CLion normally worked out this file, well, then I will not have problems connecting the sources and libraries. - MeatBoy
  • And about the source, in fact, too, not everything is unambiguous. I have a project (quite large) and it just so happens that in one branch I have several files with one name, and in the other - another (refactoring was performed). How then to do in this case? In CVS, this file does not want to be added, but the same CLion will be cursed if a specific file is not in CMakeLists.txt - MeatBoy
  • Contact CLion technical support - Abyx
  • I think that the problem is rather in CMakeLists.txt - MeatBoy

2 answers 2

So far, I have come to this option, it works more or less in CLion, but I always have to build it along with the tests, otherwise it says that the executable file is not specified.

 cmake_minimum_required(VERSION 3.8) #--- имя проекта совпадает с именем каталога проекта set (CMAKE_CXX_STANDARD 11) set (PROJECT_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) project( FSM CXX ) ######################################################################## #--- обработка хедеров if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include) list(APPEND PATH_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/include/*) FILE (GLOB_RECURSE HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/*) endif() include_directories(${PATH_INCLUDES}) ######################################################################## #--- обработка cpp-файлов FILE (GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/source/*) FILE (GLOB_RECURSE TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/source/* ${CMAKE_CURRENT_SOURCE_DIR}/tests/*) ######################################################################## include_directories(./include) ######################################################################## #--- создание цели проекта add_library(FSM ${SOURCES} ${HEADERS} ) ######################################################################## enable_testing() add_executable(FSM_test ${INCLUDES} ${TEST_SOURCES}) add_test(FSM_test runUnitTests) target_link_libraries(FSM_test gmock gtest pthread) 

Perhaps someone will be able to make a review, suggest what points you can fix / improve.

    Here's a link to git - there's a similar project https://github.com/beeen007/CMake_tester

    Further you probably know cmake, then make

    Please note that in each included directory, many recommend using the new cmake included as a library in the main assembly file (and this is better for the linker)