简体   繁体   English

C++ 在 Cmake 项目中包含库

[英]C++ include library in Cmake project

I am attempting to integrate a C++ library I downloaded into a new project using CMake.我正在尝试使用 CMake 将我下载的 C++ 库集成到一个新项目中。

I downloaded the library ("downloaded_library"), created the build folder, and inside of it ran cmake.. and make .我下载了库(“downloaded_library”),创建了build文件夹,并在其中运行cmake..make . These both ran successfully, and than I navigated to the build folder and ran./example to run the example file that came with the library.这些都成功运行,然后我导航到构建文件夹并运行 ./example 以运行库附带的示例文件。 This also was successful, and so I hoped to add it to another project.这也很成功,所以我希望将它添加到另一个项目中。

I added this working project into the 'libraries' folder of the following directory structure:我将此工作项目添加到以下目录结构的“库”文件夹中:

project
  -libraries
      -downloaded_library
          -build
          -include
              -downloaded_lib
                  -downloaded_lib.h
          -src
          -examples
              -example.cpp
          -CMakeLists.txt
  -src
      -main.cpp
  -build
  -CMakeLists.txt

I hope to run the same code that ran in example.cpp in main.cpp , but I have been unable to get the import working.我希望在main.cpp中运行在example.cpp中运行的相同代码,但我一直无法让导入工作。 I navigate to the build folder and cmake.. runs successfully, but 'make' fails with an import error (fatal error on the include line, can't find the header file).我导航到构建文件夹和cmake..成功运行,但“make”失败并出现导入错误(包含行上的致命错误,找不到 header 文件)。 This makes sense, as I didn't expect the same include line to work (I copy and pasted example.cpp to main.cpp), but I'm not sure what to make that.这是有道理的,因为我没想到相同的包含行可以工作(我将 example.cpp 复制并粘贴到 main.cpp),但我不知道该怎么做。

fatal error: downloaded_lib/downloaded_lib.h: No such file or directory
    1 | #include "downloaded_lib/downloaded_lib.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

In this situation, what should my CMakeLists.txt contain, and what should my #include contain to be able to use the contents of this library in main.cpp in the same way I can in example.cpp ?在这种情况下,我的 CMakeLists.txt 应该包含什么,我的#include应该包含什么才能像在example.cpp中一样使用main.cpp中的这个库的内容?

EDIT--- I changed the package names above for simplicity sake, but the repository of 'downloaded_library' is the following: https://github.com/siposcsaba89/socketcan-cpp .编辑---为简单起见,我更改了上面的 package 名称,但“downloaded_library”的存储库如下: https://github.com/siposcsaba89/socketcan-cpp

My top level CMakeLists.txt looks like this:我的顶级 CMakeLists.txt 如下所示:

# Minimum version of CMake required to build this project
cmake_minimum_required(VERSION 3.0)

# Name of the project
project(projectname)

# Add all the source files needed to build the executable
add_executable(${PROJECT_NAME} src/main.cpp)

add_subdirectory(libraries/downloaded_library)

# Link the executable and the library together
target_link_libraries(${PROJECT_NAME} downloaded_library)

In order to include the header files from the downloaded library, you can add the following to the CMakeLists.txt file:为了从下载的库中包含 header 文件,您可以将以下内容添加到CMakeLists.txt文件中:

find_library(downloaded_lib
             NAMES downloaded_lib
             HINTS "path to downloaded lib file")

if(NOT downloaded_lib)
  message(FATAL_ERROR "downloaded_lib not found!")
endif()

target_include_directories(${PROJECT_EXECUTABLE} PUBLIC "path to download_lib.h")
target_link_libraries(${PROJECT_EXECUTABLE} ${downloaded_lib})

This includes the directory where the header file is located in addition to the library file.除了库文件之外,这还包括 header 文件所在的目录。

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

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