如何 link Google 测试库到 CLion 项目(Mac OS X El Capitan)

How to link Google Test library to CLion project (Mac OS X El Capitan)

我正在尝试 link 我的项目使用 google C++ 测试框架。我使用 Mac OS X El Capitan 并在默认路径中安装了测试库。

lib:

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

include(对于headers):

/usr/local/include/gtest

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

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)