如何在CMake中分隔头文件和源文件?

时间:2017-03-01 13:33:01

标签: c++ cmake

我的项目结构如下:

--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:

 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})

src目录中的CmakeLists.txt:

include_directories(${PROJECT_SOURCE_DIR}/include)

如何在root和src目录中编写CMakelists来实现函数的单独实现?而且,如何在主要中调用它们。 CMake not finding proper header/include files in include_directories中的可能解决方案。但它仍然与我的情况不符。

1 个答案:

答案 0 :(得分:5)

root中的

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

set(TARGET target_name)
add_executable(${TARGET} main.cpp function.cpp)