CLion, CMake, External Libraries
Greetings:
I'm something of a CMake and CLion newb, so bare with me. I'm attempting to get gtest up and running so that I can unit test my projects. To that end, I've downloaded googletest from github, built it externally to Clion via CMake and mingw (64bit), and setup a google-distribution directory, containing the includes and lib files. That all appears fine. I've then attempted to add the external library to my test project with the following modifications to the CMakeLists.txt:
cmake_minimum_required(VERSION 3.7)
project(arguments)
set(CMAKE_STATIC "-static-libgcc -static-libstdc++ -static")
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_STATIC}" )
link_directories("../googletest/google-distribution/lib")
include_directories("../googletest/google-distribution/include")
find_library(GTEST NAMES "gtest"
PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../googletest/google-distribution/lib NO_DEFAULT_PATH)
find_library(GTEST_MAIN NAMES "gtest_main"
PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../googletest/google-distribution/lib NO_DEFAULT_PATH)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp Tests/ClassName.h Tests/test.cpp)
add_executable(arguments ${SOURCE_FILES} Tests/ClassName.h Tests/test.cpp)
target_link_libraries(arguments gtest gtest_main)
#target_link_libraries(
# arguments
# "C:/Users\nimajneb.000/Dropbox/CS5310/googletest/google-distribution/lib/libgtest.dll.a"
# "C:/Users\nimajneb.000/Dropbox/CS5310/googletest/google-distribution/lib/libgtest_main.dll.")
The static linking is due to some path issues with the WinBuilds install. The shared DLLs are not being found when I attempt to exe the project binaries from the command line instead of running from Clion. That's a minor issue I understand. What I don't understand is why the linker can't seem to find the library files for gtest and gtest_main. Here's the output of a build attempt:
"C:\Program Files\JetBrains\CLion 2017.1.1\bin\cmake\bin\cmake.exe" --build C:\Users\nimajneb.000\Dropbox\CS5310\arguments\cmake-build-debug --target arguments -- -j 8
Scanning dependencies of target arguments
[ 66%] Building CXX object CMakeFiles/arguments.dir/main.cpp.obj
[ 66%] Building CXX object CMakeFiles/arguments.dir/Tests/test.cpp.obj
C:\Users\nimajneb.000\Dropbox\CS5310\arguments\main.cpp: In function 'int main(int, char**)':
C:\Users\nimajneb.000\Dropbox\CS5310\arguments\main.cpp:6:20: warning: ignoring return value of 'int RUN_ALL_TESTS()', declared with attribute warn_unused_result [-Wunused-result]
RUN_ALL_TESTS();
^
[100%] Linking CXX executable arguments.exe
f:/_tmp/winbuilds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgtest
f:/_tmp/winbuilds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgtest_main
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [arguments.exe] Error 1
CMakeFiles\arguments.dir\build.make:123: recipe for target 'arguments.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/arguments.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/arguments.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/arguments.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/arguments.dir/rule] Error 2
Makefile:117: recipe for target 'arguments' failed
mingw32-make.exe: *** [arguments] Error 2
I rebuild the CMakeCache, examined the link and include files in the CMake stage directory. The -L and -I flags and paths I expect to see are present. However, mingw doesn't believe in them. I even attempted to copy the headers and library dlls directly into the mingw install include and library folder. Nothing has effected the linking issue. At this point my googlefu is exhausted and I need help. Thanks in advance if you can tell me what I'm doing wrong here.
请先登录再写评论。
I think I may have gotten this. The issue is actually the "-static" flag. The make I did on googletest didn't build a static library. It only built dynamic libraries. Because of that the linked wasn't finding what it needed. Removing the flag allowed a successful build, albeit with the path issues mentioned before. I'll look into getting CMake to create static versions of the library.