Exercise 4.1 issue

/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

I keep getting this error when compiling, what could be this issue?

Hello @mfalm ,

It looks like you are doing a reference to main in your code, but main has not been defined. Could you share the code you are trying to compile here?

1 Like

Adding to what Alberto mentioned, you probably do not have a function named main in your code.

All programs in C++ need to have the main function.
Example.

int main(){

     
     // your_function_or_code_here(); 
  
    return 0;
}

Hi Alberto,

This is the code I am using to compile. The main() function is there but not sure the issue.

#include "rosbot_control/rosbot_class.h"
#include <ros/ros.h>


int main(int argc, char **argv) {
  ros::init(argc, argv, "rosbot_node");

  RosbotClass rosbot;

  rosbot.move_forward(3);

  // Get readings
  float right = rosbot.get_laser(121);
  float left = rosbot.get_laser(431);
  float front = rosbot.get_laser(719);
  
  // Define and assign array values
  float array_laser[] = {left,right,front};
  // Print array
  ROS_INFO_STREAM("The wall is at " << array_laser[0] << " meters to the left, and at " << array_laser[1] << " meters to the right from the robot, and at " << array_laser[2] << " meters to the front from the robot. \n");
  
    
    
  return 0;
}

Hi @mfalm,

I just pasted your code on the ~/catkin_ws/src/cpp_course_repo/c_scripts/src/unit4_exercise.cpp and compiled with cd ~/catkin_ws; catkin_make, and everything compiled as expected as we can see below :thinking::

user:~/catkin_ws$ catkin_make
Base path: /home/user/catkin_ws
Source space: /home/user/catkin_ws/src
Build space: /home/user/catkin_ws/build
Devel space: /home/user/catkin_ws/devel
Install space: /home/user/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/user/catkin_ws/build"
####
####
#### Running command: "make -j8 -l8" in "/home/user/catkin_ws/build"
####
[  0%] Built target geometry_msgs_generate_messages_py
[  0%] Built target actionlib_msgs_generate_messages_eus
[  0%] Built target nav_msgs_generate_messages_py
[  0%] Built target nav_msgs_generate_messages_nodejs
[  0%] Built target nav_msgs_generate_messages_lisp
[  0%] Built target roscpp_generate_messages_py
[  0%] Built target rosgraph_msgs_generate_messages_eus
[  0%] Built target actionlib_msgs_generate_messages_py
[  0%] Built target geometry_msgs_generate_messages_eus
[  0%] Built target roscpp_generate_messages_cpp
[  0%] Built target rosgraph_msgs_generate_messages_cpp
[  0%] Built target std_msgs_generate_messages_cpp
[  0%] Built target sensor_msgs_generate_messages_nodejs
[  0%] Built target geometry_msgs_generate_messages_cpp
[  0%] Built target roscpp_generate_messages_eus
[  0%] Built target rosgraph_msgs_generate_messages_py
[  0%] Built target rosgraph_msgs_generate_messages_lisp
[  0%] Built target roscpp_generate_messages_nodejs
[  0%] Built target actionlib_msgs_generate_messages_cpp
[  0%] Built target rosgraph_msgs_generate_messages_nodejs
[  0%] Built target nav_msgs_generate_messages_cpp
[  0%] Built target std_msgs_generate_messages_nodejs
[  0%] Built target roscpp_generate_messages_lisp
[  0%] Built target std_msgs_generate_messages_eus
[  0%] Built target std_msgs_generate_messages_py
[  0%] Built target nav_msgs_generate_messages_eus
[  0%] Built target geometry_msgs_generate_messages_lisp
[  0%] Built target std_msgs_generate_messages_lisp
[  0%] Built target geometry_msgs_generate_messages_nodejs
[ 12%] Built target rosbot_control
[ 12%] Built target actionlib_msgs_generate_messages_lisp
[ 25%] Built target unit2_exercise
[ 37%] Built target unit5_exercise
[ 50%] Built target unit1_exercise
[ 62%] Built target unit6_exercise
Scanning dependencies of target unit4_exercise
[ 68%] Building CXX object cpp_course_repo/c_scripts/CMakeFiles/unit4_exercise.dir/src/unit4_exercise.cpp.o
[ 75%] Linking CXX executable /home/user/catkin_ws/devel/lib/c_scripts/unit4_exercise
[ 75%] Built target unit4_exercise
[ 87%] Built target unit3_exercise
[ 87%] Built target actionlib_msgs_generate_messages_nodejs
[ 87%] Built target sensor_msgs_generate_messages_cpp
[ 87%] Built target sensor_msgs_generate_messages_eus
[ 87%] Built target sensor_msgs_generate_messages_lisp
[ 87%] Built target sensor_msgs_generate_messages_py
[100%] Built target rosbot_class

If it really doesn’t work for you, I would recommend removing the build and devel folders and recompiling it with the following commands:

cd ~/catkin_ws/
rm build/ devel/ -rf

catkin_make

source devel/setup.bash

Or, could it be that your package is outside of the catkin_ws/src/ folder? Packages should be ideally inside that catkin_ws/src folder.
Along the course, we are told to clone the cpp_course_repo repository inside the catkin_ws/src folder. You can check its location using the command below:

find ~/ -name cpp_course_repo

That command will tell us where the repository is located.