cmakefile 里面的 POLICY_SCOPE
时间: 2023-11-14 10:52:35 浏览: 27
POLICY_SCOPE 是 CMake 中的一个命令,用于在指定的作用域内设置策略。在 CMake 中,策略是一种指定 CMake 行为的方式,可以通过 POLICY 命令来指定。POLICY_SCOPE 命令的作用是在指定的作用域内设置策略。它接受一个作用域参数,该参数可以是 GLOBAL、DIRECTORY 或者 FUNCTION。如果指定了 GLOBAL 作用域,则策略将在全局范围内设置;如果指定了 DIRECTORY 作用域,则策略将在当前目录作用域内设置;如果指定了 FUNCTION 作用域,则策略将在当前函数作用域内设置。POLICY_SCOPE 命令可以用于在不同的作用域中设置不同的策略,以满足不同的需求。
相关问题
# toplevel CMakeLists.txt for a catkin workspace # catkin/cmake/toplevel.cmake cmake_minimum_required(VERSION 3.0.2) project(Project) set(CATKIN_TOPLEVEL TRUE) # search for catkin within the workspace set(_cmd "catkin_find_pkg" "catkin" "${CMAKE_SOURCE_DIR}") execute_process(COMMAND ${_cmd} RESULT_VARIABLE _res OUTPUT_VARIABLE _out ERROR_VARIABLE _err OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE ) if(NOT _res EQUAL 0 AND NOT _res EQUAL 2) # searching fot catkin resulted in an error string(REPLACE ";" " " _cmd_str "${_cmd}") message(FATAL_ERROR "Search for 'catkin' in workspace failed (${_cmd_str}): ${_err}") endif() # include catkin from workspace or via find_package() if(_res EQUAL 0) set(catkin_EXTRAS_DIR "${CMAKE_SOURCE_DIR}/${_out}/cmake") # include all.cmake without add_subdirectory to let it operate in same scope include(${catkin_EXTRAS_DIR}/all.cmake NO_POLICY_SCOPE) add_subdirectory("${_out}") else() # use either CMAKE_PREFIX_PATH explicitly passed to CMake as a command line argument # or CMAKE_PREFIX_PATH from the environment if(NOT DEFINED CMAKE_PREFIX_PATH) if(NOT "$ENV{CMAKE_PREFIX_PATH}" STREQUAL "") if(NOT WIN32) string(REPLACE ":" ";" CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH}) else() set(CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH}) endif() endif() endif() # list of catkin workspaces set(catkin_search_path "") foreach(path ${CMAKE_PREFIX_PATH}) if(EXISTS "${path}/.catkin") list(FIND catkin_search_path ${path} _index) if(_index EQUAL -1) list(APPEND catkin_search_path ${path}) endif() endif() endforeach() # search for catkin in all workspaces set(CATKIN_TOPLEVEL_FIND_PACKAGE TRUE) find_package(catkin QUIET NO_POLICY_SCOPE PATHS ${catkin_search_path} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH) unset(CATKIN_TOPLEVEL_FIND_PACKAGE) if(NOT catkin_FOUND) message(FATAL_ERROR "find_package(catkin) failed. catkin was neither found in the workspace nor in the CMAKE_PREFIX_PATH. One reason may be that no ROS setup.sh was sourced before.") endif() endif() catkin_workspace()在哪添加target_link_libraries(your_target_name avcodec avformat swscale)
在您的 CMakeLists.txt 文件中,将 `target_link_libraries()` 指令添加到 `add_subdirectory("${_out}")` 的下方。具体位置如下所示:
```cmake
...
# include catkin from workspace or via find_package()
if(_res EQUAL 0)
set(catkin_EXTRAS_DIR "${CMAKE_SOURCE_DIR}/${_out}/cmake")
# include all.cmake without add_subdirectory to let it operate in same scope
include(${catkin_EXTRAS_DIR}/all.cmake NO_POLICY_SCOPE)
add_subdirectory("${_out}")
# Add the target_link_libraries() here
target_link_libraries(your_target_name avcodec avformat swscale)
else
# use either CMAKE_PREFIX_PATH explicitly passed to CMake as a command line argument
# or CMAKE_PREFIX_PATH from the environment
if(NOT DEFINED CMAKE_PREFIX_PATH)
if(NOT "$ENV{CMAKE_PREFIX_PATH}" STREQUAL "")
if(NOT WIN32)
string(REPLACE ":" ";" CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH})
else()
set(CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH})
endif()
endif()
endif()
...
endif()
...
```
请注意将 `your_target_name` 替换为您实际的目标名称。这将在生成目标时链接 avcodec、avformat 和 swscale 库。
如果您还有其他问题,请随时提问。
cmakefile 里面的 include
在 CMake 中,`include` 命令用于包含其他 CMake 脚本文件,这些文件可以是相对或绝对路径。`include` 命令的语法如下:
```
include(file [OPTIONAL] [RESULT_VARIABLE var] [NO_POLICY_SCOPE])
```
其中,`file` 是要包含的 CMake 脚本文件名;`OPTIONAL` 表示如果文件不存在,不会产生错误;`RESULT_VARIABLE var` 可以将包含的文件的结果存储到 CMake 变量 `var` 中;`NO_POLICY_SCOPE` 表示该命令不受 POLICY_SCOPE 的限制。
一般来说,`include` 命令用于将一些通用的 CMake 模块文件包含到项目中,这些模块可能提供了一些常用的函数、宏定义或变量定义等。例如:
```
include(FindPackageHandleStandardArgs)
```
这条命令会将 `FindPackageHandleStandardArgs.cmake` 文件包含到当前 CMakeLists.txt 中,以便在项目中使用该文件中定义的函数。