Recently, i have met the make, cmak and makefile during learning the ROS (Robot Operating System). They make me dizzy. There are some idea to help you remeber them.
-
gcc
GNU Compiler Collection (gcc) is considered as a kind of editor. It can compile so many kinds of programming languges including C, C++, Objective-C, Fortran, Java and so on. If we only have one source file, we can compile it with gcc directly.
However, if we have many source files needed to be compiled. You will find that this work are both chaoic and heavy. what should we do ?
The make emerge as the times require. -
make
The make is regarded as a smart batch processing tool and it cannot compile and link. However, it can compile and link indirectly by invoking orders set by the user in the makefile file.
What is makefile file ? -
makefile
if make is the chef , the makefile is the recipe. the chef cooks the dishes according to the recipes, while the make compiles depend on the makefile. the orders including invode the gcc (or other compiler) to compile some source files.
When the workspace is simple, user can write it by hand, but it is hard to writer the makefile files by hand. What is more, it is needed to be modified in different platform. A new and powerful tool was born in the proper conditions. -
cmake
The cmake can create a makefile file to be used by the make file. Of course, it is so powerful that it can be used on different platforms without modifying. However, how to generate the makefile ? the CMakeLists.txt needed -
CMakeList.txt (package.xml) [extension for robot operating system (ROS)]
the compiler is the CMake in ROS, and the rules of compiling will be set in the CMakeLists,txt in the package. the file of CMakeLists.txt will generated in the process of the package is created by catkin order. We do not need to check the relevant instruction manuals and compile our own code with slight modifications.
When you open the CMakeLists,txt file in the function package, find the following configuration items, remove the comments and modify them slightly:
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker ${PROJECT_NAME}_generate_messages_cpp)
add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(talker ${PROJECT_NAME}_generate_messages_cpp)
-
A. include_directories
-
B. add_executable
used for setting compiling codes and executable file. the first parameter “talker” is the expected executable name that you want to get. The parameters “src/talker.cpp” is the source file that need to be compiled. If many source files needed to be compiled, list them one after another separated by space. -
C. target_link_libraries
Some functions need to use the system and the third party library files, You can link to the them by this way. the first parameter is the expected executable name that you want to get. behind it is the link libraries. -
D. add_dependencies
set the dependencies. some message type needed to be defined, the codes will generated during compiling the message type. If the executable file need the codes to run, the “add_dependencies” need to be used.
- The overall process is:
- 1. write the source codes, for example .c file.
- 2. compile and generate the object file, like .o file.
- 3. generate the excutable files using the linker to link the object file, like .exe file.
If there are many source files, to compile them one by one, it is so heavy. so person design a kind of batch-processing program to compile the source files. it is the make tool.
however, the rule file that the make need to obey is needed, so the programmer have to write the makefile file.
For large project, to get the makefile is not eassy, so people begain to think and design another tool that can read all of the source files and generate makfile files automatic. So the cmake tool appeares that can output all kinds of makefile files and project files to help programmer to reduce the burden.
But the question is that the CMakeList.txt file needed to be writed due to it is the rules that the camke will obey.
You will find that when you solve the old problem, while the new question appears. it is the main cycle of life and science. the socity and techs will develop during this recycling.
1.undersatand the make/cmake/makefile/CMakeList in 5 seconds
2. the CMakeList.txt and package.xml in ROS
3. add_link_libraries/add_executable