如何将谷歌测试库链接到CLion项目(Mac OS X El Capitan)



我正试图将我的项目与谷歌C++测试框架联系起来。我使用Mac OS X El Capitan,并在默认路径中安装了测试库。

lib:

/usr/local/lib/libgtest_main.a
/usr/local/lib/libgtest.a

include(用于标头):

/usr/local/include/gtest

我创建了一个新的CLion(2016.1.1)项目,这是CMakeList.txt,应该包括lib。

cmake_minimum_required(VERSION 3.5)
project(GoogleTest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(GoogleTest ${SOURCE_FILES})
target_link_libraries(GoogleTest gtest gtest_main)

这就是结果:

Scanning dependencies of target GoogleTest
[ 50%] Building CXX object CMakeFiles/GoogleTest.dir/main.cpp.o
[100%] Linking CXX executable GoogleTest
ld: library not found for -lgtest
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [GoogleTest] Error 1
make[1]: *** [CMakeFiles/GoogleTest.dir/all] Error 2
make: *** [all] Error 2

我该怎么解决这个问题?提前感谢

看起来/usr/local/lib不在编译器的库路径列表中。尝试在target_link_libraries中指定库的完整路径。

target_link_libraries(GoogleTest /usr/local/lib/libgtest.a /usr/local/lib/libgtest_main.a)

最新更新