所以我尝试使用googletest库(https://github.com/google/googletest)。首先我用cmake编译它:
marton@linux-clwa:~/documents/github/googletest/googletest/cmake> cmake ..
-- The CXX compiler identification is GNU 4.8.5
-- The C compiler identification is GNU 4.8.5
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Found PythonInterp: /usr/local/bin/python (found version "3.4.5")
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/marton/documents/github/googletest/googletest/cmak
e
marton@linux-clwa:~/documents/github/googletest/googletest/cmake> make
Scanning dependencies of target gtest
[ 25%] Building CXX object CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 50%] Linking CXX static library libgtest.a
[ 50%] Built target gtest
Scanning dependencies of target gtest_main
[ 75%] Building CXX object CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[100%] Linking CXX static library libgtest_main.a
[100%] Built target gtest_main
并尝试用g ++编译它,make:
Thu Mar 16; 22:45:19; marton;~/documents/github/fmi_summer_2017/chisleni_metodi ; $ g++ -isystem /home/marton/documents/github/googletest/googletest/include -L/home/marton/documents/github/googletest/googletest -pthread -lgthread 002.razd_razl.cpp -o test
/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/bin/ld: cannot find -lgthread
collect2: error: ld returned 1 exit status
我不确定这个错误是由于我的编译器/发行版,但是我已经安装了这些包:
gcc-32bit gcc48-32bit glibc-devel-32bit libasan0-32bit libatomic1-32bit libgomp1-32bit libitm1-32bit
所以我认为lgtest是问题而且作为一个脑力不足的编码器我决定删除它,结果是一个未定义的函数,名称空间和变量的大列表。
所以我尝试了这个回购:(https://github.com/snikulov/google-test-examples)。 一切都正常工作意味着问题不在于我的编译器。但是repo正在使用cmakelists。
我做错了什么,我该怎么做才能编译我的程序?
我知道法西斯极客将会投票但是我搜索了google,youtube,gtest repo并且没有任何解释如何通过g ++编译你的程序你的测试。那么我在哪里可以找到有关它的信息?
使用make或CMakeLists哪个更好?
答案 0 :(得分:0)
对于编译,如果您想使用CMakeLists.txt,通过add_subdirectory
添加到父级,您可以执行类似的操作。 (摘自Google测试文档)
在ProjectRoot/CMakeLists.txt
...
set(GTEST_OUTPUT_PATH ${CMAKE_BINARY_DIR}/googletest)
add_subdirectory(build_test)
...
在ProjectRoot/build_test/CMakeLists.txt
cmake_minimum_required(VERSION 2.8.11)
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in ${GTEST_OUTPUT_PATH}/download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${GTEST_OUTPUT_PATH}/download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${GTEST_OUTPUT_PATH}/download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${GTEST_OUTPUT_PATH}/src
${GTEST_OUTPUT_PATH}/build)
在ProjectRoot/build_test/CMakeLists.txt.in
cmake_minimum_required(VERSION 2.8.2)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${GTEST_OUTPUT_PATH}/src"
BINARY_DIR "${GTEST_OUTPUT_PATH}/build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
这使我们能够做的是从他们的repo中克隆一个新的GoogleTest副本,然后使用正确的命令参数构建它。 (然后你就可以像往常那样完成链接。)我建议你尝试设置它,以确保你在构建它时没有任何问题。 GTest的唯一要求是与c ++ 98兼容的编译器和make。您可以在此处查看完整(非常小)的要求:Linux Requirements