GTest问题

1、cmkae出现"C:/Users/xxx/gcc/bin/gcc.exe"is not able to compile a simple test program.问题

  • 在camke时添加-DCMAKE_C_COMPILER=gcc-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++-DCMAKE_MAKE_PROGRAM=make指定gcc、g++与make

2、测试私有成员函数出现__xfer_bufptrs redeclared with different access

添加以下代码,注意==不要包含gtest/gtest.h头文件==,若多包含可能会出现struct std::__cxx11::basic_stringbuf<_CharT, _Traits, _Alloc>::__xfer_bufptrs' redeclared with different access错误

#define private public
#define protected public
#undef private
#undef protected

为啥不能#define private public了?

3、make时,测试cpp文件出现 未定义 被测试cpp文件 的函数 undefined reference to Update(int const&)'

主要原因就是被测cpp文件是个孤立的文件
对应在CMakeLists.txt中即问题出在编写CMakeLists.txt时未包含被测cpp文件或者未正确包含被测cpp文件

  • 如:set(SOURCES src/function/environment/vehicle.cpp )
    路径不对导致找不到被测cpp文件

4、出现cannot find -lgtestcannot find -lgtest_main 错误

找不到libgtest.alibgtest_main.a造成的,检查CMakeLists.txt中的link_directories所包含的libgtest.a libgtest_main.a路径是否正确

5、CMakeLists.txt发出The maximum full path to an object file is 250 characters 警告

CMake Warning in CMakeLists.txt:
  The object file directory C:/Users/xxx/Desktop/WR/code/xxx/test/test_xxxx.cpp/
test_xxxx/build/CMakeFiles/gtest_xxxx.dir/ has 186 characters. 
  The maximum full path to an object file is 250 characters (see CMAKE_OBJECT_PATH_MAX). 
Object file C_/Users/xxx/Desktop/WR/code/xxx/test/test_xxx.cpp/test_xxxx/test_xxxx.cpp.obj  cannot be safely placed under this directory. The build may not work correctly.

可能是由于你的名字取的太长了,导致不能完整放下,后面在make时会出现错误

fatal error: opening dependency file CMakeFiles\gtest_xxx.dir\C_\Users\xxx\Desktop\WR\code\xxx\test\test_xxx.cpp\test_xxxx\test_xxxx.cpp.obj.d: No such file or directory 
   68 | }
      | ^
compilation terminated.

我将CMakeLists.txtadd_executabletarget_link_libraries中的名字修改短后再进行cmake与make,能够成功编译;

6、出现multiple definition of main错误

多次定义main函数导致的,如在CmakeLists.txt中调用了libgtest_main.a动态库,而在其他cpp文件中也出现了main函数,就会导致两次定义;
若想自己定义gtest的main函数,则在编写CMakeLists.txt时不引用libgtest_main.a动态库;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值