cmake-08-Condition

本文介绍如何使用CMake设置不同级别的编译优化标志,并针对特定源文件进行精细化控制。通过示例展示了如何为整个库指定优化级别,同时为部分源文件设定较低级别的优化。

Condition Variable

	# use lib or use source : use lib by default(OFF)
	
	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	project(recipe-04 LANGUAGES CXX)
	
	# introduce a toggle for using a library
	set(USE_LIBRARY OFF)
	
	#print a message during cmake
	message(STATUS "Compile sources into a library? ${USE_LIBRARY}")
	
	# BUILD_SHARED_LIBS is a global flag offered by CMake
	# to toggle the behavior of add_library
	set(BUILD_SHARED_LIBS OFF) #could set to "ON"
	
	# list source,  append variale to _sources 
	list(APPEND _sources Message.hpp Message.cpp)
	
	if(USE_LIBRARY)
	  # add_library will create a static library
	  # since BUILD_SHARED_LIBS is OFF
	  add_library(message ${_sources})
	
	  add_executable(hello-world hello-world.cpp)
	
	  target_link_libraries(hello-world message)
	else()
	  add_executable(hello-world hello-world.cpp ${_sources})
	endif()

# log
	$ cmake .
	-- The CXX compiler identification is GNU 9.3.0
	-- Check for working CXX compiler: /usr/bin/c++
	-- Check for working CXX compiler: /usr/bin/c++ -- works
	-- Detecting CXX compiler ABI info
	-- Detecting CXX compiler ABI info - done
	-- Detecting CXX compile features
	-- Detecting CXX compile features - done
	-- Compile sources into a library? OFF
	-- Configuring done
	-- Generating done
	-- Build files have been written to: /home/jianleya/trainning/cmake/cmake/cmake-cookbook/cmake-cookbook/chapter-01/recipe-04/cxx-example

Condition Option

	# use dependent option
	
	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	project(recipe-05 LANGUAGES CXX)
	
	# expose options to the user
	option(USE_LIBRARY "Compile sources into a library" OFF)
	message(STATUS "Compile sources into a library? ${USE_LIBRARY}")
	
	include(CMakeDependentOption)
	# second option depends on the value of the first
	cmake_dependent_option(
	  MAKE_STATIC_LIBRARY "Compile sources into a static library" OFF
	  "USE_LIBRARY" ON
	  )
	# third option depends on the value of the first
	cmake_dependent_option(
	  MAKE_SHARED_LIBRARY "Compile sources into a shared library" ON
	  "USE_LIBRARY" ON
	  )
	
	set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
	
	# list sources
	list(APPEND _sources Message.hpp Message.cpp)
	
	if(USE_LIBRARY)
	  message(STATUS "Compile sources into a STATIC library? ${MAKE_STATIC_LIBRARY}")
	  message(STATUS "Compile sources into a SHARED library? ${MAKE_SHARED_LIBRARY}")
	
	  if(MAKE_SHARED_LIBRARY)
	    add_library(message SHARED ${_sources})
	    add_executable(hello-world hello-world.cpp)
	    target_link_libraries(hello-world message)
	  endif()
	
	  if(MAKE_STATIC_LIBRARY)
	    add_library(message STATIC ${_sources})
	    add_executable(hello-world hello-world.cpp)
	    target_link_libraries(hello-world message)
	  endif()
	  
	else()
	  add_executable(hello-world hello-world.cpp ${_sources})
	endif()

#log1
	-- The CXX compiler identification is GNU 9.3.0
	-- Check for working CXX compiler: /usr/bin/c++
	-- Check for working CXX compiler: /usr/bin/c++ -- works
	-- Detecting CXX compiler ABI info
	-- Detecting CXX compiler ABI info - done
	-- Detecting CXX compile features
	-- Detecting CXX compile features - done
	-- Compile sources into a library? OFF
	-- Configuring done
	-- Generating done
	-- Build files have been written to: /home/jianleya/trainning/cmake/cmake/cmake-cookbook/cmake-cookbook

#log2
	-- The CXX compiler identification is GNU 9.3.0
	-- Check for working CXX compiler: /usr/bin/c++
	-- Check for working CXX compiler: /usr/bin/c++ -- works
	-- Detecting CXX compiler ABI info
	-- Detecting CXX compiler ABI info - done
	-- Detecting CXX compile features
	-- Detecting CXX compile features - done
	-- Compile sources into a library? ON
	-- Compile sources into a STATIC library? OFF
	-- Compile sources into a SHARED library? ON
	-- Configuring done
	-- Generating done
	-- Build files have been written to: /home/jianleya/trainning/cmake/cmake/cmake-cookbook/cmake-cookbook/chapter-01/recipe-05/cxx-example

Source flow control

	# set build flags for each souce file
	
	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	project(recipe-10 LANGUAGES CXX)
	
	add_library(geometry
	  STATIC
	    geometry_circle.cpp
	    geometry_circle.hpp
	    geometry_polygon.cpp
	    geometry_polygon.hpp
	    geometry_rhombus.cpp
	    geometry_rhombus.hpp
	    geometry_square.cpp
	    geometry_square.hpp
	  )
	
	# we wish to compile the library with the optimization flag: -O3
	target_compile_options(geometry
	  PRIVATE
	    -O3
	  )
	
	list(
	  APPEND sources_with_lower_optimization
	    geometry_circle.cpp
	    geometry_rhombus.cpp
	  )
	
	# we use the IN LISTS foreach syntax to set source properties
	message(STATUS "Setting source properties using IN LISTS syntax:")
	foreach(_source IN LISTS sources_with_lower_optimization)
	  set_source_files_properties(${_source} PROPERTIES COMPILE_FLAGS -O2)
	  message(STATUS "Appending -O2 flag for ${_source}")
	endforeach()
	
	# we demonstrate the plain foreach syntax to query source properties
	# which requires to expand the contents of the variable
	message(STATUS "Querying sources properties using plain syntax:")
	foreach(_source ${sources_with_lower_optimization})
	  get_source_file_property(_flags ${_source} COMPILE_FLAGS)
	  message(STATUS "Source ${_source} has the following extra COMPILE_FLAGS: ${_flags}")
	endforeach()
	
	add_executable(compute-areas compute-areas.cpp)
	
	target_link_libraries(compute-areas geometry)
	
#log
	$ cmake .
	-- The CXX compiler identification is GNU 9.3.0
	-- Check for working CXX compiler: /usr/bin/c++
	-- Check for working CXX compiler: /usr/bin/c++ -- works
	-- Detecting CXX compiler ABI info
	-- Detecting CXX compiler ABI info - done
	-- Detecting CXX compile features
	-- Detecting CXX compile features - done
	-- Setting source properties using IN LISTS syntax:
	-- Appending -O2 flag for geometry_circle.cpp
	-- Appending -O2 flag for geometry_rhombus.cpp
	-- Querying sources properties using plain syntax:
	-- Source geometry_circle.cpp has the following extra COMPILE_FLAGS: -O2
	-- Source geometry_rhombus.cpp has the following extra COMPILE_FLAGS: -O2
	-- Configuring done
	-- Generating done
	-- Build files have been written to: /home/jianleya/trainning/cmake/cmake/cmake-cookbook/cmake-cookbook/chapter-01/recipe-10/cxx-example

CMake编译时FFMPEG一直显示Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.26100. CMake Warning (dev) at D:/CMake/share/cmake-4.0/Modules/Platform/Windows-MSVC.cmake:556 (enable_language): project() should be called prior to this enable_language() call. Call Stack (most recent call first): D:/CMake/share/cmake-4.0/Modules/Platform/Windows-MSVC.cmake:529 (__windows_compiler_msvc_enable_rc) D:/CMake/share/cmake-4.0/Modules/Platform/Windows-MSVC-CXX.cmake:6 (__windows_compiler_msvc) D:/CMake/share/cmake-4.0/Modules/CMakeCXXInformation.cmake:48 (include) CMakeLists.txt:107 (enable_language) This warning is for project developers. Use -Wno-dev to suppress it. CMake Warning (dev) at D:/CMake/share/cmake-4.0/Modules/Platform/Windows-MSVC.cmake:556 (enable_language): project() should be called prior to this enable_language() call. Call Stack (most recent call first): D:/CMake/share/cmake-4.0/Modules/Platform/Windows-MSVC.cmake:529 (__windows_compiler_msvc_enable_rc) D:/CMake/share/cmake-4.0/Modules/Platform/Windows-MSVC-C.cmake:5 (__windows_compiler_msvc) D:/CMake/share/cmake-4.0/Modules/CMakeCInformation.cmake:48 (include) CMakeLists.txt:107 (enable_language) This warning is for project developers. Use -Wno-dev to suppress it. Detected processor: AMD64 CMake Warning at cmake/OpenCVDetectCXXCompiler.cmake:177 (message): OpenCV does not recognize MSVC_VERSION "1943". Cannot set OpenCV_RUNTIME Call Stack (most recent call first): CMakeLists.txt:161 (include) CMake Warning at cmake/OpenCVUtils.cmake:758 (message): Unexpected option: WITH_CUFFT (=ON) Condition: IF (WITH_CUDA) Call Stack (most recent call first): CMakeLists.txt:251 (OCV_OPTION) CMake Warning at cmake/OpenCVUtils.cmake:758 (message): Unexpected option: WITH_CUBLAS (=ON) Condition: IF (WITH_CUDA) Call Stack (most recent call first): CMakeLists.txt:254 (OCV_OPTION) CMake Warning at cmake/OpenCVUtils.cmake:758 (message): Unexpected option: WITH_CUDNN (=ON) Condition: IF (WITH_CUDA) Call Stack (most recent call first): CMakeLists.txt:257 (OCV_OPTION)0K
05-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值