CMake with include and source paths - basic setup

前端 未结 3 1502
无人及你
无人及你 2020-12-07 07:56

I\'m trying to set up a test project looking like my own project just to get things working first and it looks like this:

/MainProject/inc/main.h
/MainProjec         


        
相关标签:
3条回答
  • 2020-12-07 08:17

    You could do it like following.

    • CMakeLists.txt in your MainProject directory:

      project(MainProject)
      
      add_subdirectory(LibProject/src)
      add_subdirectory(MainProject/src)
      
    • CMakeLists.txt in your LibProject/src directory:

      include_directories(${PROJECT_SOURCE_DIR}/LibProject/inc/)
      add_library(LibProject test.cpp)
      
    • CMakeLists.txt in your MainProject/src directory:

      include_directories(${PROJECT_SOURCE_DIR}/MainProject/inc/)
      add_executable(MainProject main.cpp)
      target_link_libraries(MainProject LibProject)
      
    0 讨论(0)
  • 2020-12-07 08:20

    You need a CMakeLists.txt for each source subdirectory. Your structure should look something like this:

    root
    |-MainProject
    | |-inc
    | | '-main.h
    | |-src
    | | |-main.cpp
    | | '-CMakeLists.txt
    | '-CMakeLists.txt 
    |-LibProject
    | |-inc
    | | '-test.h
    | |-src
    | | |-test.cpp
    | | '-CMakeLists.txt
    | '-CMakeLists.txt
    '-CMakeLists.txt
    

    Content of root/CMakeLists.txt:

    project(MyProject)
    add_subdirectory(MainProject)
    add_subdirectory(LibProject)
    

    Content of LibProject/CMakeLists.txt and MainProject/CMakeLists.txt:

    add_subdirectory(src)
    

    Content of LibProject/src/CMakeLists.txt:

    # Notice name prefix of this variable, set by CMake according
    # to value given with "project()" in the root CMakeLists.txt.
    include_directories(${MyProject_SOURCE_DIR}/LibProject/inc)
    add_library(LibProject test.cpp)
    

    Content of MainProject/src/CMakeLists.txt:

    include_directories(${MyProject_SOURCE_DIR}/MainProject/inc)
    # I assume you want to use LibProject as a library in MainProject.
    include_directories(${MyProject_SOURCE_DIR}/LibProject/inc)
    link_directories(${MyProject_SOURCE_DIR}/LibProject/src)
    add_executable(MainProject main.cpp)
    target_link_libraries(MainProject LibProject)
    

    Then configure and build with:

    $ cd root
    $ mkdir build
    $ cd build
    $ cmake ..
    $ make
    
    0 讨论(0)
  • 2020-12-07 08:24

    In my case I wanted to do it with a single CMakeList. And it worked for me. I add my solution in case it serves to anyone.

    This is what I did in my case:

    My structure:
    Project
    |CMakeLists.txt
    |-src
    | |*.cpp
    | |*.c
    |-include
    | |*.hpp
    | |*.h
    

    My CMakeLists.txt have to main parts:

    include_directories(
            ${PROJECT_SOURCE_DIR}/include
            ${PROJECT_SOURCE_DIR}/src
    )
    

    ^ Enables .cpp files to add headers in the include folder.

    file(GLOB all_SRCS
            "${PROJECT_SOURCE_DIR}/include/*.h"
            "${PROJECT_SOURCE_DIR}/include/*.hpp"
            "${PROJECT_SOURCE_DIR}/src/*.cpp"
            "${PROJECT_SOURCE_DIR}/src/*.c"
            )
    

    ^ Just add whatever is in those folders.

    PS: if you want to see the full CMakeLists.txt go the the project link NEGU93/ForbiddenDesert.

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