Jeremy Stucki
Jeremy Stucki

Reputation: 189

CMake link shared library built from object libraries

I can't get my cmake project running. It should build a library (Core) and then add this library to a new shared library. The problem is that the resulting library does not seem to link correctly against the executable.

fatal error: JNF_NEAT/body.h: No such file or directory


I have a CMake project which is structured as follows:

root
  -> CMakeLists.txt
  -> Core/
     -> CMakeLists.txt
     -> Sources/
  -> Examples/
     -> CMakeLists.txt
     -> Example1/
        -> CMakeLists.txt
        -> Sources/


root/CMakeLists.txt

cmake_minimum_required(VERSION 3.2.2)
project(JNF_NEAT)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/out/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/out/examples)

add_subdirectory(Core)

add_library(JNF_NEAT SHARED $<TARGET_OBJECTS:Core>)
target_link_libraries(JNF_NEAT -lstdc++fs)

add_subdirectory(Examples)


root/Core/CMakeLists.txt

file(GLOB SOURCES Sources/*.cpp Sources/*.h)
add_library(Core OBJECT ${SOURCES})


root/Examples/CMakeLists.txt

add_subdirectory(XOR)


root/Examples/XOR/CMakeLists.txt

include_directories(../../out/lib)

file(GLOB SOURCES Sources/*.cpp Sources/*.h)
add_executable(XOR_EXAMPLE ${SOURCES})
target_link_libraries(XOR_EXAMPLE JNF_NEAT)


The entire source code is available here.


Edit #1

I tried setting target_include_directories(XOR_EXAMPLE BEFORE PUBLIC ../../out/lib) before target_link_libraries

I also tried setting include_directories(out/lib) in the outer most CMakeLists.txt


Edit #2

On Linux this error occures.

Upvotes: 5

Views: 1258

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76297

The error

JNF_NEAT/body.h: No such file or directory

is not a link error, but rather a compilation error.

Check that JNF_NEAT/body.h actually exists. If it does, it might not be included in the list of directories where the compiler looks for #includes. You can set this in CMake with the include_directories command.

Add the given directories to those the compiler uses to search for include files. Relative paths are interpreted as relative to the current source directory.

The include directories are added to the INCLUDE_DIRECTORIES directory property for the current CMakeLists file. They are also added to the INCLUDE_DIRECTORIES target property for each target in the current CMakeLists file. The target property values are the ones used by the generators.

By default the directories specified are appended onto the current list of directories. This default behavior can be changed by setting CMAKE_INCLUDE_DIRECTORIES_BEFORE to ON. By using AFTER or BEFORE explicitly, you can select between appending and prepending, independent of the default.

Upvotes: 3

Related Questions