Most of the time C/C++ files are compiled directly using commands like GCC main. C or g++ main.cpp. But with more code files, it becomes difficult to compile. This is where the MakeFile tool comes in.

Makefiles solve the problem of compiling multiple files. With makefiles, you just need to run a make command in the directory of the MakeFile, and the compilation is done automatically. But writing makefiles is tedious, so clever programmers have developed a tool that automatically generates makefiles. Cmake does just that. After all, writing cmake is much easier than writing makefiles

You might say, nowadays, all IDE code, IDE will automatically compile and run, I still learn these things. But there are times when you need to compile projects on the server, or on a machine that doesn’t have an IDE, and makefiles can be useful. In fact, many ides are compiled using Cmake, such as Clion

Cmake installation

Ubuntu and Debian sudo apt-get install cmake Ubuntu and Debian sudo apt-get install cmake

CentOS

sudo yum install cmake

Since cmake only generates the MakeFile, and ultimately organizes the compilation of the MakeFile, you also install make

Make is usually pre-installed, just to be safe

Ubuntu and Debian sudo apt-get install make

CentOS

sudo yum install make

Cmake basic rules

There is no mystery to cmake. It also automatically generates makefiles according to certain rules and has a syntax

# is a comment symbol

Predefined variable

PROJECT_NAME Project name PROJECT_SOURCE_DIR Project root directory PROJECT_BINARY_DIR Directory where the cmake command is executed PROJECT_BINARY_DIR Directory where the cmake command is executed CMAKE_CURRENT_SOURCE_DIR Directory where the current cmakelists. TXT file resides CMAKE_C_FLAGS Sets C compilation options CMAKE_CXX_FLAGS Sets C++ compilation options CMAKE_C_COMPILER Sets the C compiler CMAKE_CXX_COMPILER sets the C++ compiler EXECUTABLE_OUTPUT_PATH sets the directory of executable files after compilation. LIBRARY_OUTPUT_PATH sets the directory of library files to be generated

Common rules

The cmake_minimum_required(VERSION 3.16) directive cmake VERSION project(hello_world) sets the project name Include_directories (${PROJECT_SOURCE_DIR}/include) Add a header path. Link_directories (${PROJECT_SOURCE_DIR}/lib) Add a link library path Add_subdirectory (module) Adds the module subdirectory, Add_executable (project1 main.c) specifies the executable file to compile. Add_library (lib1 SHARED library.c library.h) specifies the generated library file. Add_compile_options () add the compiler option target_link_libraries() specify the dynamic link library install() specify the directory for make install

Set (XXXX YYYYYY) is used to set and modify the variable ${XXXX}

Build a simple project

There is only one main.c file

CMakeList.txt

Cmake_minimum_required (VERSION 3.15) project(project1 C) set(CMAKE_C_STANDARD 99) add_executable(project1 main.c)Copy the code

main.c

#include <stdio.h>

int main(a) {
    printf("Hello, CMakeList! \n");
    return 0;
}
Copy the code
Build a Debug version

  1. mkdir debugCreating the Debug directory
  2. cd debugGo to the debug directory
  3. cmake -DCMAKE_BUILD_TYPR=debug ..Set the compile mode to DEBUG
  4. makeGenerate an executable file

The project1 file is generated

Cmake-dcmake_build_type =release.. Specify compile mode as release

Build a project that generates a dynamic library

There are two files library.h and library.c cmakelist.txt

cmake_minimum_required(VERSION 3.15)
project(shared C)

set(CMAKE_C_STANDARD 99)

add_library(shared SHARED library.c library.h)
Copy the code

library.h

int add(int a, int b);
Copy the code

library.c

int add(int a, int b) {
    return a + b;
}
Copy the code
Build a dynamic library
  1. mkdir libCreating a lib directory
  2. cd libGo to the lib directory
  3. cmake -DCMAKE_BUILD_TYPE=debug ..Set the compile mode to DEBUG
  4. makeGenerate an executable file

The libshared.so file is generated

Use dynamic libraries in the first project

Start by copying the libshared.so and library.h files into the first project

Modify CMakeList. TXT

cmake_minimum_required(VERSION 3.15)
project(project1 C)

set(CMAKE_C_STANDARD 99)

add_executable(project1 main.c)

target_link_libraries(project1 ${PROJECT_SOURCE_DIR}/libshared.so)# specify the dynamic library file
Copy the code
  1. mkdir debugCreating the Debug directory
  2. cd debugGo to the debug directory
  3. cmake -DCMAKE_BUILD_TYPE=debug ..Set the compile mode to DEBUG
  4. makeGenerate an executable file

The final directory is shown below

Combine the two items into one

Is it possible to generate a dynamic library in a project and use it in that project? Yes, of course.

In this project, some files are compiled into dynamic library.so files and some files are compiled into executable files

Modify CMakeList. TXT

cmake_minimum_required(VERSION 3.13.3)
project(project1 C)

set(CMAKE_C_STANDARD 99)

add_library(shared SHARED library.h library.c)

set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)Set the dynamic library output directory

add_executable(project1 main.c)

target_link_libraries(project1 shared)
Copy the code
  1. mkdir debugCreating the Debug directory
  2. cd debugGo to the debug directory
  3. cmake -DCMAKE_BUILD_TYPE=debug ..Set the compile mode to DEBUG
  4. makeGenerate an executable file

The final directory is shown below

The last

Through a few simple examples, introduced the basic use of cmake, the article is the use of Cmake build C project, into C++ basic no difference, will not show

Cmake features far more than these, but also need to learn more in the actual combat, more summary

So files cannot be changed to the directory. Once the dynamic library directory is changed, the program cannot be loaded and will not run. So I picked up some materials and wrote another one, Portal