heheda@linux:~/Linux/loveDBTeacher-v2$ tree
.
├── CMakeLists.txt
├── include
│ └── head.h
├── main.c
└── src├── add.c├── div.c├── mult.c└── sub.c
CMake Calc 项目 在这里有add.c,div.c,mult.c,sub.c,main.c,head.h
二、生成静态库
8.1 制作静态库
add_library(库名称 STATIC 源文件1 [源文件2] ...)在Linux中,静态库名字分为三部分:lib+库名字+.a,此处只需要指定出库的名字就可以了,
另外两部分在生成该文件的时候会自动填充。方法一:
aux_source_directory(${PROJECT_SOURCE_DIR}/src SRC_LIST)
add_library(calc STATIC ${SRC_LIST})方法二:
file(GLOB SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
add_library(calc STATIC ${SRC_LIST})这样最终就会生成对应的静态库文件libcalc.a
- CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MyProject)#包含头文件 注意:PROJECT_SOURCE_DIR 或 CMAKE_CURRENT_SOURCE_DIR
include_directories(${PROJECT_SOURCE_DIR}/include)# 方式一: 使用aux_source_directory命令
# aux_source_directory(./src SRC_LIST)
aux_source_directory(${PROJECT_SOURCE_DIR}/src SRC_LIST)
add_executable(app main.c ${SRC_LIST}) # 生成可执行的程序# 制作库文件
# 1.生成静态库 libcalc.a
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
add_library(calc STATIC ${SRC_LIST})#增加-std=c++11
set(CMAKE_CXX_STANDARD 11)set(HOME ${PROJECT_SOURCE_DIR}) # 定义一个变量用于存储一个绝对路径
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin) # 将拼接好的路径值设置给EXECUTABLE_OUTPUT_PATH宏
- CMakeLists.txt (也可以写成这样)
cmake_minimum_required(VERSION 3.10)
project(MyProject)#包含头文件 注意:PROJECT_SOURCE_DIR 或 CMAKE_CURRENT_SOURCE_DIR
include_directories(${PROJECT_SOURCE_DIR}/include)# 方式二: 使用GLOB命令 注意:PROJECT_SOURCE_DIR 或 CMAKE_CURRENT_SOURCE_DIR
# set(SRC ${PROJECT_SOURCE_DIR}/src)
set(SRC ${CMAKE_CURRENT_LIST_DIR}/src)
file(GLOB SRC_LIST ${SRC}/*.c)
add_executable(app main.c ${SRC_LIST}) # 生成可执行的程序# 制作库文件
# 1.生成静态库 libcalc.a
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
add_library(calc STATIC ${SRC_LIST})#增加-std=c++11
set(CMAKE_CXX_STANDARD 11)set(HOME ${PROJECT_SOURCE_DIR}) # 定义一个变量用于存储一个绝对路径
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin) # 将拼接好的路径值设置给EXECUTABLE_OUTPUT_PATH宏
- 执行命令:
执行命令:
1.mkdir build
2.cd build
3.cmake ..
4.make
5.cd ../bin
6./app执行结果:
heheda@linux:~/Linux/loveDBTeacher-v2$ mkdir build
heheda@linux:~/Linux/loveDBTeacher-v2$ cd build
heheda@linux:~/Linux/loveDBTeacher-v2/build$ cmake ..
-- 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: /home/heheda/Linux/loveDBTeacher-v2/build
heheda@linux:~/Linux/loveDBTeacher-v2/build$ make
Scanning dependencies of target app
[ 9%] Building C object CMakeFiles/app.dir/main.c.o
[ 18%] Building C object CMakeFiles/app.dir/src/add.c.o
[ 27%] Building C object CMakeFiles/app.dir/src/div.c.o
[ 36%] Building C object CMakeFiles/app.dir/src/mult.c.o
[ 45%] Building C object CMakeFiles/app.dir/src/sub.c.o
[ 54%] Linking C executable ../bin/app
[ 54%] Built target app
Scanning dependencies of target calc
[ 63%] Building C object CMakeFiles/calc.dir/src/add.c.o
[ 72%] Building C object CMakeFiles/calc.dir/src/div.c.o
[ 81%] Building C object CMakeFiles/calc.dir/src/mult.c.o
[ 90%] Building C object CMakeFiles/calc.dir/src/sub.c.o
[100%] Linking C static library ../lib/libcalc.a
[100%] Built target calc
heheda@linux:~/Linux/loveDBTeacher-v2/build$ cd ../bin
heheda@linux:~/Linux/loveDBTeacher-v2/bin$ ./app
a = 20, b = 12
a + b = 32
a - b = 8
a * b = 240
a / b = 1.666667
heheda@linux:~/Linux/loveDBTeacher-v2/bin$
三、生成动态库
8.2 制作动态库
add_library(库名称 SHARED 源文件1 [源文件2] ...)
在Linux中,动态库名字分为三部分:lib+库名字+.so,此处只需要指定出库的名字就可以了,
另外两部分在生成该文件的时候会自动填充
方法一:
aux_source_directory(${PROJECT_SOURCE_DIR}/src SRC_LIST)
add_library(calc SHARED ${SRC_LIST})方法二:
file(GLOB SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
add_library(calc SHARED ${SRC_LIST})
这样最终就会生成对应的动态库文件libcalc.so8.3 指定输出的路径
由于在Linux下生成的静态库默认不具有可执行权限,所以在指定静态库生成的路径的时候就
不能使用EXECUTABLE_OUTPUT_PATH宏了,而应该使用LIBRARY_OUTPUT_PATH,这个宏对
应静态库文件和动态库文件都适用file(GLOB SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
# 设置动态库/静态库生成路径
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
# 生成动态库
add_library(calc SHARED ${SRC_LIST})
# 生成静态库
add_library(calc STATIC ${SRC_LIST})
- CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MyProject)#包含头文件 注意:PROJECT_SOURCE_DIR 或 CMAKE_CURRENT_SOURCE_DIR
include_directories(${PROJECT_SOURCE_DIR}/include)# 方式一: 使用aux_source_directory命令
# aux_source_directory(./src SRC_LIST)
aux_source_directory(${PROJECT_SOURCE_DIR}/src SRC_LIST)
add_executable(app main.c ${SRC_LIST}) # 生成可执行的程序# 制作库文件
# 2.生成动态库 libcalc.so
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
add_library(calc SHARED ${SRC_LIST})#增加-std=c++11
set(CMAKE_CXX_STANDARD 11)set(HOME ${PROJECT_SOURCE_DIR}) # 定义一个变量用于存储一个绝对路径
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin) # 将拼接好的路径值设置给EXECUTABLE_OUTPUT_PATH宏
- CMakeLists.txt (也可以写成这样)
cmake_minimum_required(VERSION 3.10)
project(MyProject)#包含头文件 注意:PROJECT_SOURCE_DIR 或 CMAKE_CURRENT_SOURCE_DIR
include_directories(${PROJECT_SOURCE_DIR}/include)# 方式二: 使用GLOB命令 注意:PROJECT_SOURCE_DIR 或 CMAKE_CURRENT_SOURCE_DIR
# set(SRC ${PROJECT_SOURCE_DIR}/src)
set(SRC ${CMAKE_CURRENT_LIST_DIR}/src)
file(GLOB SRC_LIST ${SRC}/*.c)
add_executable(app main.c ${SRC_LIST}) # 生成可执行的程序# 制作库文件
# 2.生成动态库 libcalc.so
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
add_library(calc SHARED ${SRC_LIST})#增加-std=c++11
set(CMAKE_CXX_STANDARD 11)set(HOME ${PROJECT_SOURCE_DIR}) # 定义一个变量用于存储一个绝对路径
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin) # 将拼接好的路径值设置给EXECUTABLE_OUTPUT_PATH宏
执行命令:
1.mkdir build
2.cd build
3.cmake ..
4.make
5.cd ../bin
6./app执行结果:
heheda@linux:~/Linux/loveDBTeacher-v2$ mkdir build
heheda@linux:~/Linux/loveDBTeacher-v2$ cd build
heheda@linux:~/Linux/loveDBTeacher-v2/build$ cmake ..
-- 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: /home/heheda/Linux/loveDBTeacher-v2/build
heheda@linux:~/Linux/loveDBTeacher-v2/build$ make
Scanning dependencies of target app
[ 9%] Building C object CMakeFiles/app.dir/main.c.o
[ 18%] Building C object CMakeFiles/app.dir/src/add.c.o
[ 27%] Building C object CMakeFiles/app.dir/src/div.c.o
[ 36%] Building C object CMakeFiles/app.dir/src/mult.c.o
[ 45%] Building C object CMakeFiles/app.dir/src/sub.c.o
[ 54%] Linking C executable ../bin/app
[ 54%] Built target app
Scanning dependencies of target calc
[ 63%] Building C object CMakeFiles/calc.dir/src/add.c.o
[ 72%] Building C object CMakeFiles/calc.dir/src/div.c.o
[ 81%] Building C object CMakeFiles/calc.dir/src/mult.c.o
[ 90%] Building C object CMakeFiles/calc.dir/src/sub.c.o
[100%] Linking C shared library ../lib/libcalc.so
[100%] Built target calc
heheda@linux:~/Linux/loveDBTeacher-v2/build$ cd ../bin
heheda@linux:~/Linux/loveDBTeacher-v2/bin$ ./app
a = 20, b = 12
a + b = 32
a - b = 8
a * b = 240
a / b = 1.666667
heheda@linux:~/Linux/loveDBTeacher-v2/bin$