folibis
folibis

Reputation: 12874

CMake can't find include file from subfolder

I want to add to my CMake project some subfolder which contains some additional functionality. MyProject is a shared library. Ideally it will be fine to just include files from subfolder and to compile all them together. But so far I've found a way only to include that as a static library that suits me too. So my files structure should look like the following:

MyProject
    src
        main.cpp
    include
        main.h
    CMakeLists.txt
    3dparty
        SomeFolder
           src
               file.cpp
           include
               file.h
           CMakeLists.txt

MyProject/CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(MyProject)
file(GLOB SRC . src/*.cpp)
file(GLOB INC . include/*.h)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
add_library(${PROJECT_NAME} SHARED ${SRC} ${INC})
target_include_directories(${PROJECT_NAME} PUBLIC
  ${CMAKE_CURRENT_SOURCE_DIR}/include
)
add_subdirectory(3dparty/SomeFolder)

and 3dparty/SomeFolder/CMakeLists.txt:

project (SomeFolder)
file(GLOB SRC . src/*.cpp)
file(GLOB INC . include/*.h)
add_library(${PROJECT_NAME} STATIC ${SRC} ${INC})
target_include_directories(${PROJECT_NAME} PUBLIC
  ${CMAKE_CURRENT_SOURCE_DIR}/include
)

I can compile the project and I see that compiler produses SomeFolder.a so it looks good. But for some reason I can't include files from 3dparty/SomeFolder/include in main.*. So far I've tried:

#include "file.h"
#include "3dparty/SomeFolder/file.h"

but nothing works. I get error: 'file.h' file not found.

What I did wrong? How can I include files from subproject?

Upvotes: 1

Views: 2487

Answers (1)

Arne J
Arne J

Reputation: 415

Because you forgot to link the sublibrary to your main library. Usr target_link_libraries()

Upvotes: 1

Related Questions