【问题标题】:How to include an open source library into C++ project?如何将开源库包含到 C++ 项目中?
【发布时间】:2021-02-09 13:57:40
【问题描述】:

我正在尝试在我的项目中使用一个开源库,特别是这个here

我已遵循安装指南并在全球范围内使用

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

我可以看到库安装在/usr/local/lib,现在回到我自己的C++代码,当我尝试时

#include <benchmark/benchmark.h>

如指令所示仍然没有出错

'benchmark/benchmark.h' file not found

所以有没有遗漏的步骤?

/usr/local/lib 我有:

libbenchmark.a
libbenchmark_main.a

/usr/local/include 我有:

benchmark

我没有在我的 XCode 项目中使用 CMake。

-------------- 分割------------

深挖问题here.

我将此/usr/local/include 添加到Header Search Paths 中,如下所示。

然后我尝试了

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

但这导致直接构建失败并出现错误

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

【问题讨论】:

标签: c++ xcode cmake


【解决方案1】:

感谢大家的帮助,我终于弄明白了。

在 Xcode 中转到 build settings 并添加头文件和库搜索路径

然后转到linking,添加链接器,我的情况是这样的

【讨论】:

    【解决方案2】:

    假设您的项目也使用cmake,您的CMakeLists.txt 应该包含如下内容:

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

    这应该可以解决包含和链接问题(您没有指定问题的实际性质)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-25
      • 2013-06-06
      • 2018-11-19
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多