CMake整理

本文详细介绍了CMake中的关键变量如目录结构、如何设置源文件和头文件、编译库和可执行程序的路径,以及针对不同平台的编译选项和调试策略。特别关注了多级目录项目管理和跨平台编译配置。
摘要由CSDN通过智能技术生成

1、变量

CMAKE_CURRENT_BINARY_DIR

指向当前处理的 CMakeLists.txt 文件对应的构建目录。对于多级目录结构的项目,每个目录的这个变量指向的都是它自己的构建目录

CMAKE_SOURCE_DIR

指向最顶层 CMakeLists.txt 文件的源码目录,即整个项目的根源码目录。在所有的 CMakeLists.txt 文件中保持不变,无论它们在源码树的何处。

PROJECT_SOURCE_DIR

指向最近通过 project() 命令定义的项目的根源码目录。如果是单个项目,则与 CMAKE_SOURCE_DIR 相同。在使用 add_subdirectory 命令嵌套项目时,对于每个子项目来说,它指向的是各自的根源码目录

2、设置源文件和头文件

源文件

aux_source_directory(. SRC_LIST) #这里 . 表示当前目录,SRC_LIST 是存放源文件列表的变量

file(GLOB SRC_LIST "src/*.cpp") # 这里file(GLOB ...)命令来查找src目录下所有扩展名为.cpp的文件,并将它们的名字存入SRC_LIST变量

add_subdirectory(src) #进入子目录

头文件

include_directories(

${TOP_DIR}/Include

${TOP_DIR}/Src

)

3、编译库和进程

SET(EXECUTABLE_OUTPUT_PATH ${TOP_DIR}/Bin/$${PLATFORM}) ###设置可执行程序生成路径

SET(LIBRARY_OUTPUT_PATH ${TOP_DIR}/Lib/$${PLATFORM}) #####设置库生成路径

#########设置库属性 源文件和输出名字

set(STATIC_LIB ${PROJECT_NAME}_static)

add_library(${STATIC_LIB} STATIC ${LIB_SRC})

set_target_properties(${STATIC_LIB} PROPERTIES OUTPUT_NAME ${LIB_NAME})

编译选项相关

set(CMAKE_SYSTEM_NAME LINUX)

set(CMAKE_C_COMPILER "gcc")

set(CMAKE_CXX_COMPILER "g++")

set(CMAKE_C_FLAGS "xxx")

set(CMAKE_CXX_FLAGS "xxx")

add_compile_options(-std=c++11 -g) ##增加编译选项

add_definitions(-DXXX) ### 添加宏

调试

message("-----  PLAT=${PLAT}")

示例

SET(CMAKE_SYSTEM_NAME LINUX)

SET(CMAKE_C_COMPILER "gcc")
SET(CMAKE_CXX_COMPILER "g++")

SET(CMAKE_C_FLAGS "xxxx")
SET(CMAKE_CXX_FLAGS "xxxx")

cmake_minimum_required(VERSION 3.1)

project(test_module)

set(TOP_DIR ${PROJECT_SOURCE_DIR})
set(ROOT_DIR ${TOP_DIR}/../..)
set(HEADER_FILE ${ROOT_DIR}/headerfile)

set(LIB_NAME test_module)
set(BIN_NAME test_module)


set(CMAKE_CXX_STANDARD 11)

add_definitions(-Dxxxxx_)

include_directories(
    ${HEADER_FILE}/xxxx
    ${TOP_DIR}/Src 
)

#可执行程序源码
aux_source_directory(${TOP_DIR}/Src BIN_SRC)


#库源码
aux_source_directory(${TOP_DIR}/Src LIB_SRC)


set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)

#编译库
set(STATIC_LIB ${PROJECT_NAME}_static)
add_library(${STATIC_LIB} STATIC ${LIB_SRC})
set_target_properties(${STATIC_LIB} PROPERTIES OUTPUT_NAME ${LIB_NAME} CLEAN_DIRECT_OUTPUT 1)

#编译进程
link_directories(${TOP_DIR}/libs/${CROSS_COMPILER}/lib)
add_executable(${BIN_NAME} ${BIN_SRC})
target_link_libraries(${BIN_NAME} pthread)

install(TARGETS ${STATIC_LIB} DESTINATION lib)
install(TARGETS ${BIN_NAME} DESTINATION bin)

# add_custom_command(TARGET ${BIN_NAME} POST_BUILD 
#                     COMMAND ${CMAKE_COMMAND} -E copy ${ROOT_DIR}/bin/${PLATFORM}/${BIN_NAME} ${ROOT_DIR}/bin/${PLATFORM}/${BIN_DEBUG_NAME}
#                     COMMAND ${CMAKE_STRIP} ${ROOT_DIR}/bin/${PLATFORM}/${BIN_NAME}
#                     COMMENT "${CMAKE_STRIP} bins"
#                   )

IF(CMAKE_SYSTEM_NAME MATCHES "Windows")
# message("windows")
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux")
# message("linux")
ENDIF()

 外部shell脚本启动如下

目录结构如下

├── build.sh
├── CMakeLists.txt
├── configs
│   ├── arm-ivotu840-linux.cmake
│   ├── win64.cmake
│   └── x64.cmake

#!/bin/bash

TOP_DIR=${PWD}

help()
{
    echo "usage: ./build.sh [PLATFORM]"
	echo "eg: ./build.sh x64"
	echo ""

	for ITEM in `ls configs`;do
		NAME="${ITEM%%.*}"
        if [ "$NAME" == "win64" ];then
			continue
		fi
		echo "${NAME}"
	done
}

build()
{
    BUILD_DIR=build/$1
    mkdir -p ${BUILD_DIR}
    rm -fr ${BUILD_DIR}/*

    cd ${BUILD_DIR}
    cmake -DCMAKE_TOOLCHAIN_FILE=${TOP_DIR}/configs/$1.cmake ${TOP_DIR}
    make

}

main()
{
    if [ $# = 1 ];then
        build $1 
    else
        help 
        exit -1
    fi
}

main "$@"

开源库是config编译

#!/bin/sh

TOP_DIR=$(pwd)

CHIP_ALL_LIST="arm-ivotu840-linux
  arm-ivotg840-linux
"

DIR_NAME="libevent-2.1.8-stable"

func_usage() {
  echo ""
  echo "CHIP List:"
  echo "  ${CHIP_ALL_LIST}"
  echo "  all"
  echo ""
  echo "Usage:"
  echo "  $0 {CHIP}"
  exit 1
}

if [ ! -z $1 ]; then
  if [ $1 == "all" ]; then
    CHIP_LIST=${CHIP_ALL_LIST}
  else
    CHIP_LIST=$1
  fi
else
  func_usage
fi

##### func #####
func_param() {
  TARGET_DIR=${TOP_DIR}/${DIR_NAME}
  BUILD_DIR=${TOP_DIR}/build/${CHIP}
  INSTALL_DIR=${BUILD_DIR}/_install
  EXTRA_CONFIG=" --disable-shared --disable-openssl "
  SMAKE_C_FLAGS=" -Wall -Os -g -DNDEBUG -D_REENTRANT -fdata-sections -ffunction-sections -Wl,-gc-sections -fPIC -fdiagnostics-color=auto "
  SMAKE_CXX_FLAGS=" -Wall -Os -g -DNDEBUG -D_REENTRANT -fdata-sections -ffunction-sections -Wl,-gc-sections -fPIC -fdiagnostics-color=auto "
  if [ "${CHIP}" == "arm-ivotu840-linux" ]; then
    SMAKE_COMPILER="arm-ivotu840-linux"
    SMAKE_C_FLAGS+="  -mtune=cortex-a53  -mfloat-abi=hard -ftree-vectorize -fno-builtin -fno-common -Wformat=1"
    SMAKE_CXX_FLAGS+=" -mtune=cortex-a53 -mfloat-abi=hard -ftree-vectorize -fno-builtin -fno-common -Wformat=1"
  elif [ "${CHIP}" == "arm-ivotg840-linux" ]; then
    SMAKE_COMPILER="arm-ivotg840-linux"
    SMAKE_C_FLAGS+=" -mtune=cortex-a53  -mfloat-abi=hard -ftree-vectorize -fno-builtin -fno-common -Wformat=1"
    SMAKE_CXX_FLAGS+=" -mtune=cortex-a53 -mfloat-abi=hard -ftree-vectorize -fno-builtin -fno-common -Wformat=1"
  else
    func_usage
  fi
  if [ ! -z ${SMAKE_COMPILER} ]; then
    SMAKE_C_COMPILER=${SMAKE_COMPILER}-gcc
    SMAKE_CXX_COMPILER=${SMAKE_COMPILER}-g++
  else
    SMAKE_C_COMPILER=gcc
    SMAKE_CXX_COMPILER=g++
  fi
  echo ""
  echo "********************************************************************************"
  echo "* CHIP: ${CHIP}"
  echo "* TARGET_DIR: ${TARGET_DIR}"
  echo "* BUILD_DIR: ${BUILD_DIR}"
  echo "* INSTALL_DIR: ${INSTALL_DIR}"
  echo "* EXTRA_CONFIG: ${EXTRA_CONFIG}"
  echo "* SMAKE_COMPILER: ${SMAKE_COMPILER}"
  echo "* SMAKE_C_COMPILER: ${SMAKE_C_COMPILER}"
  echo "* SMAKE_CXX_COMPILER: ${SMAKE_CXX_COMPILER}"
  echo "* SMAKE_C_FLAGS: ${SMAKE_C_FLAGS}"
  echo "* SMAKE_CXX_FLAGS: ${SMAKE_CXX_FLAGS}"
  echo "********************************************************************************"
}

func_copy() {
  CONFIG=${TOP_DIR}/.config
  if [ -e "${CONFIG}" ]; then
    while read LINE; do
      eval "${LINE}"
    done <${CONFIG}
    if [ -d "${CONFIG_COPY_DEST_DIR}" ]; then
      cp -v ${INSTALL_DIR}/lib/libevent.a ${CONFIG_COPY_DEST_DIR}
      cp -v ${INSTALL_DIR}/lib/libevent_core.a ${CONFIG_COPY_DEST_DIR}
      cp -v ${INSTALL_DIR}/lib/libevent_extra.a ${CONFIG_COPY_DEST_DIR}
      cp -v ${INSTALL_DIR}/lib/libevent_pthreads.a ${CONFIG_COPY_DEST_DIR}
    fi
  fi
}

##### make #####
for CHIP in ${CHIP_LIST}; do
  func_param
  rm -rf ${BUILD_DIR}
  mkdir -p ${BUILD_DIR}
  pushd ${BUILD_DIR}

  ${TARGET_DIR}/configure  --host=${SMAKE_COMPILER} --prefix=${INSTALL_DIR} \
    CFLAGS="${SMAKE_C_FLAGS}" CXXFLAGS="${SMAKE_CXX_FLAGS}" ${EXTRA_CONFIG}
  make clean
  make -j8
  make install
  if [ $? -ne 0 ]; then
    exit 1
  fi
  popd
  func_copy
done
exit 0

用makefile驱动cmake编译

DIR_TOP = $(PWD)
DIR_PLATFORM = ${DIR_TOP}/cmakes
FILE_PLATFORM = $(wildcard ${DIR_PLATFORM}/*.cmake)
DIR_INSTALL = ${DIR_TOP}/_install

PLATFORM = $(patsubst ${DIR_PLATFORM}/%.cmake,%,${FILE_PLATFORM})
EXTRA_CONFIG="-DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_LIBS=ON"
DIR_NAME="AudioDecEnc"
TARGET_DIR=${DIR_TOP}/${DIR_NAME}

all: ${PLATFORM}

${PLATFORM}:
	@mkdir -p ${DIR_TOP}/build/$@ ${DIR_INSTALL}/$@
	@rm -fr ${DIR_TOP}/build/$@/*
	@cd ${DIR_TOP}/build/$@;cmake -DCMAKE_TOOLCHAIN_FILE=${DIR_PLATFORM}/$@.cmake -DCMAKE_INSTALL_PREFIX=${DIR_INSTALL}/$@ \
	"${EXTRA_CONFIG}" ${TARGET_DIR}

	@make -C ${DIR_TOP}/build/$@ install

	@if [ -n "${MT_PREFIX}" ];then \
		if [ ! -d  ${MT_PREFIX}/$@ ];then \
			mkdir -p ${MT_PREFIX}/$@; \
		fi;\
		cp ${DIR_INSTALL}/$@/lib ${MT_PREFIX}/$@ -rvf; \
	fi
	
clean distclean:
	@rm -rf ${DIR_TOP}/build ${DIR_INSTALL}
        

.PHONY: clean all
set(CMAKE_SYSTEM_NAME Linux)

set(CROSS_COMPILER arm-ivotg840-linux)
set(CMAKE_C_COMPILER ${CROSS_COMPILER}-gcc)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILER}-g++)
set(CMAKE_LINKER ${CROSS_COMPILER}-ld)
set(CMAKE_AR ${CROSS_COMPILER}-ar)
set(CMAKE_RANLIB ${CROSS_COMPILER}-ranlib)
set(CMAKE_STRIP ${CROSS_COMPILER}-strip)

set(BASE_FLAGS "-Wall -Os -g -DNDEBUG -D_REENTRANT -fdata-sections -ffunction-sections -fPIC -fdiagnostics-color=auto ")
set(PLATFORM_FLAGS " -mtune=cortex-a53 -mfloat-abi=hard -ftree-vectorize -fno-builtin -fno-common -Wformat=1")

set(CMAKE_C_FLAGS "${BASE_FLAGS} ${PLATFORM_FLAGS} ")
set(CMAKE_CXX_FLAGS "${BASE_FLAGS} ${PLATFORM_FLAGS} ")

用makefile驱动内部config编译

DIR_TOP = $(PWD)
DIR_PLATFORM = ${DIR_TOP}/makes
FILE_PLATFORM = $(wildcard ${DIR_PLATFORM}/*.make)
DIR_INSTALL = ${DIR_TOP}/_install

PLATFORM = $(patsubst ${DIR_PLATFORM}/%.make,%,${FILE_PLATFORM})

all: ${PLATFORM}

help:
	@echo Usage: make arm-ivotg840-linux MT_PREFIX=./out_dir
	@echo MT_PREFIX=./out_dir
	
${PLATFORM}:
	@mkdir -p ${DIR_TOP}/build/$@ ${DIR_INSTALL}/$@
	@rm -fr ${DIR_TOP}/build/$@/*
	
	@make -f ${DIR_TOP}/rules/Makefile PLATFORM=$@

	@if [ -n "${MT_PREFIX}" ];then \
		if [ ! -d  ${MT_PREFIX}/$@/lib ];then \
			mkdir -p ${MT_PREFIX}/$@/lib; \
		fi;\
		cp ${DIR_INSTALL}/$@/lib/libevent.a ${MT_PREFIX}/$@/lib -rvf; \
	fi
	
clean distclean:
	@rm -rf ${DIR_TOP}/build ${DIR_INSTALL}
        

.PHONY: clean all

make配置

SMAKE_COMPILER=arm-ivotg840-linux

SMAKE_C_COMPILER=${SMAKE_COMPILER}-gcc
SMAKE_CXX_COMPILER=${SMAKE_COMPILER}-g++
SMAKE_LINKER=${CROSS_COMPILER}-ld
SCMAKE_AR=${CROSS_COMPILER}-ar
SMAKE_STRIP=${CROSS_COMPILER}-strip

SMAKE_BASE_FLAGS= -Wall -Os -g -DNDEBUG -D_REENTRANT -fdata-sections -ffunction-sections -Wl,-gc-sections -fPIC -fdiagnostics-color=auto
SMAKE_PLATFORM_FLAGS= -mtune=cortex-a53 -mfloat-abi=hard -ftree-vectorize -fno-builtin -fno-common -Wformat=1 

SMAKE_C_FLAGS=${SMAKE_BASE_FLAGS}
SMAKE_C_FLAGS+=${SMAKE_PLATFORM_FLAGS}

SMAKE_CXX_FLAGS=${SMAKE_BASE_FLAGS}
SMAKE_CXX_FLAGS+=${SMAKE_PLATFORM_FLAGS}

内部rules makefile

TOP_DIR = ${CURDIR}
DIR_NAME=libevent-2.1.8-stable
TARGET_DIR=${TOP_DIR}/${DIR_NAME}
DIR_INSTALL = ${TOP_DIR}/_install
SMAKE_EXTRA_CONFIG= --disable-shared --disable-openssl 

ifndef PLATFORM
$(error PLATFORM is not defined!)
endif

include ${TOP_DIR}/makes/${PLATFORM}.make

all:
	cd ${TOP_DIR}/build/${PLATFORM};\
	${TARGET_DIR}/configure  --host=${SMAKE_COMPILER} --prefix=${DIR_INSTALL}/${PLATFORM} \
    CFLAGS="${SMAKE_C_FLAGS}" CXXFLAGS="${SMAKE_CXX_FLAGS}" ${SMAKE_EXTRA_CONFIG} 

	cd ${TOP_DIR}/build/${PLATFORM};make && make install

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值