samedi 15 décembre 2018

Undefined reference to main in gtest

I am trying to test my code using google test and for that I run the command make_runtests in /build directory of my workspace. I cross verified my CMakeLists.txt with similar projects and I feel it doesn't have any issues !

I have gone through these four questions and could not find the right solution : Q1, Q2, Q3 and Q4

I have two main() functions in the following locations :

1. TinyNomad / src / main.cpp

2. TinyNomad / test / tinynomadTest.cpp

The following is my folder structure:

enter image description here

This is my CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8.3)
project(tinynomad)

## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)

## Find catkin macros and libraries
find_package(catkin REQUIRED COMPONENTS
  image_transport
  message_generation
  move_base_msgs
  roscpp
  rospy
  sensor_msgs
  std_msgs
  message_runtime
  cv_bridge
)

###########
## Build ##
###########

find_package(OpenCV REQUIRED COMPONENTS core imgproc highgui opencv_imgcodecs)

## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(include ${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})

add_executable(tinynomad src/Navigation.cpp src/main.cpp src/Turtlebot.cpp src/Vision.cpp)

## Specify libraries to link a library or executable target against
target_link_libraries(tinynomad ${catkin_LIBRARIES} ${OpenCV_LIBS})

###########
# Testing #
###########

if(CATKIN_ENABLE_TESTING)
  find_package(rostest REQUIRED)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 --coverage")
  add_rostest_gtest(tnomadTest test/tinynomadTest.launch test/NavigationTest.cpp 
                    test/VisionTest.cpp test/TurtlebotTest.cpp src/Navigation.cpp  
                    src/Vision.cpp src/Turtlebot.cpp)
  target_link_libraries(tnomadTest ${catkin_LIBRARIES} ${GTEST_LIBRARIES} pthread)
  add_dependencies(tnomadTest ${catkin_EXPORTED_TARGETS})
  set_target_properties(tnomadTest PROPERTIES LINKER_LANGUAGE CXX)
endif()

This is the error message:

[ 22%] Built target sensor_msgs_generate_messages_cpp
[ 22%] Built target sensor_msgs_generate_messages_eus
[ 33%] Linking CXX executable tnomadTest
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
TinyNomad/CMakeFiles/tnomadTest.dir/build.make:259: recipe for target 'TinyNomad/tnomadTest' failed
make[3]: *** [TinyNomad/tnomadTest] Error 1
CMakeFiles/Makefile2:967: recipe for target 'TinyNomad/CMakeFiles/tnomadTest.dir/all' failed
make[2]: *** [TinyNomad/CMakeFiles/tnomadTest.dir/all] Error 2
CMakeFiles/Makefile2:106: recipe for target 'CMakeFiles/run_tests.dir/rule' failed
make[1]: *** [CMakeFiles/run_tests.dir/rule] Error 2
Makefile:186: recipe for target 'run_tests' failed
make: *** [run_tests] Error 2

Aucun commentaire:

Enregistrer un commentaire