Adding multiple executables in CMake

前端 未结 2 1364
陌清茗
陌清茗 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: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} )
    

提交回复
热议问题