其他分享
首页 > 其他分享> > cmake构建和链接静态库与动态库

cmake构建和链接静态库与动态库

作者:互联网

目录

源码

..
├── 3rd
│   ├── math
│   │   ├── CMakeLists.txt
│   │   ├── math_add.c
│   │   ├── math.h
│   │   └── math_sub.c
│   └── print
│       ├── CMakeLists.txt
│       ├── print.c
│       └── print.h
├── build
├── CMakeLists.txt
└── src
    ├── CMakeLists.txt
    └── main.c

5 directories, 10 files

# /3rd/math/CMakeLists.txt
aux_source_directory(./ LIST_MATH)
add_library(lib_math OBJECT ${LIST_MATH})
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
// ./3rd/math/math.h
#if !defined(__MATH_H__)
#define __MATH_H__

int math_add(int a, int b);
int math_sub(int a, int b);

#endif // __MATH_H__

// ./3rd/math/math_sub.c
#include "math.h"

int math_sub(int a, int b) {
    return a - b;
}

// ./3rd/math/math_add.c
#include "math.h"

int math_add(int a, int b) {
    return a + b;
}
aux_source_directory(./ LIST_PRINT)

add_library(lib_print_dynamic SHARED ${LIST_PRINT})
add_library(lib_print_static  STATIC ${LIST_PRINT})

set_target_properties(lib_print_dynamic PROPERTIES OUTPUT_NAME "print")
set_target_properties(lib_print_static  PROPERTIES OUTPUT_NAME "print")

set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
// ./3rd/print/print.h
#if !defined(__PRINT_PRINT_H__)
#define __PRINT_PRINT_H__

void print();

#endif // __PRINT_PRINT_H__

// ./3rd/print/print.c
#include "print.h"
#include <stdlib.h>

void print(){
    printf("lib print\n");
}
include_directories(${PROJECT_SOURCE_DIR}/3rd)

set(SRC_MAIN main.c)

add_executable(main ${SRC_MAIN})

target_link_libraries(main lib_math)
target_link_libraries(main lib_print_dynamic)
#include <stdio.h>

#include "math/math.h"
#include "print/print.h"

int main(int argc, char **argv){
    printf("Hello World from t1 Main\n");
    printf("3 + 2 = %d\n", math_add(3, 2));
    printf("3 - 2 = %d\n", math_sub(3, 2));
    print();
    return 0;
}

cmake_minimum_required(VERSION 3.11)

project(main)

add_subdirectory(./3rd/math  lib_math)
add_subdirectory(./3rd/print lib_print)

add_subdirectory(./src bin)

运行

$ cmake ..
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/fl/Qt/test_code/cmake/t1111/build

$ make
Scanning dependencies of target lib_math
[ 12%] Building C object lib/CMakeFiles/lib_math.dir/math_add.c.o
[ 25%] Building C object lib/CMakeFiles/lib_math.dir/math_sub.c.o
[ 25%] Built target lib_math
Scanning dependencies of target lib_print_static
[ 37%] Building C object lib2/CMakeFiles/lib_print_static.dir/print.c.o
[ 50%] Linking C static library ../lib/libprint.a
[ 50%] Built target lib_print_static
Scanning dependencies of target lib_print_dynamic
[ 62%] Building C object lib2/CMakeFiles/lib_print_dynamic.dir/print.c.o
[ 75%] Linking C shared library ../lib/libprint.so
[ 75%] Built target lib_print_dynamic
Scanning dependencies of target main
[ 87%] Building C object bin/CMakeFiles/main.dir/main.c.o
[100%] Linking C executable main
[100%] Built target main

$ ll
total 56
drwxrwxr-x 7 fl fl  4096 11月 14 17:19 ./
drwxrwxr-x 6 fl fl  4096 10月 30 22:47 ../
drwxrwxr-x 3 fl fl  4096 11月 14 17:20 bin/
-rw-rw-r-- 1 fl fl 13735 11月 14 17:19 CMakeCache.txt
drwxrwxr-x 4 fl fl  4096 11月 14 17:20 CMakeFiles/
-rw-rw-r-- 1 fl fl  1857 11月 14 17:19 cmake_install.cmake
drwxrwxr-x 2 fl fl  4096 11月 14 17:20 lib/
drwxrwxr-x 3 fl fl  4096 11月 14 17:19 lib_math/
drwxrwxr-x 3 fl fl  4096 11月 14 17:19 lib_print/
-rw-rw-r-- 1 fl fl  5596 11月 14 17:19 Makefile

$ ldd bin/main
	linux-vdso.so.1 (0x00007ffebdd17000)
	libprint.so => /home/fl/Qt/test_code/cmake/t1111/build/lib/libprint.so (0x00007fab04105000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fab03ef1000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fab04111000)

注意事项

  1. ldd使用注意事项请参考文章https://www.cnblogs.com/faithlocus/p/15552440.html

  2. add_library(<name> [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] [source1] [source2 ...]) # 使用指定的源文件向项目添加库

    • STATIC:用于创建静态库,即编译文件的打包存档,以便在连接其他目标时使用。

    • SHARED:用于创建动态库,即可以动态链接,并在运行时加载的库。

    • OBJECT:可将给定add_library的列表中的源码编译到目标文件,不将它们归档到静态库中,也不能将它们链接到共享对象中。

    • MODULE:又名DSO组。与SHARED库不同,它们不链接到项目中的任何目标,不过

标签:cmake,lib,静态,--,add,print,fl,链接,math
来源: https://www.cnblogs.com/faithlocus/p/15552669.html