顶层CMakeLists.txt
cmake_minimum_required (VERSION 3.5)# 配置 交叉编译 放置在 project() 命令之前# /opt/fslc-wayland/2.5.2/sysroots/aarch64-fslc-linux/usr/bin/make: error
# while loading shared libraries: libdl.so.2: cannot open shared object file:
# No such file or directory
# 解决:export LD_LIBRARY_PATH=<交叉编译链里面的libdl.so.2的路径>set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)# 设置交叉编译工具链
set (CMAKE_SYSROOT /opt/fslc-wayland/2.5.2/sysroots/aarch64-fslc-linux)
set (COMPILE_PREFIX /opt/fslc-wayland/2.5.2/sysroots/x86_64-fslcsdk-linux/usr/bin/aarch64-fslc-linux/aarch64-fslc-linux-)
set (CMAKE_C_COMPILER ${COMPILE_PREFIX}gcc)
set (CMAKE_CXX_COMPILER ${COMPILE_PREFIX}g++)# 设置一个变量
set (LIBS_DIR ${CMAKE_SOURCE_DIR}/libs)project (ttm_hsm)# 添加编译子目录
add_subdirectory (./libs/ttm_hsm_api/)
add_subdirectory (./src/)
子目录CMakeLists.txt(编译共享库)
cmake_minimum_required (VERSION 3.5)project (ttm_hsm_api_lib)include_directories (${LIBS_DIR}/m2000/include)# 自动查找指定目录的源文件
aux_source_directory (. SRC_LIST)# set (SRC_LIST ./ttm_hsm_api.c)add_library (ttm_hsm_api_shared SHARED ${SRC_LIST})set_target_properties (ttm_hsm_api_shared PROPERTIES OUTPUT_NAME "ttm_hsm_api")set (LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR})message (STATUS "LIBS_DIR: ${LIBS_DIR}")
message (STATUS "LIBRARY_OUTPUT_PATH: ${LIBRARY_OUTPUT_PATH}")
message (STATUS "SRC_LIST: ${SRC_LIST}")
子目录CMakeLists.txt(编译可执行文件)
cmake_minimum_required (VERSION 3.5)project (ttm_hsm_demo)set (SRC_LIST ./demo/main.c)
set (SRC_LIST_1 ./demo2/main.c)include_directories (${LIBS_DIR}/m2000/include${LIBS_DIR}/ttm_hsm_api
)# find_library 在cmake时检查库是否存在
# find_library (LIB_M2000 m2000api HINTS ${LIBS_DIR}/m2000/lib)
# find_library (LIB_TTM_HSM_API ttm_hsm_api HINTS ${LIBS_DIR}/ttm_hsm_api)link_directories (${LIBS_DIR}/m2000/lib)
link_directories (${LIBS_DIR}/ttm_hsm_api)add_executable (demo1 ${SRC_LIST})
add_executable (demo2 ${SRC_LIST_1})set (EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)target_link_libraries (demo1 ttm_hsm_apim2000apipthread
)target_link_libraries (demo2 m2000api
)