UsewxWidgets

此模块作为使用 wxWidgets 库(以前称为 wxWindows)的便捷封装器,并将其使用需求(如库目录、包含目录和编译器标志)传播到当前目录作用域,供目标使用。

在 CMake 项目中加载此模块,使用

include(UsewxWidgets)

此模块在调用 find_package(wxWidgets) 后,调用 include_directories()link_directories(),为当前目录设置编译定义,并追加一些编译标志以使用 wxWidgets 库。

示例

在找到 wxWidgets 后,在项目中包含此模块以配置其使用需求。

CMakeLists.txt
# Note that for MinGW users the order of libraries is important.
find_package(wxWidgets COMPONENTS net gl core base)

add_library(example example.cxx)

if(wxWidgets_FOUND)
  include(UsewxWidgets)

  # Link wxWidgets libraries for each dependent executable/library target.
  target_link_libraries(example PRIVATE ${wxWidgets_LIBRARIES})
endif()

截至 CMake 3.27,一种更好的方法是仅将 wxWidgets::wxWidgets 导入的目标链接到需要它的特定目标,而不是包含此模块。导入的目标通过仅将包的使用属性(如包含目录和编译标志)应用于它们链接到的目标,从而提供更好的控制,避免不必要地传播到当前目录中的所有目标。

CMakeLists.txt
find_package(wxWidgets COMPONENTS net gl core base)

add_library(example example.cxx)

# Link the imported target for each dependent executable/library target.
target_link_libraries(example PRIVATE wxWidgets::wxWidgets)

另请参阅