andreapavan andreapavan - 1 year ago 124
C++ Question

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

I'm trying to link my project with google C++ testing framework. I use Mac OS X El Capitan and I have installed test library in the default path.



(for headers):


I created a new CLion (2016.1.1) project a this is the
which should include the lib.

cmake_minimum_required(VERSION 3.5)

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)

This is the result:

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

How can I fix this? Thanks in advance

Answer Source

It looks like /usr/local/lib is not in the compiler's library path list. Try specifying the full path for the libraries in target_link_libraries.

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