1. 程式人生 > 其它 >Step 3: Adding Usage Requirements for a Library

Step 3: Adding Usage Requirements for a Library

1新增庫的使用規範

2具體指令碼

2.1target_compile_definitions()

向工程中加入預處理定義

語法:

	target_compile_definitions(<target>
	  <INTERFACE|PUBLIC|PRIVATE> [items1...]
	  [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
  • cmake --build . --config release : 可以生成release版本,預設為debug版本

舉例

	cmake_minimum_required(VERSION 3.15)
	
	project ( hello_world_prj )
	
	set( EXE_SOURCES
	    src/main.cpp
	    src/Hello.cpp
	)
	
	add_executable( hello_world_exe  ${EXE_SOURCES} )
	
	target_include_directories( hello_world_exe
	    PRIVATE 
	        ${PROJECT_SOURCE_DIR}/include
	)
	
	target_compile_definitions( hello_world_exe PRIVATE MY_DEF1 )
	target_compile_definitions( hello_world_exe PRIVATE MY_DEF2 )
	target_compile_definitions( hello_world_exe PRIVATE MY_DEF3 )
	target_compile_definitions( hello_world_exe
	                            PRIVATE 
	                                  MY_DEF4
	                                  MY_DEF5
	                                  MY_DEF6
	                                  )
	
	# 到此,向工程hello_world_prj中添加了如上的預處理


	add_executable( hello_world_exe_1024  ${EXE_SOURCES} )
	
	target_include_directories( hello_world_exe_1024
	    PRIVATE 
	        ${PROJECT_SOURCE_DIR}/include
	)
	
	target_compile_definitions( hello_world_exe_1024 PRIVATE HELLO_DEF1 )
	target_compile_definitions( hello_world_exe_1024 PRIVATE HELLO_DEF2 )

	# 到此,向工程hello_world_exe_1024中添加了如上的預處理

參考
參考

2.2 target_compile_options()

向工程中新增編譯選項,target目標檔案必須已經存在啦

	Adds options to the COMPILE_OPTIONS or INTERFACE_COMPILE_OPTIONS target properties. These options are used when compiling the given <target>, which must have been created by a command such as add_executable() or add_library() and must not be an ALIAS target.

	在編譯給定目標檔案時,指定要用到的編譯選項。target目標檔案必須已經存在(由命令add_executable()或add_library()建立)且不能被IMPORTED修飾。

語法

	target_compile_options(<target> [BEFORE]
	  <INTERFACE|PUBLIC|PRIVATE> [items1...]
	  [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])

舉例1

在cmake指令碼中,設定編譯選項可以通過add_compile_options命令,也可以通過set命令修改CMAKE_CXX_FLAGSCMAKE_C_FLAGS
使用這兩種方式在有的情況下效果是一樣的,但請注意它們還是有區別的:
add_compile_options命令新增的編譯選項是針對所有編譯器的(包括c和c++編譯器),而set命令設定CMAKE_C_FLAGS

CMAKE_CXX_FLAGS變數則是分別只針對c和c++編譯器的。

	#判斷編譯器型別,如果是gcc編譯器,則在編譯選項中加入c++11支援
	if(CMAKE_COMPILER_IS_GNUCXX)
	    add_compile_options(-std=c++11)
	    message(STATUS "optional:-std=c++11")   
	endif(CMAKE_COMPILER_IS_GNUCXX)

使用add_compile_options新增-std=c++11選項,是想在編譯c++程式碼時加上c++11支援選項。但是因為add_compile_options是針對所有型別編譯器的,所以在編譯c程式碼時,就會產生如下warning,消除的方法為:

	#判斷編譯器型別,如果是gcc編譯器,則在編譯選項中加入c++11支援,在Linux可以跑通
	if(CMAKE_COMPILER_IS_GNUCXX)
	    set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
	    message(STATUS "optional:-std=c++11")   
	endif(CMAKE_COMPILER_IS_GNUCXX)

參考1

舉例2

	cmake_minimum_required ( VERSION 3.15 )

	project ( COMPILE_OPTIONS_PRJ CXX )
	
	set ( PRJ_SRC_LIST )
	set ( PRJ_HEADER_LIST )
	set ( PRJ_LIBRARIES )
	set ( PRJ_INCLUDE_DIRS )
	
	file ( GLOB root_header_files "${CMAKE_CURRENT_SOURCE_DIR}/*.h" )
	file ( GLOB root_src_files "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" )
	
	list ( APPEND PRJ_HEADER_LIST ${root_header_files} )
	list ( APPEND PRJ_SRC_LIST ${root_src_files} )
	
	add_executable ( ${PROJECT_NAME} ${PRJ_SRC_LIST} ${PRJ_HEADER_LIST} )
	
	target_include_directories ( ${PROJECT_NAME}
		PRIVATE
			${PRJ_INCLUDE_DIRS}
	)
	
	target_compile_options ( ${PROJECT_NAME}
		PRIVATE
			-MY_OP1
	)
	
	target_compile_options ( ${PROJECT_NAME}
		PRIVATE
			-MY_OP2
	)
	
	target_compile_options ( ${PROJECT_NAME}
		PRIVATE
			-MY_OP3
	)
	
	get_target_property ( MY_COMPILE_OPTIONS ${PROJECT_NAME} COMPILE_OPTIONS )
	
	message ( STATUS "MY_COMPILE_OPTIONS  = ${MY_COMPILE_OPTIONS}" )
	
	target_compile_features ( ${PROJECT_NAME} 
		PUBLIC 
			cxx_std_14
	)
	
	target_link_libraries ( ${PROJECT_NAME} 
	    PRIVATE 
	        ${PRJ_LIBRARIES}
	)

參考2

2.3 target_include_directories()

舉例

	cmake_minimum_required(VERSION 3.10)
	
	#遞迴獲取目錄下所有的.cxx檔案
	#file(GLOB_RECURSE cpp_files ./*.cxx)
	
	# 遞迴獲取目錄下所有的h檔案
	#file(GLOB_RECURSE h_files ./*.h)
	
	project(MathFunctions)
	
	add_library(MathFunctions mysqrt.cxx)
	
	# 新增該指令碼之後,主CMakeLists.txt就可以不包含該庫的標頭檔案啦
	# 因為使用該庫(MathFunctions)的使用者,可以使用該庫所在檔案的
	# 所以,使用INTERFACE介面
	# 子庫自己已經指明,若呼叫該子庫,就可以使用該子庫的標頭檔案
	# 這個方法好啊
	target_include_directories(MathFunctions INTERFACE
							${CMAKE_CURRENT_SOURCE_DIR})

詳見 Step1

2.5 target_compile_features

Add expected compiler features to a target. 如cxx_std_14(就是新增C++14的編譯特徵,可以對C++14進行編譯)

語法

	target_compile_features(<target> <PRIVATE|PUBLIC|INTERFACE> <feature> [...])

參考

最佳實踐:參考#2.2中的舉例2

Q:在ubuntu系統下,使用cmake編譯jsoncpp的時候,執行cmake後,出現錯誤,關鍵資訊為:target_compile_features specified unknown feature cxx_std_11 for target...

A:cmake版本太低,找不到cxx_std_11等相關巨集的定義。解決方法:升級cmake版本到最新版。

3參考

參考

參考