How does the compilor know where a header file's corresponding source code is? C++

Hi, I am having difficulty understanding a couple of concepts in C++ from the C++ course.

Question 1a:

How does the compiler know where the corresponding source code for a header file is?

In unit5_exercise.cpp file:
image

we are telling the compilor that we are using the header file rosbot_class.h from the package rosbot_control. Now we are not mentioning the location of the corresponding source code for the header file, then, how does the compiler locate it?

Question 1b)
Is there a file structure we need to follow to create a header file and its corresponding source file, taking the rosbot_class as an example. (does header file always need to be in a folder called include
and does source file also need to in a folder called src.

image

Question 2:
In rosbot_class.cpp, which is the source file for the rosbot_class.h header file, we have a main() function as below. Here the main function is asking the robot to move using the rosbot.move() function.

#include "rosbot_control/rosbot_class.h"
.
.
.
int main(int argc, char **argv) {
  ros::init(argc, argv, "rosbot_class_node");

  RosbotClass rosbot;

  rosbot.move();

  float coordinate = rosbot.get_position(1);

  ROS_INFO_STREAM(coordinate);

  return 0;
}

The unit5_exercise.cpp file also has a main function, that tells it to move using function trajectory() (user defined)

#include "rosbot_control/rosbot_class.h"
.
.
.

int main(int argc, char **argv) {
  ros::init(argc, argv, "Rosbot_move_node");
    
  // CALL YOUR CLASS HERE
Avoid_WALL avoid_wall_obj;
avoid_wall_obj.trajectory();

}

Since we are using rosbot_class.cpp source code in unit5_exercise.cpp, does the robot respond to both the main() functions?

Feedback to @staff:
Though I figured most of it from google, It would have been great if the following concepts were explained in the course itself. :

  • How Pre-processor work

  • How compilation works

  • How Linker works

  • What is an Object file

  • Difference between <>and “” for #include files

While answering i would really appreciate it if you could quote the question using image from above toolbar , making it easier to comprehend.

Thanks in advance.
-Joseph

Q1a: <> says that load the files from the system folder specified when installed.
/opt/ros/noetic/include/ros/ros.h
" " from your folder specified in CMakeLists.txt, that said, your package

Q1b: Usually is a good idea to add folders related to the thing that they do, look at the ros folder and you will see opt/ros/noetic/include

Q2a: You use the instantiation of a class so you will use the class RosbotClass structure and functions. You should take attention which class you are calling. Is the same as in python if you think just C++ is more complex and powerfull.

Thanks @issaiass for replying.

Question1:

Usually in ROS, we place messages in a folder called Msg, script in a folder called src and launch in a file called launch. Here we are using header files in inlcude folder and source files in src folder. Is this done so that its easy for the user to understand or is this done because the compilor expects certain files under certain folder names?



Question 2:

Is it safe to say that the main function is called only when we call the file containing it directly, similar to how if name == “main” is used in python?

Not sure, i didn’t see this in c or c++, usually there is only 1 main function.
But if you want you can implement a main function inside your class for sure, but it is not usually done.

class MyClass{
public:
     int main(){ return 0; };
};


int main(int argc, char *argv[]){
    MyClass containing;

   containing.main();
   return 0;
}
1 Like

Can you tell me about this also?

Hi @Joseph1001,

yes, the “main” function is the one called automatically when a C++ code is compiled.

You can create other functions, with different names, for example, with some print statements, so that you can confirm that “main” is the one called.

It is exactly analogous to “if __name__ == __main__” in python.

1 Like