CMAKE: Build library and link against it

前端 未结 4 1918
挽巷
挽巷 2021-02-13 20:38

I\'m trying to use cmake (on Linux with GNU make and g++) to build a project with two sub-directories: MyLib and MyApp. MyLib contains source for a static library; MyApp needs

相关标签:
4条回答
  • 2021-02-13 20:53
    1. Use "out of the source build". Make a directory used only for build and while in it, call

      cmake <path to the sources, it may be relative>
    2. Either use

      link_directories(${MyProj_BINARY_DIR}/MyLib)

      or make CMakeLists.txt in each subdirectory - that would be better for project larger than very small.

    3. This is a bit tricky, check out CMAKE_BUILD_TYPE in the docs (you can set it and/or "if" by it). You can also set it from command line:

      cmake -DCMAKE_BUILD_TYPE=Debug
    0 讨论(0)
  • 2021-02-13 21:08

    Well, it is better to read this example and do exactly as suggested.

    cmake_minimum_required (VERSION 2.6)
    project (MyProj CXX)
    add_subdirectory(MyLib)
    add_subdirectory(MyApp)
    

    Then for each subdirectory specified, CMakeLists.txt files are created

    MyLib\CMakeLists.txt

    file(GLOB SRC_FILES *.cpp)
    add_library(MyLib ${SRC_FILES})
    

    MyApp\CMakeLists.txt

    file(GLOB SRC_FILES *.cpp)
    add_executable(MyApp ${SRC_FILES})
    target_link_libraries(MyApp MyLib) 
    
    0 讨论(0)
  • 2021-02-13 21:09

    Do not add libraries and executables in the root Cmakelists.txt. Add these libraries and executables in Cmakelists.txt of subdirectories.

    0 讨论(0)
  • 2021-02-13 21:15

    I've discovered the 'optimal' solution to (1)... so, thought I should post it here:

    SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY MyStage)
    SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY MyStage)
    

    The thing that confused me previously is that static libraries are not considered a LIBRARY by Cmake - they're considered to be ARCHIVEs.

    0 讨论(0)
提交回复
热议问题