Friday, October 29, 2021

[SOLVED] How to separate header file and source file in CMake?

Issue

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.


Solution

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)


Answered By - fandyushin