我的CMakeLists.txt模板, 适用于windows SDK风格的程序,不考虑测试和安装问题.
rc资源文件部分,适用windows项目。
# ################################################################
# Copyright (c) 2022 wsk3Q
#
# history:
# 2022-10-05 created
#
# 使用:
# mkdir build
# cd build
# cmake -A Win32 ..
# cmake -G "MSYS Makefiles" ..
# cmake --build . --config Release
# ################################################################
cmake_minimum_required(VERSION 3.0)
if (POLICY CMP0074)
#for xxx_LIBRARY
cmake_policy(SET CMP0074 NEW)
endif()
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
message(STATUS "Using CMake version ${CMAKE_VERSION}")
# handle debug lib naming based on shipped project naming
if(NOT CMAKE_DEBUG_POSTFIX)
set(CMAKE_DEBUG_POSTFIX d)
endif()
set(PROJ_NAME "testbin")
project(${PROJ_NAME} VERSION 0.1)
set(PROJ_HDRS
resource.h
)
set(PROJ_SRCS
WinAnim.c
)
# RC文件处理
if(MSVC)
list(APPEND PROJ_SRCS WinAnim.rc)
elseif(MINGW)
# This gets us DLL resource information when compiling on MinGW.
if(NOT CMAKE_RC_COMPILER)
set(CMAKE_RC_COMPILER windres.exe)
endif()
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/res.obj
COMMAND ${CMAKE_RC_COMPILER}
-D GCC_WINDRES
-I ${CMAKE_CURRENT_SOURCE_DIR}
-I ${CMAKE_CURRENT_BINARY_DIR}
-o ${CMAKE_CURRENT_BINARY_DIR}/res.obj
-i ${CMAKE_CURRENT_SOURCE_DIR}/WinAnim.rc)
list(APPEND PROJ_SRCS ${CMAKE_CURRENT_BINARY_DIR}/res.obj)
endif()
add_executable(${PROJ_NAME} WIN32
${PROJ_SRCS} ${PROJ_HDRS}
)
if(UNIX)
target_compile_options(${PROJ_NAME} PRIVATE "-O2")
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
set_property(TARGET ${PROJ_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
target_compile_options(${PROJ_NAME} PRIVATE
"-Wno-maybe-uninitialized"
"-Wno-unused-result"
)
endif()
install(TARGETS ${PROJ_NAME}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
elseif(WIN32)
target_compile_definitions(${PROJ_NAME} PRIVATE
"UNICODE"
"_UNICODE"
"_WIN32_WINNT=0x600"
)
if(MSVC)
target_compile_definitions(${PROJ_NAME} PRIVATE
"_CRT_SECURE_NO_WARNINGS"
"_CRT_SECURE_NO_DEPRECATE"
)
target_compile_options(${PROJ_NAME} PRIVATE
"/wd4996"
"/wd4244"
"/wd4246"
"/wd4267"
"/wd4146"
"/wd4018"
)
endif()
target_link_libraries(${PROJ_NAME}
PRIVATE
user32
gdi32
Comctl32
)
endif()