buildcmakeprojectexternal-project

cmake: how to include/compile files out of the project directory?


I have this directory structure:

projects/
    project1/
        src/
            main.cpp
            CMakeLists.txt
    project2/
        src/
            file1.h
            file1.cpp
            test1.cpp

The top level projects directory cannot be considered a top level project, but just a collection of unrelated projects, so I would not put a CMakeLists.txt file at that level.

I want project1 to include files from project2 without specifying the full path, e.g.:

// main.cpp
#include "file1.h"

And I also want implementation files of project2 to be built in project1.

I need project2 not to be a library, but just use its files like if they were part of project1.

I am using cmake, and this is the CMakeLists.txt file I wrote (it does not work):

cmake_minimum_required(VERSION 2.8)
project(project1)
add_subdirectory(src)
add_subdirectory(../../project2/src)

Even specifying the full path to project2 does not work: ${CMAKE_CURRENT_SOURCE_DIR)/../../project2/src

I get "fatal error: file1.h: no such file or directory" from make.


Solution

  • To use external include files, this works:

    include_directories