Bootstrap

[工程构建] 使用 pkg-config 的 cmake 模板

可执行文件

# 1) cmake basic
cmake_minimum_required(VERSION 3.12) #cmake version check
set(CXX_STANDARD 17) #c++ standard version)



# 2) project info
#auto generated variables as below:
#PROJECT_NAME: "hello"
#hello_BINARY_DIR: build root dir
#hello_SOURCE_DIR: source root dir
project(hello LANGUAGES C CXX)  #project name
message("build root dir: ${hello_BINARY_DIR}")
message("source root dir: ${hello_SOURCE_DIR}")



# 3) specify source files and create target
#SOURCE_FILES: all c cpp and hpp as source file
file(GLOB_RECURSE SOURCE_FILES  #glob all source files(c cpp hpp , h is excluded)
    ${hello_SOURCE_DIR}/*.c
    ${hello_SOURCE_DIR}/*.cpp
    ${hello_SOURCE_DIR}/*.hpp
)
list(FILTER SOURCE_FILES EXCLUDE REGEX "CMakeFiles/*")   #exclude cmake files from source list
message("source files: ${SOURCE_FILES}")
add_executable(${PROJECT_NAME} ${SOURCE_FILES}) #add executable file



# 4) package dependency (pkg-config)
find_package(PkgConfig REQUIRED)

#GTK_INCLUDE_DIRS : HEADER SERARCH PATHS
#GTK_LIBRARIES : LIBRARY NAME
#GTK_LIBRARY_DIRS : LIBRARY PATHS
pkg_check_modules(GTK REQUIRED gtk+-3.0)



# 5) include and link
include_directories(                    #head search path
    ${GTK_INCLUDE_DIRS}
)
link_directories(                      #library search path 
    ${GTK_LIBRARY_DIRS}
)
target_link_libraries(${PROJECT_NAME}   #what libraries needs to link
    ${GTK_LIBRARIES}
)



# 6) ADD_DEFINITIONS
ADD_DEFINITIONS(-D LINUX)



# 7) install
#install ${PROJECT_NAME} to ~/bin
set(INSTALL_PATH ~/bin)
install(
    TARGETS ${PROJECT_NAME} 
    DESTINATION ${INSTALL_PATH}
)
#install config file to ~/bin
set(INSTALL_PATH ~/bin)
file(GLOB_RECURSE CONFIG_FILES 
    ${hello_SOURCE_DIR}/*.ui
    ${hello_SOURCE_DIR}/*.ini
    ${hello_SOURCE_DIR}/*.conf
)
install(
    FILES ${CONFIG_FILES}
    DESTINATION ${INSTALL_PATH}
)



动态库

;