简体   繁体   English

如何将开源库包含到 C++ 项目中?

[英]How to include an open source library into C++ project?

I am trying to use an open-source library into my project, specifically this one here .我正在尝试在我的项目中使用一个开源库,特别是这里的这个。

I have followed the installation guidelines and installed it globally with我已遵循安装指南并在全球范围内安装了它

sudo cmake --build "build" --config Release --target install

And I could see that the library is installed in /usr/local/lib , now back to my own C++ code, when I tried to我可以看到该库安装在/usr/local/lib中,现在回到我自己的 C++ 代码

#include <benchmark/benchmark.h>

as shown in the instruction still no go with error如指令所示仍然没有 go 错误

'benchmark/benchmark.h' file not found

SO is there any step that is missing?那么是否缺少任何步骤?

In /usr/local/lib I have:/usr/local/lib我有:

libbenchmark.a
libbenchmark_main.a

And in /usr/local/include I have:/usr/local/include我有:

benchmark

I am not using CMake for my XCode project.我没有在我的 XCode 项目中使用 CMake。

-------------- Segmentation ------------ -------------- 分割 ------------

After digging on the question here.在挖掘了这里的问题之后。

I added this /usr/local/include into the Header Search Paths as following.我将此/usr/local/includeHeader Search Paths中,如下所示。

在此处输入图像描述

And then I tried然后我尝试了

#include "benchmark/benchmark.h" // I tried <benchmark/benchmark.h> as well

But this resulted in a direct build failure with error但这导致直接构建失败并出现错误

Undefined symbols for architecture x86_64:
  "benchmark::internal::InitializeStreams()", referenced from:
      ___cxx_global_var_init in main.o
ld: symbol(s) not found for architecture x86_64

Thanks for the many helps, I have finally figured it out.感谢大家的帮助,我终于弄明白了。

Go to build settings in Xcode and add both header and library search path Go 在 Xcode 中build settings并添加 header 和库搜索路径

在此处输入图像描述

And then go to linking , add linkers, for my case it looks like this然后 go 到linking ,添加链接器,就我而言,它看起来像这样

在此处输入图像描述

Assuming your project uses cmake too, your CMakeLists.txt should contain something like this:假设您的项目也使用cmake ,您的CMakeLists.txt应该包含如下内容:

find_package(benchmark REQUIRED)

add_executable(YouLibBenchamrkTarget sources1.cpp ...)
target_link_libraries(YouLibBenchamrkTarget YourLibraryTarget benchmark::benchmark)

This should resolve problems with include and linking issues (you didn't specified actual nature of your problem).这应该解决包含和链接问题的问题(您没有指定问题的实际性质)。

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

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