利⽤CMake搭建软件编译架构
利⽤CMake搭建软件编译架构
0 写在前⾯的话
本博⽂主要介绍如何利⽤cmake组织软件编译,并且提供⼀个简单的软件编译架构的模板。⼤家可以根据⾃⼰实际情况进⾏改动,本⽂主要的内容:
cmake 命令简介
cmake 常⽤API详解
利⽤cmake搭建软件编译框架
主要的软件环境如下:
cmake版本:3.15
os:Ubuntu 18.04 64bit
Language:c/c++
cmake 命令⾏模式
⼤家可以从cmake官⽹下载cmake⼯具、查看相关的⽂档
1 CMake 能做什么
CMake是⼀种管理源代码编译的⼯具,最初被设计成Makefile的⽣成器,随着应⽤越来越⼴泛,利⽤CMake可以⽣成Visual Studio 等IDE的⼯程⽂件以便于其进⾏编译。CMake主要在c/c++中使⽤,当然也可以利⽤它进⾏编译其他语⾔。
2 CMake编译模板整体框架
⽬录结构如下:
.
├── app // ⽣成各种可执⾏⽂件
│├── // 添加各种⼦⽬录
│└── cmake_test
│├── // ⽣成可执⾏⽂件 cmake_test
│└── cmake_test.cpp
├── cmake_config // cmake相关配置⽂件
│├── ake // 添加cmake⼀些公共的配置信息:⽐如说编译选项等
│├── cm_ake // 设备型号配置⽂件
│├── ake // 平台相关的配置⽂件⽐如说:硬件平台软件平台等
│└── cm_ake // X86_64对应的配置⽂件
├── // 总体的cmake⽂件
├── components // 组件类的配置⽂件软件系统的各个组件
│├── // 本级⽬录的cmake⽂件
│└── dpdk_fifo // ⽆锁队列⽬录
│├── // ⽣成静态库或者动态库
│├── rte_atomic.h
│├── rte_ring.cpp
│└── rte_ring.h
├── include // 公共的头⽂件
├── lib // Generate Dynamic Library or Static Library in this directory ,all kinds of platform
│└── X86_64 // X86_64 platform
│├── libdpdk_fifo.a
│└── libdpdk_fifo.so
├── main_frame // the main frame directory:include the main frame of the software system
│├──
│└── Event // the event modle handles all kinds of event,such as: read write timer signal etc..│└──
├── out // the out directory : the exe file and the dynamic library
│└── cmake_test
│├── cmake_test
│└── libdpdk_fifo.so
└── third_party // the third party header file in this directory
3 项⽬使⽤
建⽴⼀个临时的编译⽬录 在该⽬录中⽣成⼤量的中间⽂件
root@wan:/wan/fly_in_coding# mkdir build
root@wan:/wan/fly_in_coding# cd build/
root@wan:/wan/fly_in_coding/build# pwd
/wan/fly_in_coding/build
root@wan:/wan/fly_in_coding/build# pwd
/wan/fly_in_coding/build
⽣成Makefile
root@wan:/wan/fly_in_coding/build# cmake ../ -Dplatform=X86_64
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.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: /wan/fly_in_coding/build
执⾏编译
root@wan:/wan/fly_in_coding/build# make
Scanning dependencies of target dpdk_fifo
[ 25%] Building CXX object components/dpdk_fifo/CMakeFiles/dpdk_fifo.dir/rte_ring.cpp.o [ 50%] Linking CXX shared library ../../../lib/X86_64/libdpdk_fifo.so
[ 50%] Built target dpdk_fifo
Scanning dependencies of target cmake_test
[ 75%] Building CXX object app/cmake_test/CMakeFiles/cmake_test.dir/cmake_test.cpp.o [100%] Linking CXX executable cmake_test
[100%] Built target cmake_test
root@wan:/wan/fly_in_coding/build# ls
app CMakeFiles ake components Makefile
进⾏安装 将动态库和可执⾏ ⽂件复制到⼀个单独的⽬录下
root@wan:/wan/fly_in_coding/build# make install
[ 50%] Built target dpdk_fifo
[100%] Built target cmake_test
Install
-- Install configuration: ""
-- Installing: /wan/fly_in_coding/out/cmake_test/cmake_test
-- Set runtime path of "/wan/fly_in_coding/out/cmake_test/cmake_test" to ""
-- Up-to-date: /wan/fly_in_coding/out/cmake_test
-- Installing: /wan/fly_in_coding/out/cmake_test/libdpdk_fifo.so
4 cmake⽂件清单及详解
4.1 顶级
cmake_minimum_required(VERSION 3.5)
add_subdirectory(components)
add_subdirectory(app)
4.2
cmake_minimum_required(VERSION 3.5)
add_subdirectory(cmake_test)
4.2.1 cmake_test中⽂件清单
<
#要求cmake版本最低3.5
cmake_minimum_required(VERSION 3.5)
include (../../cmake_config/ake)
#根据设备类型选择不同的cmake配置⽂件
include (${PROJECT_ROOT_PATH}/cmake_config/cm_ake)
#设置当前⼯程名字
set(project_name cmake_test)
set (CMAKE_INSTALL_PREFIX ${PROJECT_ROOT_PATH}/out/${project_name})
#添加源⽂件⽬录
aux_source_directory(. DIR_SRCS)
#当前模块⽣成可以⾏⽂件
add_executable(${project_name} ${DIR_SRCS})
#添加当前模块需要的库
target_link_libraries(${project_name} libdpdk_fifo.so )
install(TARGETS ${project_name} DESTINATION ${CMAKE_INSTALL_PREFIX})
install(DIRECTORY ${LIBRARY_OUTPUT_PATH}/ DESTINATION ${CMAKE_INSTALL_PREFIX} FILES_MATCHING PATTERN "*.so") cmake_test.cpp
#include <stdio.h>
#include "rte_ring.h"
int main(void)
{
struct rte_ring * r =NULL;
r =rte_ring_create("test_ring",32,0);
if(r !=NULL)
{
rte_ring_destory(r);
cmake如何使用}
else
{
}
printf("cmake_test \n");
return0;
}
4.3 cmake_config ⽂件详解
├── cmake_config // cmake相关配置⽂件
│ ├── ake // 添加cmake⼀些公共的配置信息:⽐如说编译选项等
│ ├── cm_ake // 设备型号配置⽂件
│ ├── ake // 平台相关的配置⽂件 ⽐如说:硬件平台 软件平台等
│ └── cm_ake // X86_64对应的配置⽂件
ake
#添加 c++11编译选项
add_compile_options(-std=c++11)
#添加 debug -g 编译选项
add_compile_options(-g)
add_compile_options(-fPIC)
#把编译警告当做错误处理
#add_compile_options(-Werror)
SET(PROJECT_ROOT_PATH "/wan/fly_in_coding")
#添加公共的头⽂件
include_directories(
#当前
./
# 第三库⽂件
${PROJECT_ROOT_PATH}/third_party/
${PROJECT_ROOT_PATH}/app/cmake_test
# 主框架头⽂件
${PROJECT_ROOT_PATH}/main_frame/
# 组件头⽂件
${PROJECT_ROOT_PATH}/components/
${PROJECT_ROOT_PATH}/components/dpdk_fifo/
)
#SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_ROOT_PATH}/out)
cm_ake
#根据设备类型选择不同的cmake配置⽂件
if(${DEVICE} STREQUAL "MODLE1")
add_definitions(-DMODLE1)
elseif(${DEVICE} STREQUAL "MODLE2")
add_definitions(-DMODLE2)
elseif(${DEVICE} STREQUAL "MODLE3")
add_definitions(-DMODLE3)
elseif(${DEVICE} STREQUAL "MODLE4")
add_definitions(-DMODLE4)
else()
endif()
ake
#根据设备类型选择不同的cmake配置⽂件
if(${platform} STREQUAL "X86_64")
include (${PROJECT_ROOT_PATH}/cmake_config/cm_ake) elseif(${platform} STREQUAL "NPNC2")
include (${PROJECT_ROOT_PATH}/cmake_config/cm_ake) else()
include (${PROJECT_ROOT_PATH}/cmake_config/cm_ake) endif()
cm_ake
add_definitions(-DX86_64)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_ROOT_PATH}/lib/X86_64) link_directories(${PROJECT_ROOT_PATH}/lib/X86_64)
4.4 components ⽂件详解
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论