2017-03-16 9 views
0

所以我試着用googletest庫(https://github.com/google/googletest)。首先,我用cmake的編譯它:在linux上編譯googletest程序openSUSE with g ++,make,cmakelists

[email protected]:~/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 
[email protected]:~/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 ++編譯它,使:

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-32位gcc48-32bit的glibc-devel的,32位libasan0-32bit libatomic1-32bit libgomp1-32bit libitm1-32bit

所以我認爲lgtest是這個問題和作爲糟糕的編碼器我決定刪除它,結果是一個未定義函數,命名空間和變量的大名單。

所以我試過這個回購:(https://github.com/snikulov/google-test-examples)。 一切工作正常,這意味着問題不在我的編譯器,但回購是使用cmakelists。

我在做什麼錯,我需要做些什麼來編譯我的程序?

我知道法西斯怪人會倒下來,但我搜索谷歌,YouTube,gtest回購,並沒有任何解釋如何通過g ++編譯你的程序你的測試的解釋。那我在哪裏可以找到關於這方面的信息?

使用make或CMakeLists更好嗎?

+0

您在哪裏找到用於構建測試的命令行?爲什麼你需要'gthread'(GLib線程)庫來進行測試? – Tsyvarev

+0

如果通過gthread你的意思是-lgthread,我不確定它是否需要,但如果我嘗試編譯我的程序沒有它,我的終端得到充滿錯誤 – Hartun

回答

0

對於編譯,如果你想使用CMakeLists.txt,通過add_subdirectory添加到父類,你可以這樣做。 (從谷歌測試資料爲準)

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  "" 
) 

這是什麼使我們能夠做的是克隆一個新的副本GoogleTest從他們的回購,和然後用正確的命令參數構建它。 (你可以在完成後像往常一樣鏈接到它)。我建議嘗試設置它以確保在構建它的過程中沒有任何問題。 GTest的唯一要求是與C++ 98兼容的編譯器和make。您可以在此處看到完整(非常小)的要求:Linux Requirements

+0

我現在會嘗試,問題是我不是完全熟悉這項技術,如果有的話,我不能修改它......所以有可能在將來我不得不修改它嗎?我該如何編譯這個cmakelist? – Hartun

+0

你當然可以修改它以適應你的需求。要構建它,你可以在你的項目根目錄下運行'cmake .',然後在完成後運行'make'。 –