栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Linux下CMake链接googletest【CMake编写】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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更好的方法

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/717934.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号