小项目通用cmake

发布时间:2023年12月26日

小项目通用CMakeLists.txt

自己的小项目使用的 仅供参考

cmake_minimum_required(VERSION 3.12)

set(Target "LinuxApp.exe")

project(${Target})

option("BUILD_TEST" "Build test" OFF)
option("CROSS_COMPILE" "Cross compile" ON)

# 设置交叉编译器
if(CROSS_COMPILE)
    SET(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
    SET(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
else()
    SET(CMAKE_C_COMPILER gcc)
    SET(CMAKE_CXX_COMPILER g++)
endif()

message("CMAKE_C_COMPILER = " ${CMAKE_C_COMPILER})
message("CMAKE_CXX_COMPILER = " ${CMAKE_CXX_COMPILER})

# 设置编译选项
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")


# 查找源文件Target
file(GLOB_RECURSE SOURCES "${CMAKE_SOURCE_DIR}/*.c" "${CMAKE_SOURCE_DIR}/*.cpp")

if(BUILD_TEST)
    # 添加测试文件
    foreach(src_var IN ITEMS ${SOURCES})
        message("src_var = " ${src_var})
    endforeach(src_var IN ITEMS)
endif()

# 移除不需要的源文件
list(REMOVE_ITEM SOURCES "${CMAKE_SOURCE_DIR}/Hardware/gpio/test_gpio.c" 
                         "${CMAKE_SOURCE_DIR}/Hardware/uart/test_i2c.c"
                         "${CMAKE_SOURCE_DIR}/build/CMakeFiles/${CMAKE_VERSION}/CompilerIdC/CMakeCCompilerId.c"
                         "${CMAKE_SOURCE_DIR}/build/CMakeFiles/${CMAKE_VERSION}/CompilerIdCXX/CMakeCXXCompilerId.cpp")
message("----------------------------------")
if(BUILD_TEST)
    foreach(src_var IN ITEMS ${SOURCES})
        message("src_var = " ${src_var})
    endforeach()
endif(BUILD_TEST)


# 查找头文件
file(GLOB_RECURSE HEADERS "${CMAKE_SOURCE_DIR}/*.h")
message("----------------------------------")
if(BUILD_TEST)
    foreach(header_var IN ITEMS ${HEADERS})
        message("head_var" = ${header_var})
    endforeach()
endif(BUILD_TEST)

message("----------------------------------")
# 设置CMAKE_SOURCE_DIR
set(CMAKE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

# 查找所有文件夹并保存到一个list中
function(find_all_directories DIR_LIST DIR)
    file(GLOB children RELATIVE ${DIR} ${DIR}/*)
    list(REMOVE_ITEM children ".git" ".vscode" "build" "Bin")
    foreach(child ${children})
        if(IS_DIRECTORY ${DIR}/${child})
            set(FULL_PATH ${DIR}/${child})
            list(APPEND DIR_LIST ${FULL_PATH})
            message("FULL_PATH = " ${FULL_PATH})
            include_directories(${FULL_PATH})
            find_all_directories(DIR_LIST ${FULL_PATH})
        endif()
    endforeach()
endfunction()

# 调用函数并获取所有文件夹列表
find_all_directories(DIRECTORIES ${CMAKE_SOURCE_DIR})


# 生成目标文件
add_executable(${Target} ${SOURCES} ${HEADERS})





# 链接-lpthread库
target_link_libraries(${Target} pthread)
if(CROSS_COMPILE)
    # find_library(LIB_MODBUS 
    #     NAMES modbus 
    #     PATHS ${CMAKE_SOURCE_DIR}/Lib/arm/
    # )
    # message("LIB_MODBUS = " ${LIB_MODBUS})

    # # 使用find_library查找SQLite3库
    # find_library(LIB_SQLITE3
    #     NAMES sqlite3   # SQLite3库的基本名称,不包含前缀和后缀
    #     PATHS ${CMAKE_SOURCE_DIR}/Lib/arm/   # 使用指定的路径
    # )
    # message("LIB_SQLITE3 = " ${LIB_SQLITE3})

    target_link_libraries(${Target} ${CMAKE_SOURCE_DIR}/Lib/arm/libmodbus.so)
    target_link_libraries(${Target} ${CMAKE_SOURCE_DIR}/Lib/arm/libsqlite3.so)
else()
    find_library(LIB_MODBUS modbus /usr/local/lib)
    find_library(LIB_SQLITE3 sqlite3 /lib/x86_64-linux-gnu)
    target_link_libraries(${Target} ${LIB_MODBUS})
    target_link_libraries(${Target} ${LIB_SQLITE3})
endif(CROSS_COMPILE)

编译脚本

echo $1

if [ "$1" == "clean" ]; then
    echo "Cleaning..."
    rm -rf build
    rm Bin/LinuxApp.exe
    exit 0
fi

if [ "$1" == "build" ] || [ -z "$1" ]; then
    rm -rf build
    echo "Building..."
    cmake -B build -D CROSS_COMPILE=OFF
    cmake --build build
    cp build/LinuxApp.exe Bin/LinuxApp.exe
    exit 0
fi

文章来源:https://blog.csdn.net/weixin_43739167/article/details/135219242
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。