Windows常用CMake工程配置模板C++/C

xingyun86 2021-7-8 1529

Windows常用CMake工程配置模板C++/C

工程CMakeLists.txt:

# CMakeLists.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.8)
project ("MSUI")
option(ENABLE_PUBLISHER_LEVEL "Option for enable or disable publisher level" ON)
option(ENABLE_RELEASE_DEBUGFULL "Option for enable or disable publisher level" ON)
option(ENABLE_ADMINISTRATOR_LEVEL "Option for enable or disable administrator level" OFF)
if (MSVC)
    MESSAGE(STATUS "CMAKE_BUILD_TYPE=" ${CMAKE_BUILD_TYPE})
    set(CompilerFlags
        CMAKE_CXX_FLAGS
        CMAKE_CXX_FLAGS_DEBUG
        CMAKE_CXX_FLAGS_RELEASE
        CMAKE_CXX_FLAGS_RELWITHDEBINFO
        CMAKE_C_FLAGS
        CMAKE_C_FLAGS_DEBUG
        CMAKE_C_FLAGS_RELEASE
        CMAKE_C_FLAGS_RELWITHDEBINFO
        )
    foreach(CompilerFlag ${CompilerFlags})
        string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
    endforeach()
    add_compile_definitions(_CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE _WINSOCK_DEPRECATED_NO_WARNINGS WIN32_LEAN_AND_MEAN _WIN32_WINNT=0x0501)
    add_compile_options("/source-charset:utf-8" "/W4" "/bigobj" "/std:c++latest" "/IGNORE:4200")
    add_link_options("/IGNORE:4006,4075,4088,4099" "/FORCE:MULTIPLE")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /LARGEADDRESSAWARE")
    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LARGEADDRESSAWARE")
    set(TARGET_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
    if(ENABLE_PUBLISHER_LEVEL)
        if(CMAKE_CL_64)
            set(WINDOW_VERSION "5.02")
        else(CMAKE_CL_64)
            set(WINDOW_VERSION "5.01")
        endif(CMAKE_CL_64)
        #set(CMAKE_WIN32_EXECUTABLE ON) #if use WinMain uncomment
        set(TARGET_LINKER_FLAGS "${TARGET_LINKER_FLAGS} /SUBSYSTEM:\"WINDOWS\",\"${WINDOW_VERSION}\" /ENTRY:\"mainCRTStartup\"")
        #set(TARGET_LINKER_FLAGS "${TARGET_LINKER_FLAGS} /SUBSYSTEM:\"WINDOWS\",\"${WINDOW_VERSION}\" /ENTRY:\"wmainCRTStartup\"")
    endif(ENABLE_PUBLISHER_LEVEL)    
    if(ENABLE_RELEASE_DEBUGFULL)        
        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
        set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG:FULL /OPT:REF /OPT:ICF")
        set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG:FULL /OPT:REF /OPT:ICF")
    endif(ENABLE_RELEASE_DEBUGFULL)
    if(ENABLE_ADMINISTRATOR_LEVEL)
        set(TARGET_LINKER_FLAGS "${TARGET_LINKER_FLAGS} /MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\"")
    endif(ENABLE_ADMINISTRATOR_LEVEL)
else(MSVC)
endif(MSVC)
if (CMAKE_BUILD_TYPE STREQUAL Debug)
    add_compile_definitions(BUILD_DEBUG=1)
else()
    add_compile_definitions()
endif()
if(CMAKE_CL_64)
    set(BUILD_MODEL 64)
    set(BUILD_TARGET x64)
else()
    set(BUILD_MODEL 32)
    set(BUILD_TARGET x86)
endif()
if (CMAKE_HOST_APPLE)
    message(STATUS "Platform=APPLE")
    add_compile_definitions(BUILD_PLATFORM_APPLE=1)
elseif (CMAKE_HOST_UNIX)
    message(STATUS "Platform=LINUX")
    add_compile_definitions(BUILD_PLATFORM_LINUX=1)
elseif (CMAKE_HOST_WIN32)
    message(STATUS "Platform=WINDOWS")
    add_compile_definitions(BUILD_PLATFORM_WINDOWS=1)
endif ()
# Include sub-projects.
add_subdirectory ("MSUI")

子工程CMakeLists.txt

# CMakeLists.txt : CMake project for MSUI, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
set(project_name "MSUI")
project(${project_name})
include_directories(
	${CMAKE_CURRENT_SOURCE_DIR}
	${CMAKE_CURRENT_BINARY_DIR}
	${CMAKE_SOURCE_DIR}/vendor
	${CMAKE_SOURCE_DIR}/vendor/db
	${CMAKE_SOURCE_DIR}/vendor/ui
	${CMAKE_SOURCE_DIR}/vendor/dbg
	${CMAKE_SOURCE_DIR}/vendor/reg
	${CMAKE_SOURCE_DIR}/vendor/srv
	${CMAKE_SOURCE_DIR}/vendor/json
)
link_directories(
	${CMAKE_CURRENT_BINARY_DIR}
)
if(MSVC)
	if(CMAKE_CL_64)
		link_directories(
			${CMAKE_SOURCE_DIR}/vendor/lib/x64
		)
	else(CMAKE_CL_64)
		link_directories(
			${CMAKE_SOURCE_DIR}/vendor/lib/win32
		)
	endif(CMAKE_CL_64)
	list(APPEND LIBS ws2_32)
else(MSVC)
endif(MSVC)
if (CMAKE_HOST_WIN32)
	message(STATUS "Platform=Windows")
	list(APPEND PLATFORM_DEFINE -DTARGET_HOST_MS_WINDOWS=1)
elseif (CMAKE_HOST_UNIX)
	message(STATUS "Platform=Linux")
	list(APPEND PLATFORM_DEFINE -DTARGET_HOST_POSIX_X11=1)
elseif (CMAKE_HOST_APPLE)
	message(STATUS "Platform=Macos")
endif ()
list(APPEND common_ui_defs ASIO_STANDALONE)
if(MSVC)
	list(APPEND common_ui_opts "/std:c++latest")
else(MSVC)
	list(APPEND common_ui_opts "-std=c++23")
endif(MSVC)
file(GLOB SRCS "*.c" "*.rc" "*.cpp")
# Add source to this project's executable.
add_executable (${project_name} ${SRCS})
if(MSVC)
	SET_TARGET_PROPERTIES(${project_name} PROPERTIES LINK_FLAGS ${TARGET_LINKER_FLAGS})
endif(MSVC)
target_link_libraries(${project_name} ${LIBS})
target_compile_definitions(${project_name} PRIVATE ${common_ui_defs} PRIVATE ${PLATFORM_DEFINE})
target_compile_options(${project_name} PRIVATE ${common_ui_opts})
# TODO: Add tests and install targets if needed.

MSVC开启utf-8也可以使用:

add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")

add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")


×
打赏作者
最新回复 (0)
查看全部
全部楼主
返回