10

My project is in the structure as follows:

--root: main.cpp CMakeLists.txt 

    --src: function.cpp CMakeLists.txt 

    --include: function.h

main.cpp:

#include <iostream>
#include "function.h"
using namespace std;

int main(int argc, char *argv[])
{
    //call module in function.hpp
    return 0;
}

CMakeLists.txt in root directory:

 project(t1)
 cmake_minimum_required(VERSION 2.8)
 add_subdirectory(src)               
 file(GLOB_RECURSE SOURCES
     include/function.h
     src/function.cpp)            
 add_executable(${PROJECT_NAME} ${SOURCES})

CmakeLists.txt in src directory:

include_directories(${PROJECT_SOURCE_DIR}/include)

How to write CMakelists in the root and src directory to realize separate implementation of function? And further more, how to call them in main. Possible solutions in CMake not finding proper header/include files in include_directories. But it still doesn't match my situations.

Community
  • 1
  • 1
Qinchen
  • 381
  • 1
  • 3
  • 14
  • 1
    `${SOURCES}` shouldn't contain the header file. Use `include_directories` to specify the `include` path. – πάντα ῥεῖ Mar 01 '17 at 13:55
  • 1
    @πάνταῥεῖ You *can* put header files among a target's sources. CMake won't try to compile them, but other tools, e.g. IDEs, can show the headers in lists/trees, which can be quite nice. – Biffen Mar 01 '17 at 14:04
  • How to show include/headers in Qt trees is exactly what I desired. But I cannot quite understand specific operation you've mentioned above. Maybe you can show me how to add operations to the accepted answer below. – Qinchen Mar 02 '17 at 13:23
  • @Biffen thank you for your help . – Qinchen Mar 02 '17 at 13:24
  • solution: http://stackoverflow.com/questions/28384935/qtcreator-cmake-project-how-to-show-all-project-files – Qinchen Mar 02 '17 at 13:45

2 Answers2

8

in root:

project(t1)
cmake_minimum_required(VERSION 2.8)
include_directories(include)
add_subdirectory(src)               

in src:

set(TARGET target_name)
add_executable(${TARGET} main.cpp function.cpp)
fandyushin
  • 2,350
  • 2
  • 20
  • 32
  • Your solution solved my problem perfectly if main.cpp is in src. But what if main.cpp is in root? – Qinchen Mar 02 '17 at 13:12
  • 3
    @Qinchen `add_executable(${TARGET} ../main.cpp function.cpp)` or `${CMAKE_SOURCE_DIR}/main.cpp` – fandyushin Mar 02 '17 at 13:18
  • 4
    Why would you want to set your target in the src subdirectory? This strikes me as a bit messed up. I want to set my target top level and tell it to use the stuff in the src directory. – RichieHH Mar 05 '19 at 23:41
1

Today, I have the same question and I found the solution.

CMakeLists.txt in root directory:

cmake_minimum_required(VERSION 3.21.3)
project(t1)
add_executable(${PROJECT_NAME} main.cpp)
add_subdirectory(src)
target_include_directories(${PROJECT_NAME} PUBLIC include)
target_link_directories(${PROJECT_NAME} PRIVATE src)
target_link_libraries(${PROJECT_NAME} function)

CMakeLists.txt in src directory:

include_directories(../include)
add_library(function function.cpp)

Thanks Code, Tech and Tutorial and his video https://www.youtube.com/watch?v=kEGQKzhciKc&t=606s. This helps me to have an idea to solve this problem