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.

5
  • 1
    ${SOURCES} shouldn't contain the header file. Use include_directories to specify the include path. Commented Mar 1, 2017 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. Commented Mar 1, 2017 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. Commented Mar 2, 2017 at 13:23
  • @Biffen thank you for your help . Commented Mar 2, 2017 at 13:24
  • solution: stackoverflow.com/questions/28384935/… Commented Mar 2, 2017 at 13:45

2 Answers 2

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) 
Sign up to request clarification or add additional context in comments.

3 Comments

Your solution solved my problem perfectly if main.cpp is in src. But what if main.cpp is in root?
@Qinchen add_executable(${TARGET} ../main.cpp function.cpp) or ${CMAKE_SOURCE_DIR}/main.cpp
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.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.