################################################################################
# author: yanjie@20210426
# description: cmake简易指南
#
# cmake常用函数说明:
# cmake_minimum_required: 指定cmake最低版本号要求
# set: 设置变量
# add_compile_options: 增加编译函数
# execute_process: 执行命令
# message: 打印信息
# find_package: 查找第三方库
# include_directories: 指定头文件,类似于-I参数
# add_custom_target: 增加自定义编译目标,一般用于调用makefile
# aux_source_directory: 查找目标目录下的源码文件,并赋值给变量
# add_library: 将目标编译成lib形式
# add_executable: 将目标编译成可执行文件形式
# set_target_properties: 指定目标编译的属性,一般用于指定lib库版本并链接
# target_link_libraries: 指定依赖的库(-l/.a文件/.so文件),注意必须是绝对路径
################################################################################
cmake_minimum_required (VERSION 3.1)
project(VISMARTY_ATLAS_LIBS)
# 指定编译器
# set (CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER, "g++")
# 设置语言标准
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)
# 添加-fPIC编译函数
add_compile_options(-fPIC)
# 判断平台
set(ARCH_NAME x86_64)
set(ARCH_LIB_NAME x86_64)
# `| tr -d` 去掉换行
execute_process(COMMAND bash -c
"uname -m | tr -d '\n'"
OUTPUT_VARIABLE ARCHITECTURE
)
#string(REPLACE "\n" "" ARCHITECTURE "${ARCHITECTURE}") # 去掉换行
message(STATUS "Current arch:" ${ARCHITECTURE})
if(${ARCHITECTURE} STREQUAL "aarch64")
set(ARCH_NAME arm64)
set(ARCH_LIB_NAME aarch64)
endif()
# 查找opencv路径, 然后可以使用变量OpenCV_INCLUDE_DIRS和OpenCV_LIBS
find_package(OpenCV REQUIRED)
if(OpenCV_FOUND)
message(STATUS "OpenCV Found Version:" ${OpenCV_VERSION})
endif(OpenCV_FOUND)
# 指定头文件目录(-I)
include_directories(
./inc
./src
./src/utils
./src/service
./src/service/atlas300
./src/service/engine
/usr/local/Ascend/ascend-toolkit/latest/acllib_linux.${ARCH_NAME}/include
/usr/local/include
${OpenCV_INCLUDE_DIRS}
)
################## 编译uuid ###################
add_custom_target(build_uuid ALL
COMMAND ${CMAKE_MAKE_PROGRAM}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/utils/uuid
)
################## 编译分类库 ###################
# 设置要编译的目标名
set(CLASSIFY atlas_service)
# 指定要搜索源码文件
aux_source_directory(./src/utils/ UTILS_SRC)
# 也可以用下面的方式搜索
# file(GLOB ALL_C ./src/utils/*.c)
# file(GLOB ALL_CPP ./src/utils/*.cpp)
# 编译成动态库,指定源码文件
add_library(${CLASSIFY} SHARED
./src/service/engine/model.cpp
./src/service/atlas300/atlas.cpp
${UTILS_SRC}
)
# 指定要编译的动态库的版本
set_target_properties(${CLASSIFY} PROPERTIES VERSION 2.0.3)
# 指定依赖的库(-l/.a文件/.so文件)注意这里必须是绝对路径
target_link_libraries(${CLASSIFY}
${OpenCV_LIBS}
pthread
rt
${CMAKE_SOURCE_DIR}/src/utils/uuid/libuuid.a
${CMAKE_SOURCE_DIR}/src/utils/openssl/libssl_1.0.2r_ubuntu1804_${ARCH_LIB_NAME}.a
${CMAKE_SOURCE_DIR}/src/utils/openssl/libcrypto_1.0.2r_ubuntu1804_${ARCH_LIB_NAME}.a
/usr/local/Ascend/ascend-toolkit/latest/acllib_linux.${ARCH_NAME}/lib64/libascendcl.so
)
一个简易的cmake模板,用于编译自定义目标和lib库
最新推荐文章于 2024-09-05 02:12:01 发布