andreapavan andreapavan - 3 months ago 72
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.

lib
:

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


include
(for headers):

/usr/local/include/gtest


I created a new CLion (2016.1.1) project a this is the
CMakeList.txt
which should include the 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)


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

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)