In addition to using add_executable to generate executable files, you can also use add_library to generate link libraries.

Link libraries for Linux

Let’s start with the Linux link library, which is the binary form of some function code, both dynamic and static, with dynamic usually using the.so suffix and static usually using the.a suffix.

Static link library. A: the file is named libxxx.a with “lib” before the library name and “. A “after the library name. Static libraries are bulky because their code has already been loaded into the executable during compilation. When used, the linker finds the functions the program needs and copies them to the executable file. Since this copy is complete, once the connection is made, the static library is no longer needed.

Dynamic link library (shared library) : file name: libxxx.so, prefix the library name with lib, suffix is. So, and the library name is XXX. Dynamic library code is loaded into memory while the executable is running, and is simply referenced during compilation, so the code is small. When a program calls a dynamically linked library function during execution, the operating system first looks at all running programs to see if there is a copy of the library function in memory. If so, let it share that copy. Link loads only if there is none. While the program is running, the called dynamically linked library function is placed somewhere in memory, and all programs calling it will point to this code segment. Therefore, the code must use relative addresses, not absolute addresses. At compile time, we need to tell the compiler that these object files are used for dynamically linked libraries, so Position Independent Code (PIC) is used.

There are two loading modes of dynamic link library: implicit loading and display loading.

Note: The default for connecting under Linux is to connect to the dynamic library first, that is, to connect to the dynamic library if both static and dynamic libraries exist, unless specified.

Create a link library

add_library(<name> [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] [source1] [source2 ...] )Copy the code
  • Name Specifies the name of the link library to be created, which must be globally unique in the project.
  • STATIC, SHARED, or MODULE can specify the type of library to create. STATIC stands for STATIC linked library. SHARED stands for dynamic linked library. The MODULE library is a plug-in that is not linked to another target, but can be loaded dynamically at run time using dlopen-like functionality.
  • EXCLUDE_FROM_ALL If given, sets the corresponding property on the created target. Please refer to the EXCLUDE_FROM_ALL
  • Source The source file on which the link library is created.

Three, an example:

hello.c

#include <stdio.h>

int hello(a)
{
    printf("hello CMake! \n");

    return 0;
}
Copy the code

CMakeLists.txt

# cmake_MINIMum_required (VERSION 3.5) # cmake_MINIMum_required (VERSION 3.5) # Test_2 STATIC hello.c (test_2 STATIC hello.c);Copy the code

Result after compilation :(the compilation method is the sameCMake learning (a) – the simplest cmakelists.txt)

Libtest_2.a is the generated statically linked library.

Reference: www.cnblogs.com/inrgihc/art… Blog.csdn.net/sinat_34684…