Linux下CMake链接googletest【CMake编写】

Linux下CMake链接googletest【CMake编写】,第1张

Linux下CMake链接googletest【CMake编写】 源码下载
git clone https://github.com/google/benchmark.git
git clone https://github.com/google/googletest.git benchmark/googletest

这两个源代码分别编译 并执行

sudo make install

文件被安装在

/usr/local/include

库文件被安装在

/usr/local/lib
CMake文件链接文件简介 通过find_package查找

添加代码如下:

find_package(Ceres REQUIRED)
include_directories(${Ceres_INCLUDE_DIRS})
target_link_libraries(Test ${Ceres_LIBRARIES})

以Ceres库为例,find_package通过findceres.cmake文件查找ceres的头文件位置和.a文件(库文件)位置,在find.cmake文件中包含了Ceres_INCLUDE_DIRS和Ceres_LIBRARIES的位置。

通过link_directories直接设置库文件和头文件的地址

以本例为例

include_directories("/home/usrname/benchmark/googletest/googletest/include/gtest")
link_directories("/usr/local/lib")
target_link_libraries(run libbenchmark.a)
include_directories指定了googletest的头文件的搜索路径
link_directories指定了googletest的.a文件的搜索路径
target_link_libraries将.a文件和可执行文件名链接

查找.a文件的位置:

/usr/lib或/usr/local/lib中 使用whereis命令查找指定的.a文件

也可以尝试用whereis搜索.cmake文件的路径,我想不起来在哪个路径了。

总结

使用find_package命令查找不到文件时是因为没有对应的findXXX.cmake文件。这时候需要使用link_directories命令直接设置库文件和头文件的地址。

添加pthread链接

我们需要的是pthread的库,不需要lpthread的库(lpthread会有一些问题,导致编译不过)
在cmake中添加pthread而不是lpthread:

set_target_properties(${TARGET} PROPERTIES
COMPILE_FLAGS "-pthread"
link_FLAGS "-pthread")
成果展示:
2022-01-25T14:36:29+08:00
Running ./run
Run on (16 X 4800 MHz CPU s)
CPU Caches:
  L1 Data 32 KiB (x8)
  L1 Instruction 32 KiB (x8)
  L2 Unified 256 KiB (x8)
  L3 Unified 16384 KiB (x1)
Load Average: 0.61, 0.59, 0.54
***WARNING*** CPU scaling is enabled, the benchmark real time measurements may be noisy and will incur extra overhead.
***WARNING*** Library was built as DEBUG. Timings may be affected.
---------------------------------------------------------------
Benchmark                     Time             CPU   Iterations
---------------------------------------------------------------
bench_array_operator        161 ns          160 ns      4085540
bench_array_at              160 ns          159 ns      4395393
bench_array_get             160 ns          160 ns      4384017

googletest原理详解链接。
使用的也是上面的例子。
CMake链接第三方库三种方法
CMake文件链接pthread更好的方法

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5714253.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存