Adding multiple executables in CMake

前端 未结 2 1365
陌清茗
陌清茗 2020-12-07 09:01

My code in a C++ project is organised as follows

  • I have several .cpp and .h files which contains my classes
  • I have several <
相关标签:
2条回答
  • 2020-12-07 09:26

    My suggestion is to tackle this in two phases:

    1. Build a library from the .cpp and .h files, using add_library
    2. Iterate through all your .cxx files and create an executable from each, using add_executable and foreach

    Build the library

    This could be something as simple as

    file( GLOB LIB_SOURCES lib/*.cpp )
    file( GLOB LIB_HEADERS lib/*.h )
    add_library( YourLib ${LIB_SOURCES} ${LIB_HEADERS} )
    

    Build all the executables

    Simply loop over all the .cpp files and create separate executables.

    # If necessary, use the RELATIVE flag, otherwise each source file may be listed 
    # with full pathname. RELATIVE may makes it easier to extract an executable name
    # automatically.
    # file( GLOB APP_SOURCES RELATIVE app/*.cxx )
    file( GLOB APP_SOURCES app/*.cxx )
    foreach( testsourcefile ${APP_SOURCES} )
        # I used a simple string replace, to cut off .cpp.
        string( REPLACE ".cpp" "" testname ${testsourcefile} )
        add_executable( ${testname} ${testsourcefile} )
        # Make sure YourLib is linked to each app
        target_link_libraries( ${testname} YourLib )
    endforeach( testsourcefile ${APP_SOURCES} )
    

    Some warnings:

    • file( GLOB ) is usually not recommended, because CMake will not automatically rebuild if a new file is added. I used it here, because I do not know your sourcefiles.
    • In some situations, source-files may be found with a full pathname. If necessary, use the RELATIVE flag for find( GLOB ... ).
    • Manually setting the source-files requires a change to CMakeLists.txt, which triggers a rebuild. See this question for the (dis-)advantages of globbing.
    • I generated the testname using a string( REPLACE ... ). I could have used get_filename_component with the NAME_WE flag.

    Concerning "general" CMake info, I advise you to read some of the broad "CMake Overview" questions already asked here on stackoverflow. E.g.:

    • CMake tutorial
    • What are the dusty corners a newcomer to CMake will want to know?
    0 讨论(0)
  • 2020-12-07 09:48

    this CMakeLists.txt works for my OpenCV project
    assuming *.cpp files are in the same directory as CMakeLists.txt

    cmake_minimum_required(VERSION 3.5)
    
    project(opencv LANGUAGES CXX)
    
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(OpenCV REQUIRED)
    include_directories( ${OpenCV_INCLUDE_DIRS} )
    
    file( GLOB APP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp )
    foreach( sourcefile ${APP_SOURCES} )
        file(RELATIVE_PATH filename ${CMAKE_CURRENT_SOURCE_DIR} ${sourcefile})
        string( REPLACE ".cpp" "" file ${filename} )
        add_executable( ${file} ${sourcefile} )
        target_link_libraries( ${file} ${OpenCV_LIBS} )
    endforeach( sourcefile ${APP_SOURCES} )
    
    0 讨论(0)
提交回复
热议问题