Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake link shared library built from object libraries

Tags:

c++

cmake

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.

like image 933
Jeremy Stucki Avatar asked Oct 19 '22 01:10

Jeremy Stucki


1 Answers

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.

like image 142
Ami Tavory Avatar answered Oct 21 '22 05:10

Ami Tavory