Cmake 链接共享库:"No such file or directory" 当包含库中的头文件时

Cmake Linking Shared Library: "No such file or directory" when include a header file from library

我正在学习使用 Cmake 构建一个库。构建库的代码结构如下:

include:  
   Test.hpp       
   ITest.hpp     // interface
src:
   Test.cpp
   ITest.cpp

在CMakeLists.txt中,我用来建库的语句是:

file(GLOB SRC_LIST "src/iTest.cpp" "src/Test.cpp" "include/Test.hpp"
        "include/iTest.hpp"  "include/deadreckoning.hpp")
add_library(test SHARED ${SRC_LIST})
target_link_libraries( test ${OpenCV_LIBS})  // link opencv libs to libtest.so

然后我又写了一个测试文件(main.cpp),把库复制粘贴到同目录下,link库,调用库里面的函数。 这个 CMakeLists.txt 是

cmake_minimum_required(VERSION 2.8)
project(myapp)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -O3 -Wall -ftree-vectorize -ffast-math -funroll-loops")

add_executable(myapp main.cpp)
target_link_libraries(myapp "/home/labUser/test_lib/libtest.so")

如果我不在库中包含头文件,main.cpp 编译并 运行s 成功:

#include <iostream>
using namespace std;

int main(){
    cout << "hello world" << endl;
    return -1;
}

但是当我包含头文件#include "ITest.hpp"时,出现错误:

fatal error: iTest.hpp: No such file or directory
  #include "iTest.hpp"   
compilation terminated.

我不明白为什么它 happens.I 认为我已经 link 成功编辑了库,因为当我 运行 main.cpp 没有包含头文件时,它没有'给出任何 "linking" 错误。而且我认为头文件显然在库中。为什么我不能包括它?谁能帮我解决这个问题?

非常感谢!

你有几个问题。

正在向目标用户传播 header:

虽然您已将包含文件添加到库目标,但您需要让库目标的 消费者 知道如何找到 header.

因此,当您的应用 myapp link 针对您的库目标 test 时,您需要告诉 cmake 添加 ./includemyapp's包括搜索路径。

有一个特殊的 cmake 变量,${CMAKE_CURRENT_LIST_DIR} 解析为当前正在处理的 CMakeLists.txt 目录的路径。

在您的实例中,即 srcinclude 的 parent 文件夹。

./                    <-- ${CMAKE_CURRENT_LIST_DIR} is this directory
+--- CMakeLists.txt
+--- src/
|    +---Test.cpp
|    +---ITest.cpp
+--- include/
     +---Test.hpp
     +---ITest.hpp

为了告诉 cmake 添加路径到它的包含搜索路径,您使用 target_include_directories

为此,路径将是 ${CMAKE_CURRENT_LIST_DIR}/include

所以您要寻找的语法是:

target_include_directories(test PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)

请注意,这意味着您不必将 "include/iTest.hpp""include/Test.hpp" 添加到 SRC_LIST glob,因为编译器将能够从上面的 target_include_directories

链接到您的测试库:

现在您已经创建了您的库并添加了包含目录,要在您的应用程序中实际 使用它,您应该再次使用 target_link_libraries,但是不要t 指定生成的 .so 文件的路径,而是引用您创建的库目标的名称,test

target_link_libraries(myapp test)

现在 myapp 将知道如何找到 Test.hpp,因为它将从您在 myapp 和 [= 之间创建的“依赖关系 link”中获取该信息18=]

因此,假设以下目录结构,以下 CMakeLists.txt 个文件可能有效

src/
+--- library/
|    +--- < sources for your shared library >
+--- app/
     +--- < sources for your application >

src/CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(myapp)

add_subdirectory(library)
add_subdirectory(app)

src/library/CMakeLists.txt

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} 
    -std=c++11 
    -pthread 
    -O3 
    -Wall 
    -ftree-vectorize 
    -ffast-math 
    -funroll-loops")

find_package(OpenCV REQUIRED)

add_library(test SHARED "src/iTest.cpp src/Test.cpp")
target_link_libraries(test ${OpenCV_LIBS})  // link opencv libs to libtest.so
target_include_directories(test PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)

src/app/CMakeLists.txt

add_executable(myapp main.cpp)
target_link_libraries(myapp test)