CMake internal build vs external build

CMake internal build vs external build

1. Internal construction

First, we use CMake Tool to create a simple project for us. Of course, we can also create it manually. The general directory structure is a main.cpp and a CMakeLists.txt under the project directory

If using an internal build, we execute cmake in the directory where the top-level CMakeLists.txt is located and see what happens.

cmake .

20221231142043

After execution, you will find a pair of messy files in the current project directory. If there are many source code files in your original project, can you still find them now? Can you tell what is useful and what is not?

20221231142117

This is the case with internal builds, which generate some intermediate files that cannot be automatically removed, so internal builds should be avoided as much as possible.

2. External construction

  1. First, we restore the project directory to its original state and create a build subdirectory in it. Of course, you can also create a build directory anywhere else, not necessarily in the project.

    20221231142043

  2. Enter the build directory and run

     cmake ..
    

    Note that ... represents the parent directory, because the parent directory has the CMakeLists.txt we need, if you have established a build directory elsewhere, you need to run cmake <path_to_project>, check the build directory, you will find that the compilation required MakeFile and other intermediate files.

    20221231143006

  3. Run make to build the project in the build directory, and you will get the target file in the current directory, that is, build. The file name is the project name you specified in CMakeLists.txt.
    20221231143424

The above process is the so-called out-of-source external construction. One of the biggest advantages is that it has no impact on the original project, and all actions take place in the compilation directory. Through this, it is enough to convince us to use external compilation to build the project.

Guess you like

Origin blog.csdn.net/hubing_hust/article/details/128505399