1. 文件拓朴图
.
├── bin
│ └── main.cpp
├── build
├── CMakeLists.txt
├── include
│ ├── add.h
│ └── sub.h
└── src
├── add.cpp
└── sub.cpp
2. add.h内容如下:
int add(int a, int b);
3. add.cpp内容如下:
#include "add.h"
int add(int a, int b)
{
return a+b;
}
4. sub.h内容如下:
int sub(int a, int b);
6. sub.cpp源码如下:
#include "sub.h"
int sub(int a, int b)
{
return a-b;
}
7. main.cpp内容如下:
#include <iostream>
#include <fstream>
#include <cmath>
#include "add.h"
#include "sub.h"
using namespace std;
int main(int argc, char* argv[])
{
cout << add(13, 12) << endl;
cout << sub(13, 12) << endl;
return 0;
}
8. CMakeLists.txt内容如下:
cmake_minimum_required(VERSION 3.5.0)
project(BSP_INTERFACE)
# 设置测试动态链接库应用程序的名字
set(TEST_APP test_app)
#设置动态库的名字,最终生成libbsp_interface.so
set(BSP_DYNAMIC_LIB bsp_interface)
# 设置动态链接库输出的指定路径
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/lib)
# 根据库文件代码生成动态库
add_library(${BSP_DYNAMIC_LIB}
SHARED
src/add.cpp
src/sub.cpp
)
# 设置动态链接库版本号
set_target_properties(${BSP_DYNAMIC_LIB} PROPERTIES VERSION 0.0.1 SOVERSION 1)
# 包含指定头文件所在的目录
target_include_directories(${BSP_DYNAMIC_LIB}
PUBLIC
${PROJECT_SOURCE_DIR}/include
)
# 设置安装库文件和头文件的路径
set(CMAKE_INSTALL_INCLUDEDIR ${PROJECT_SOURCE_DIR}/build/install)
# make install头文件
install(DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/include
)
# make install库文件
install(DIRECTORY ${LIBRARY_OUTPUT_PATH}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/lib
)
# 创建测试动态链接库应用程序
add_executable(${TEST_APP}
${PROJECT_SOURCE_DIR}/bin/main.cpp
)
# 链接动态库文件
target_link_libraries(${TEST_APP} ${BSP_DYNAMIC_LIB})
9. 编译项目:
mkdir build
cd build/
cmake ..
make
make install
10. 执行编译的log信息:
~/workspace/portable/APP/build$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.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/tiantian/workspace/portable/APP/build
~/workspace/portable/APP/build$ make
Scanning dependencies of target bsp_interface
[ 20%] Building CXX object CMakeFiles/bsp_interface.dir/src/add.cpp.o
[ 40%] Building CXX object CMakeFiles/bsp_interface.dir/src/sub.cpp.o
[ 60%] Linking CXX shared library lib/libbsp_interface.so
[ 60%] Built target bsp_interface
Scanning dependencies of target test_app
[ 80%] Building CXX object CMakeFiles/test_app.dir/bin/main.cpp.o
[100%] Linking CXX executable test_app
[100%] Built target test_app
~/workspace/portable/APP/build$ make install
[ 60%] Built target bsp_interface
[100%] Built target test_app
Install the project...
-- Install configuration: ""
-- Installing: /home/tiantian/workspace/portable/APP/build/install/include
-- Installing: /home/tiantian/workspace/portable/APP/build/install/include/sub.h
-- Installing: /home/tiantian/workspace/portable/APP/build/install/include/add.h
-- Installing: /home/tiantian/workspace/portable/APP/build/install/lib/lib
-- Installing: /home/tiantian/workspace/portable/APP/build/install/lib/lib/libbsp_interface.so.0.0.1
-- Installing: /home/tiantian/workspace/portable/APP/build/install/lib/lib/libbsp_interface.so
-- Installing: /home/tiantian/workspace/portable/APP/build/install/lib/lib/libbsp_interface.so.1
11. 执行测试应用程序:
~/workspace/portable/APP/build$ ./test_app
25
1