While studying the official ROS tutorial Writing a Simple Publisher and Subscriber (C++), the following error occurred when running catkin_make:
Base path: /home/free/catkin_ws
Source space: /home/free/catkin_ws/src
Build space: /home/free/catkin_ws/build
Devel space: /home/free/catkin_ws/devel
Install space: /home/free/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/free/catkin_ws/build"
####
####
#### Running command: "make -j12 -l12" in "/home/free/catkin_ws/build"
####
[ 0%] Built target std_msgs_generate_messages_lisp
[ 0%] Built target std_msgs_generate_messages_nodejs
[ 0%] Built target std_msgs_generate_messages_cpp
[ 0%] Built target std_msgs_generate_messages_py
[ 0%] Built target std_msgs_generate_messages_eus
[ 0%] Built target _beginner_tutorials_generate_messages_check_deps_Num
[ 0%] Built target _beginner_tutorials_generate_messages_check_deps_AddTwoInts
[ 58%] Built target beginner_tutorials_generate_messages_py
[ 64%] Built target beginner_tutorials_generate_messages_eus
[ 58%] Built target beginner_tutorials_generate_messages_lisp
[ 58%] Built target beginner_tutorials_generate_messages_cpp
[ 76%] Built target beginner_tutorials_generate_messages_nodejs
[ 76%] Built target beginner_tutorials_generate_messages
[ 82%] Linking CXX executable /home/free/catkin_ws/devel/lib/beginner_tutorials/talker
[ 88%] Linking CXX executable /home/free/catkin_ws/devel/lib/beginner_tutorials/listener
/usr/bin/ld: CMakeFiles/listener.dir/src/listener.cpp.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: Final link failure: output non-representable links
collect2: error: ld returned 1 exit status
beginner_tutorials/CMakeFiles/listener.dir/build.make:112: recipe for target '/home/free/catkin_ws/devel/lib/beginner_tutorials/listener' failed
make[2]: *** [/home/free/catkin_ws/devel/lib/beginner_tutorials/listener] Error 1
CMakeFiles/Makefile2:1344: recipe for target 'beginner_tutorials/CMakeFiles/listener.dir/all' failed
make[1]: *** [beginner_tutorials/CMakeFiles/listener.dir/all] Error 2
make[1]: *** Waiting for unfinished tasks....
/usr/bin/ld: CMakeFiles/talker.dir/src/talker.cpp.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: Final link failure: output non-representable links
collect2: error: ld returned 1 exit status
beginner_tutorials/CMakeFiles/talker.dir/build.make:112: recipe for target '/home/free/catkin_ws/devel/lib/beginner_tutorials/talker' failed
make[2]: *** [/home/free/catkin_ws/devel/lib/beginner_tutorials/talker] Error 1
CMakeFiles/Makefile2:1381: recipe for target 'beginner_tutorials/CMakeFiles/talker.dir/all' failed
make[1]: *** [beginner_tutorials/CMakeFiles/talker.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j12 -l12" failed
After looking for the reason for a long time, I finally found that the final error was due to the wrong version of GCC and G + +. After changing GCC and g++ 5.5.0, I successfully compiled
Process for installing GCC and g++ version 5.5.0:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-5
sudo apt-get install g++-5
Change GCC and g++ versions
cd /usr/bin
sudo rm gcc
sudo ln -s gcc-5 gcc
sudo rm g++
sudo ln -s g++-5 g++
Finally, check whether the modification is successful
gcc --version
g++ --version
Recompile:
catkin_make clean #Clear the front compile
catkin_make