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

xingyun86 19天前 46

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" OFF)
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_C_FLAGS
		CMAKE_C_FLAGS_DEBUG
		CMAKE_C_FLAGS_RELEASE
		)
	foreach(CompilerFlag ${CompilerFlags})
		string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
	endforeach()
	add_compile_definitions(_CRT_SECURE_NO_WARNINGS _WINSOCK_DEPRECATED_NO_WARNINGS WIN32_LEAN_AND_MEAN _WIN32_WINNT=0x0501)
	add_compile_options("/W4" "/bigobj" "/std:c++latest" "/IGNORE:4200")
	add_link_options("/IGNORE:4006,4075,4088,4099")
	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(TARGET_LINKER_FLAGS "${TARGET_LINKER_FLAGS} /SUBSYSTEM:\"WINDOWS\",\"${WINDOW_VERSION}\" /ENTRY:\"mainCRTStartup\"")
	endif(ENABLE_PUBLISHER_LEVEL)
	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_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)
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})
# TODO: Add tests and install targets if needed.


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