简体   繁体   English

从对象库构建的CMake链接共享库

[英]CMake link shared library built from object libraries

I can't get my cmake project running. 我无法让我的cmake项目运行。 It should build a library (Core) and then add this library to a new shared library. 它应该构建一个库(Core),然后将此库添加到新的共享库中。 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: 我有一个CMake项目,其结构如下:

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


root/CMakeLists.txt 根/的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 根/核心/的CMakeLists.txt

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


root/Examples/CMakeLists.txt 根/实施例/的CMakeLists.txt

add_subdirectory(XOR)


root/Examples/XOR/CMakeLists.txt 根/实施例/ 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 编辑#1

I tried setting target_include_directories(XOR_EXAMPLE BEFORE PUBLIC ../../out/lib) before target_link_libraries 我尝试设置target_include_directories(XOR_EXAMPLE BEFORE PUBLIC ../../out/lib)之前target_link_libraries

I also tried setting include_directories(out/lib) in the outer most CMakeLists.txt 我还尝试在最外面的CMakeLists.txt中设置include_directories(out/lib)


Edit #2 编辑#2

On Linux this error occures. 在Linux上出现错误。

The error 错误

JNF_NEAT/body.h: No such file or directory JNF_NEAT / body.h:没有这样的文件或目录

is not a link error, but rather a compilation error. 不是链接错误,而是编译错误。

Check that JNF_NEAT/body.h actually exists. 检查JNF_NEAT/body.h确实存在。 If it does, it might not be included in the list of directories where the compiler looks for #include s. 如果是,它可能不包含在编译器查找#include s的目录列表中。 You can set this in CMake with the include_directories command. 您可以使用include_directories命令在CMake中进行设置。

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. include目录被添加到当前CMakeLists文件的INCLUDE_DIRECTORIES目录属性中。 They are also added to the INCLUDE_DIRECTORIES target property for each target in the current CMakeLists file. 它们还会添加到当前CMakeLists文件中每个目标的INCLUDE_DIRECTORIES目标属性中。 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. 通过将CMAKE_INCLUDE_DIRECTORIES_BEFORE设置为ON,可以更改此默认行为。 By using AFTER or BEFORE explicitly, you can select between appending and prepending, independent of the default. 通过明确使用AFTER或BEFORE,您可以在附加和前置之间进行选择,与默认值无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM