[CMake] CMake builds C++ code (1)

In the Linux development process, it is inevitable that CMake will be used to build your code. This article will explain how to build your own code and turn your own code into a shared library for use by other code.


(Follow "Test Development Automation" Gong Zhonghao for more learning content)

Brief introduction to CMake

If a friend uses C/C++/Fortran/Java to develop on the Linux side, I believe that he is familiar with the CMake construction tool, and it is also a code construction skill that most programmers must master.
The reason why Cmake will enter the sight of most programmers and be used by most programmers is mainly because CMake has the following characteristics:

  1. Native support for automatic dependency analysis of C/C++/Fortran/Java, eliminating the need for programmers to adjust code dependencies, which is of great help to the entire development work.
  2. Support SWIG, Qt, FLTK development framework.
  3. Support cross-platform compilation, which is the source of CMake's name.
  4. CMake requires users to write CMake scripts with the syntax of the CMake specification, which is easy to use and easy to get started with.
  5. Ability to convert IDE project files for specific platforms, such as xcode.
  6. Integrate with Dart, CTest and CPack to form an automated build system.

It should be noted that: to use CMake, programmers must write the CMake script CMAkeLists.txt. For some complex projects, it may be necessary to write CMake modules, but for the construction process, it is extremely simple.


1. Build your first CMake project

First of all, if you know, a standard project, the folder structure should be like this:

  1. doc folder: store project documents
  2. src folder: store source files
  3. bin folder: store the built target files

Well, let's build a simple project using CMake to build! , the premise of our construction is all examples under the Linux system. Therefore, when you study, it is best to study under the Linux system.

2.1 Engineering structure

We create a folder under the home path of our own Linux system (or your own system), and create corresponding files under the corresponding folder. The structure of the entire t3 folder is as follows:

 t3
├── build
├── CMakeLists.txt
└── lib
       ├── CMakeLists.txt
       ├── hello.cpp
       └── hello.h

2.2 File content

An empty file was created above, and we will add the contents of the file below:

  1. t3/CMakeLists.txt file:

project(hellolib) # Project name
add_subdirectory(lib) # Generate a lib folder in the project directory (build folder), and store dynamic and static files in the build/lib folder

  1. t3/lib/CMakeLists.txt file:

set(LIBHELLO_SRC hello.cpp)
add_library(hello SHARED ${LIBHELLO_SRC}) # Generate a dynamic library file of hello.so
add_library(hello_static STATIC ${LIBHELLO_SRC}) # Generate a static library file of hello_static.o
#To generate The name of the dynamic library and the static library are the same, you need to use set_target_properties
set_target_properties(hello_static PROPERTIES OUTPUT_NAME "hello") #Rename hello_static to hello# You can get libhello.so and libhello.a
set_target_properties(hello PROPERTIES VERSION 1.2 SOVERSION 1) #Set the generated The version of .so is 1.2
INSTALL(TARGETS hello hello_static # Install the dynamic library libhello.so and libsello.a into /usr/lib
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
INSTALL(FILES hello.h DESTINATION include/hello) #Install the header The file is installed into /usr/include/hello

  1. hello.cpp file:
#include <iostream>
void HelloFunc()
{
    
    
    std::cout << "hello world"<< std::endl;
}
  1. hello.h file:
#ifndef HELLO_H
#define HELLO_H
#include <iostream>
void HelloFunc();
#endif

3. Start building

What is mainly used here is the way of external construction. The external build is to generate the build information in the build folder, which is also highly recommended, so the internal build will not be explained too much here.
Enter the following Linux command in the terminal:

cd build
cmake .. && make

4. Build folder structure after construction

After building, the build folder structure looks like this:


build
├── CMakeCache.txt
├── CMakeFiles
├── cmake_install.cmake
├── Makefile
└── lib
       ├── CMakeFiles
       ├── cmake_install.cmake
       ├── Makefile
       ├── libhello.a        # set_target_properties(hello_static PROPERTIES OUTPUT_NAME "hello") 
       ├── libhello.so       # set_target_properties(hello_static PROPERTIES OUTPUT_NAME "hello") 
       ├── libhello.so.1     # set_target_properties(hello PROPERTIES VERSION 1.2 SOVERSION 1)
       └── libhello.so.1.2   # set_target_properties(hello PROPERTIES VERSION 1.2 SOVERSION 1)

5. Install the shared library

If you want others to be able to call your code directly only through the .h file, then you need to install the shared library. The function is: install the header files and shared libraries to the system directories /usr/lib and /usr/include/hello, which can be used directly by including <hello.h> (equivalent to Windows environment variables). Enter in the build folder:

make install

6. Summary

So far, we have completed the construction of our shared library, and we will explain how to use the shared library to run our code later.

(Follow "Test Development Automation" Gong Zhonghao for more learning content)

insert image description here

Guess you like

Origin blog.csdn.net/weixin_44244190/article/details/129066808