cmake适用于c++编译模板

# cmake 版本
cmake_minimum_required(VERSION 3.5.1)
project(test)
# name
set(PROJECT_NAME test)


option(ARM32"option for ARM" OFF)



# set cmake cross compile
#set(CMAKE_CROSEECOMPILING  TRUE)
if(ARM32)
    set(CMKA_SYSTRM_NAME       Linux)
    set(CMAKE_C_COMPILER       arm-linux-gnueabihf-gcc)
    #set(CMAKE_CXX_COMPILER     arm-linux-gnueabihf-g++)
    #库存放的文件夹名字,链接库位于lib-32的目录
    set(LIB lib-32)
else()
    set(CMKA_SYSTRM_NAME       Linux)
    set(CMAKE_C_COMPILER       aarch64-linux-gnu-gcc)
    #set(CMAKE_CXX_COMPILER     aarch64-linux-gnu-g++)
    #库存放的文件夹名字,链接库位于lib的目录
    set(LIB lib)
endif(ZQ7020)

# 编译的参数
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -rdynamic -O2 -Wall -Wno-pointer-to-int-cast\
                            -Wno-incompatible-pointer-types -Wno-pointer-sign\
                            -Wno-missing-braces -Wno-format")

# add message 打印
message(STATUS "PROJECT_BINARY_DIR: " ${PROJECT_BINARY_DIR})
message(STATUS "PROJECT_SOURCE_DIR: " ${PROJECT_SOURCE_DIR})
#message(SEND_ERROR "this is a SEND_ERROR")
#message(FATAL_ERROR "this is a FATAL_ERROR")

# add 源码目录
aux_source_directory(${PROJECT_SOURCE_DIR}/src DIR_SRC)

# add 源码的头文件目录
include_directories(${PROJECT_SOURCE_DIR}/include)

# set 动态库的路径
set(cjson_install ${PROJECT_SOURCE_DIR}/3rd_opensrc/cjson_install/${LIB})
set(curl_install ${PROJECT_SOURCE_DIR}/3rd_opensrc/curl_install/${LIB})
set(openssl_install ${PROJECT_SOURCE_DIR}/3rd_opensrc/openssl_install/${LIB})
set(zlog_install ${PROJECT_SOURCE_DIR}/3rd_opensrc/zlog_install/${LIB})

link_directories(${cjson_install} ${curl_install} ${openssl_install} ${zlog_install})

set(3rd_lib cjson crypto ssl curl zlog pthread m crypt dl rt)

# set动态库的头文件

set(cjson_include ${PROJECT_SOURCE_DIR}/3rd_opensrc/cjson_install/include)
set(curl_include ${PROJECT_SOURCE_DIR}/3rd_opensrc/curl_install/include)
set(openssl_include ${PROJECT_SOURCE_DIR}/3rd_opensrc/openssl_install/include)
set(zlog_include ${PROJECT_SOURCE_DIR}/3rd_opensrc/zlog_install/include)

include_directories(${cjson_include} ${curl_include} ${openssl_include} ${zlog_include})

#可以向源文件传递宏
#add_definitions(-DDBG)

add_executable(${PROJECT_NAME} ${DIR_SRC})
target_link_libraries(${PROJECT_NAME} ${3rd_lib})

#创建release文件夹,并指定生成的可执行文件位于其中
execute_process(COMMAND mkdir -p ${PROJECT_BINARY_DIR}/release)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/release)
posted @ 2020-08-03 15:49  tccxy  阅读(314)  评论(0编辑  收藏  举报