Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate header file and source file in CMake?

Tags:

c++

cmake

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.

like image 337
Qinchen Avatar asked Mar 01 '17 13:03

Qinchen


People also ask

Do you include header files in CMake?

To include headers in CMake targets, use the command target_include_directories(...) . Depending on the purpose of the included directories, you will need to define the scope specifier – either PUBLIC , PRIVATE or INTERFACE .

What is the source directory in CMake?

The source directory is where the source code for the project is located. This is also where the CMakeLists files will be found. The binary directory is sometimes referred to as the build directory and is where CMake will put the resulting object files, libraries, and executables.

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

What does CMakeLists TXT do?

CMakeLists. txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory.


1 Answers

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)
like image 194
fandyushin Avatar answered Oct 09 '22 09:10

fandyushin