這篇主要要談如何建立一個library, 首先我們再src底下創建目錄mymath01用來放置我們的library, 裡面的soucre code包含mymath01.c與mymath01.h, 所以src/CMakeList.txt需要用add_subdirectory(mymath01)告訴CMake要編譯src/mymath01, 而src/mymath01/CMakeList.txt需要描述如何編譯Library, 主要是用add_library()告訴CMake這個library要用哪些source code編譯.
[brook@:~/Projects/cmake/02]$ tree
.
`-- src
|-- CMakeLists.txt
`-- mymath01
|-- CMakeLists.txt
|-- mymath01.c
`-- mymath01.h
2 directories, 4 files
[brook@:~/Projects/cmake/02]$ cat src/CMakeLists.txt
# Require a minimum version of cmake.
# cmake_minimum_required(VERSION <min>>[...<policy_max>] [FATAL_ERROR])
cmake_minimum_required(VERSION 3.10)
# Set the name of the project.
# project(<PROJECT-NAME< [<anguage-name>...])
# project(<PROJECT-NAME>
# [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
# [DESCRIPTION <project-description-string>]
# [LANGUAGES <language-name>...])
project(Tutorial VERSION 0.1.2.3 DESCRIPTION "This is Brook 1st CMake Lib")
# Add a subdirectory to the build.
# add_subdirectory(source_dir [binary_dir]
# [EXCLUDE_FROM_ALL])
add_subdirectory(mymath01)
[brook@:~/Projects/cmake/02]$ cat src/mymath01/CMakeLists.txt
# Add a library to the project using the specified source files.
# add_library( [STATIC | SHARED | MODULE]
# [EXCLUDE_FROM_ALL]
# source1 [source2 ...])
add_library(MyMathLib1 mymath01.c)
[brook@:~/Projects/cmake/02]$ cat src/mymath01/mymath01.h
#ifndef MYMATH01_H
#define MYMATH01_H
int squar(int a);
#endif
[brook@:~/Projects/cmake/02]$ cat src/mymath01/mymath01.c
#include "mymath01.h"
int squar(int a)
{
return a*a;
}
[brook@:~/Projects/cmake/02]$ mkdir build && cd build
[brook@:~/Projects/cmake/02/build]$ cmake ../src/
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.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: /build/brook/Projects/cmake/02/build
[brook@:~/Projects/cmake/02/build]$ cmake --build .
Scanning dependencies of target MyMathLib1
[ 50%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o
[100%] Linking C static library libMyMathLib1.a
[100%] Built target MyMathLib1
[brook@:~/Projects/cmake/02/build]$ ls
CMakeCache.txt CMakeFiles Makefile cmake_install.cmake mymath01
[brook@:~/Projects/cmake/02/build]$ ls mymath01/
CMakeFiles Makefile cmake_install.cmake libMyMathLib1.a
[brook@:~/Projects/cmake/02/build]$ nm mymath01/libMyMathLib1.a
mymath01.c.o:
0000000000000000 T squar
接著我們結合"Step 1"學的創建一個hello.c來引用MyMathLib1, 我們要用target_link_libraries()將MyMathLib1 link進來.
[brook@:~/Projects/cmake/02]$ tree
.
`-- src
|-- CMakeLists.txt
|-- hello.c
`-- mymath01
|-- CMakeLists.txt
|-- mymath01.c
`-- mymath01.h
2 directories, 5 files
[brook@:~/Projects/cmake/02]$ cat src/CMakeLists.txt
# Require a minimum version of cmake.
# cmake_minimum_required(VERSION <min>>[...<policy_max>] [FATAL_ERROR])
cmake_minimum_required(VERSION 3.10)
# Set the name of the project.
# project(<PROJECT-NAME< [<anguage-name>...])
# project(<PROJECT-NAME>
# [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
# [DESCRIPTION <project-description-string>]
# [LANGUAGES <language-name>...])
project(Tutorial VERSION 0.1.2.3 DESCRIPTION "This is Brook 1st CMake Lib")
# Add an executable to the project using the specified source files.
# add_executable(<name> [WIN32] [MACOSX_BUNDLE]
# [EXCLUDE_FROM_ALL]
# [source1] [source2 ...])
add_executable(Tutorial hello.c)
# Specify libraries or flags to use when linking a given target and/or its dependents.
# target_link_libraries(<target>
# <PRIVATE|PUBLIC|INTERFACE> - ...
# [<PRIVATE|PUBLIC|INTERFACE>
- ...]...)
target_link_libraries(Tutorial PUBLIC MyMathLib1)
# Add include directories to a target.
# target_include_directories(&;lt;target> [SYSTEM] [AFTER|BEFORE]
# &;lt;INTERFACE|PUBLIC|PRIVATE> [items1...]
# [&;lt;INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
target_include_directories(Tutorial PUBLIC "${PROJECT_SOURCE_DIR}/mymath01")
# Add a subdirectory to the build.
# add_subdirectory(source_dir [binary_dir]
# [EXCLUDE_FROM_ALL])
add_subdirectory(mymath01)
[brook@:~/Projects/cmake/02]$ cat src/hello.c
#include <stdio.h>
#include <stdlib.h>
#include "mymath01.h"
int main(int argc, char *argv[])
{
printf("%d\n", squar(atoi(argv[1])));
return 0;
}
[brook@:~/Projects/cmake/02]$ mkdir build && cd build
[brook@:~/Projects/cmake/02/build]$ cmake ../src
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.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: /build/brook/Projects/cmake/02/build
[brook@:~/Projects/cmake/02/build]$ ls
CMakeCache.txt CMakeFiles Makefile cmake_install.cmake mymath01
[brook@:~/Projects/cmake/02/build]$ cmake --build .
Scanning dependencies of target MyMathLib1
[ 25%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o
[ 50%] Linking C static library libMyMathLib1.a
[ 50%] Built target MyMathLib1
Scanning dependencies of target Tutorial
[ 75%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/02/build]$ ls
CMakeCache.txt CMakeFiles Makefile Tutorial cmake_install.cmake mymath01
[brook@:~/Projects/cmake/02/build]$ ./Tutorial 5
25
預設是build STATIC, 如果要改成SHARED, 只要加上SHARED這個Key word就可以了add_library(MyMathLib1 SHARED mymath01.c), 並利用set_target_properties()給shared library version與soversion.
[brook@:~/Projects/cmake/02-1]$ mkdir build && cd build [brook@:~/Projects/cmake/02-1/build]$ cat ../src/mymath01/CMakeLists.txt # Add a library to the project using the specified source files. # add_library(<name> [STATIC | SHARED | MODULE] # [EXCLUDE_FROM_ALL] # source1 [source2 ...]) add_library(MyMathLib1 SHARED mymath01.c) set_target_properties(MyMathLib1 PROPERTIES VERSION 1.2.3 SOVERSION 1) [brook@:~/Projects/cmake/02-1/build]$ cmake ../src -- The C compiler identification is GNU 7.5.0 -- The CXX compiler identification is GNU 7.5.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: /build/brook/Projects/cmake/02-1/build [brook@:~/Projects/cmake/02-1/build]$ cmake --build . Scanning dependencies of target MyMathLib1 [ 25%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o [ 50%] Linking C shared library libMyMathLib1.so [ 50%] Built target MyMathLib1 Scanning dependencies of target Tutorial [ 75%] Building C object CMakeFiles/Tutorial.dir/hello.c.o [100%] Linking C executable Tutorial [100%] Built target Tutorial [brook@:~/Projects/cmake/02-1/build]$ ls -al mymath01/ total 32 drwxrwxrwx 3 brook chbg 4096 Jun 26 21:51 . drwxrwxrwx 4 brook chbg 4096 Jun 26 21:51 .. drwxrwxrwx 3 brook chbg 4096 Jun 26 21:51 CMakeFiles -rw-rw-rw- 1 brook chbg 5602 Jun 26 21:50 Makefile -rw-rw-rw- 1 brook chbg 1135 Jun 26 21:50 cmake_install.cmake lrwxrwxrwx 1 brook chbg 18 Jun 26 21:51 libMyMathLib1.so -> libMyMathLib1.so.1 lrwxrwxrwx 1 brook chbg 22 Jun 26 21:51 libMyMathLib1.so.1 -> libMyMathLib1.so.1.2.3 -rwxrwxrwx 1 brook chbg 7440 Jun 26 21:51 libMyMathLib1.so.1.2.3
