CMake基础学习

CMake基础学习

  • 设定软件安装目录
if(NOT DEFINED CMAKE_INSTALL_PREFIX)
    set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Installation Directory")
endif()
  • 定义当前cmake版本范围
    cmake_minimum_required(VERSION 3.5.1)

  • 定义工程名称
    project(youapp)

#make verbose=1 开启
set(CMAKE_VERBOSE_MAKEFILE ON) 

#编译类型为Debug, 另一个是Release
SET(CMAKE_BUILD_TYPE "Debug")

#设置c++编译时的参数
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

#开关选项
option(WITH_Board_X64 "board x64" ON)
option(WITH_lua "have lua" ON)
option(WITH_opencv "have opencv" ON)
if(WITH_opencv)
	set(CONFIG_HAVE_OPENCV "1")
else()
	set(CONFIG_HAVE_OPENCV "0")
endif()
  • 判断
if()
elseif()
endif()

#判断linux系统
if(UNIX)
#判断windows系统
if(WIN32)

#判断文件存在
if(EXISTS "aab")
#判断文件不存在
if(NOT "aaa")
  • 设定值set
#设定OpenCV_DIR目录
set(OpenCV_DIR "D:/opencv4.4.0/opencv/build")

#设定Torch目录
set(Torch_DIR "F:/libtorch/")
  • 查找cmake包 find_package
#查找OpenCV
find_package(OpenCV REQUIRED)
#查找libtorch
find_package(Torch PATHS ${Torch_DIR} NO_DEFAULT REQUIRED)
  • 添加子目录中的Makefile
add_subdirectory(lxai_zc_api)
  • 头文件包含
include_directories(${PROJECT_SOURCE_DIR}/include)
  • 连接文件目录对应gcc -L
link_directories(${PROJECT_BINARY_DIR}/ex)
  • 生成执行文件
#后面是文件列表
add_executable(myapp src/main.cpp)
  • 生成动态库
add_library(${PROJECT_NAME}  SHARED ${SRC_LIST})

SHARED 动态
STATIC 静态
  • 添加依赖库
target_link_libraries(myapp ex)
#target_link_libraries(myapp libex.so)
#target_link_libraries(myapp libex.a)
#target_link_libraries(myapp libex.lib)
#target_link_libraries(myapp -lex)
  • 设置RPATH路径
set_target_properties(myapp PROPERTIES INSTALL_RPATH "$ORIGIN:$ORIGIN/libs:libtorch/lib:/opt/myapp/libs:/opt/myapp/libtorch/lib")

set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-Wl,-rpath,./libs,-disable-new-dtags")

#文件生成模板config.h.in生成config.h
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/config.h)

其中config.h.in 如下
#define CONFIG_HAVE_LUA @CONFIG_HAVE_LUA@
#define CONFIG_HAVE_OPENCV @CONFIG_HAVE_OPENCV@
  • 源文件列表
aux_source_directory(src/ SRC_LIST)

file(GLOB SRC_LIST src/*.cpp src/*.c)

  • 复制到安装目录
install(TARGETS ${PROJECT_NAME} DESTINATION lib)
install(FILES ${LIB_HEADERS} DESTINATION include)
  • 编译输出目录(vc工程可用)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_INSTALL_PREFIX}/debug)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_INSTALL_PREFIX}/release)
  • Windows中使用cmake进行dll的函数导出(你不用再写dllexport)
if(WIN32)
	add_definitions(-D_CRT_SECURE_NO_WARNINGS)

	#https://segmentfault.com/q/1010000042696634 您不需要任何导出宏到类或函数! 使用此方法时不能使用 /GL( 全程序优化)!
	set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()
#vs20xx VC工程中的文件分组
if(WIN32)
source_group(TREE ${CMAKE_SOURCE_DIR} FILES ${libmedia_src} ${pipe_header_files})
endif()

# windows release模式下生成pdb文件
if(WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4996 /Zi /utf-8")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
endif()

#Windows DLL由cmake导出api
#https://segmentfault.com/q/1010000042696634 您不需要任何导出宏到类或函数! 使用此方法时不能使用 /GL( 全程序优化)!
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  • 添加子模块
#libtools子模块目录添加(比如设计一个libtools.dll)
ADD_SUBDIRECTORY(libtools libtools)
#最后链接这个库
target_link_libraries(${PROJECT_NAME} libtools)

例子lua的编译

#lua库生成cmake文件
cmake_minimum_required(VERSION 3.5.1)

#安装目录
#set(CMAKE_INSTALL_PREFIX "${PROJECT_SOURCE_DIR}/install")
set(CMAKE_CONFIGURATION_TYPES "Debug;Release")

project(liblua52)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4996 /Zi /utf-8")

#兼容lua5.1的一些功能
if(WIN32)
add_definitions(-DLUA_BUILD_AS_DLL)
add_definitions(-DLUA_COMPAT_ALL)
add_definitions(-DLUA_LIB)
endif()

#头文件
include_directories(${PROJECT_SOURCE_DIR}/src)

file(GLOB LUA_LIB_SRC src/lapi.c src/lcode.c src/lctype.c src/ldebug.c src/ldo.c src/ldump.c src/lfunc.c src/lgc.c src/llex.c src/lmem.c src/lobject.c src/lopcodes.c src/lparser.c src/lstate.c src/lstring.c src/ltable.c src/ltm.c src/lundump.c src/lvm.c src/lzio.c)
	
file(GLOB LUA_CORE_SRC src/lauxlib.c src/lbaselib.c src/lbitlib.c src/lcorolib.c src/ldblib.c src/liolib.c src/lmathlib.c src/loslib.c src/lstrlib.c src/ltablib.c src/loadlib.c src/linit.c)

#add_library(${PROJECT_NAME}  SHARED ${LUA_LIB_SRC} ${LUA_CORE_SRC})

add_library(${PROJECT_NAME} SHARED ${LUA_LIB_SRC} ${LUA_CORE_SRC})

file(GLOB_RECURSE LIB_HEADERS src/lua.h src/luaconf.h src/lualib.h src/lauxlib.h src/lua.hpp)

add_executable(lua52 src/lua.c)
add_executable(luac52 src/luac.c ${LUA_LIB_SRC} ${LUA_CORE_SRC})

target_link_libraries(lua52 ${PROJECT_NAME})
#target_link_libraries(luac52 ${PROJECT_NAME})

install(TARGETS ${PROJECT_NAME} DESTINATION lib)
install(FILES ${LIB_HEADERS} DESTINATION include)
install(TARGETS lua52 DESTINATION lib)
install(TARGETS luac52 DESTINATION lib)

例子:ffmpeg的ffplay的编译

if(NOT DEFINED CMAKE_INSTALL_PREFIX)
    set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Installation Directory")
endif()
message(STATUS "CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}")

#find_package(PythonInterp REQUIRED)
cmake_minimum_required(VERSION 3.5.1)

set(CMAKE_CONFIGURATION_TYPES "Release")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_INSTALL_PREFIX}/debug)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_INSTALL_PREFIX}/release)

project(ffplay)

#设置RPATH
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) 

#SDL2
set(SDL_Dir "F:\\src\\SDL2\\")
include_directories(${SDL_Dir}/include/SDL2)
find_library(SDL2 SDL2-static.lib ${SDL_Dir}/lib)

#ffmpeg
set(DEPS_PATH_FFMPEG "${CMAKE_CURRENT_SOURCE_DIR}/../install")

include_directories(${DEPS_PATH_FFMPEG}/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../)

add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/ffplay.c 
		${CMAKE_CURRENT_SOURCE_DIR}/cmdutils.c
		${CMAKE_CURRENT_SOURCE_DIR}/../config.h
		)

message("avutil ${avutil}")
message("avcodec ${avcodec}")
message("avdevice ${avdevice}")
message("avfilter ${avfilter}")

#ffmpeg
find_library(avcodec avcodec ${DEPS_PATH_FFMPEG}/bin)
find_library(avdevice avdevice ${DEPS_PATH_FFMPEG}/bin)
find_library(avfilter avfilter ${DEPS_PATH_FFMPEG}/bin)
find_library(avformat avformat ${DEPS_PATH_FFMPEG}/bin)
find_library(avutil avutil ${DEPS_PATH_FFMPEG}/bin)
find_library(postproc postproc ${DEPS_PATH_FFMPEG}/bin)
find_library(swresample swresample ${DEPS_PATH_FFMPEG}/bin)
find_library(swscale swscale ${DEPS_PATH_FFMPEG}/bin)

target_link_libraries(${PROJECT_NAME} ${avutil})
target_link_libraries(${PROJECT_NAME} ${avcodec})
target_link_libraries(${PROJECT_NAME} ${avdevice})
target_link_libraries(${PROJECT_NAME} ${avfilter})
target_link_libraries(${PROJECT_NAME} ${avformat})
target_link_libraries(${PROJECT_NAME} ${postproc})
target_link_libraries(${PROJECT_NAME} ${swresample})
target_link_libraries(${PROJECT_NAME} ${swscale})

#
target_link_libraries(${PROJECT_NAME} ${SDL2})

#其他依赖库文件
if(WIN32)
target_link_libraries(${PROJECT_NAME} user32.lib;gdi32.lib;winmm.lib;imm32.lib;ole32.lib;oleaut32.lib;version.lib;uuid.lib;advapi32.lib;setupapi.lib;shell32.lib;dinput8.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib)
else()
target_link_libraries(${PROJECT_NAME} pthread)
target_link_libraries(${PROJECT_NAME} dl)
endif()


你可能感兴趣的:(cmake)