mardi 29 janvier 2019

How to run google Test from command line with --std=c++11 flag enabled

I installed gtest development package in ubuntu as follow:

  sudo apt-get install libgtest-dev
  sudo apt-get install cmake # install cmake
  cd /usr/src/gtest
  sudo cmake CMakeLists.txt
  sudo make
  # copy or symlink libgtest.a and libgtest_main.a to your /usr/lib folder
  sudo cp *.a /usr/lib

In order to compile and run the tests:

ubuntu@XPS:/hm/an/folder$ cmake CMakeLists.txt
-- The C compiler identification is GNU 5.5.0
-- The CXX compiler identification is GNU 5.5.0
-- 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
-- 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
-- Found GTest: /usr/lib/libgtest.a  
-- Looking for pthread.h
-- Looking for 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: /hm/an/folder/

After which I run make

Scanning dependencies of target runTests
[ 50%] Building CXX object CMakeFiles/runTests.dir/test.cpp.o
In file included from /hm/an/folder/test.cpp:1:0:
/hm/an/folder/myfunc.cpp: In function ‘std::__cxx11::string test(std::__cxx11::string)’:

The content of my CMakeLists.txt:

cmake_minimum_required(VERSION 3.5.1)

# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests test.cpp)
target_link_libraries(runTests ${GTEST_LIBRARIES}  pthread)

I understood that the problem that, while compiling the compiler isn't able to recognize the std::string. Could anyone help me to fix the issue? So it's looking for --std=c++17 flag!

I can run my program on the command line using: g++ --std=c++17 myfunc.cpp -o main, but not my test case which I have written using gtest. Any suggestions on what I am doing wrong?

Aucun commentaire:

Enregistrer un commentaire