cmake编译安装wxWidgets并使用
- 从 wxWidgets 的 git 仓库克隆最新代码
- 用 cmake 编译安装 wxWidgets 静态库
- 在个人项目中使用 wxWidgets,使用 config 文件和 find_package() 调用 wxWidgets 依赖
- 将 wxWidgets 库文件链接到个人项目的 目标(target)
从 wxWidgets 的 git 仓库克隆最新代码
我将源码放在用户家目录顶层。
参考:wxWidgets/README-GIT.md at master · wxWidgets/wxWidgets (github.com)
用 cmake 编译安装 wxWidgets 静态库
安装至~/wx_install
,也就是家目录下的 wx_install
文件夹。
参考:源码目录下wxWidgets/docs/doxygen/overviews/cmake.md
在个人项目中使用 wxWidgets
在 wxWidgets 的安装目录内,~/wx_install/lib/cmake/wxWidgets
下有自动生成的配置文件wxWidgetsConfig.cmake
和wxWidgetsConfigVersion.cmake
,用于帮助 CMake 能够正确地配置个人项目以使用 wxWidgets 库。以及在 ~/wx_install/lib/cmake/wxWidgets/vc_x64_lib
内有配置文件 wxWidgetsTargets.cmake
和wxWidgetsTargets-debug.cmake
,作用问gpt。
用于测试的小项目来自 wxWidgets 自带示例 wxWidgets/samples/minimal
.
minimal.cpp
编译项目需要文件CMakeLists.txt
。
导入包 wxWidgets
我认为这类似于 python 代码的 import 语句。让编译器能找到 wxWidgets 的库代码。
要让项目包含 wxWidgets 的代码需使用 find_package()
函数,像这样:
这里指定了 wxWidgets 名称、版本号,以及各个组件的名称,以及指定了 CONFIG
,表示 CMake 将使用配置模式(Config mode),也就是 CMake 会尝试调用 wxWidgetsConfig.cmake
文件,来配置 wxWidgets 库。
但是只有上面那一句还不够,因为 CMake 不知道要去哪里寻找wxWidgetsConfig.cmake
。所以,在 CMakeLists.txt
文件中,应该将下面一句代码放在 find_package()
前面:
有关于此的更详细解释参见:使用依赖项指南 Using Dependencies Guide — CMake 3.29.0-rc3 Documentation
Packages will not be found automatically without help if they are in locations not known to CMake, such as
/opt/mylib
or$HOME/dev/prefix
. This is a normal situation, and CMake provides several ways for users to specify where to find such libraries.
如果包位于 CMake 未知的位置(例如/opt/mylib
或$HOME/dev/prefix
),则在没有帮助的情况下不会自动找到包。这是正常情况,CMake 提供了多种方法供用户指定在哪里找到此类库。The
CMAKE_PREFIX_PATH
variable may be set when invoking CMake. It is treated as a list of base paths in which to search for config files. A package installed in/opt/somepackage
will typically install config files such as/opt/somepackage/lib/cmake/somePackage/SomePackageConfig.cmake
. In that case,/opt/somepackage
should be added toCMAKE_PREFIX_PATH
.
调用 CMake 时可以设置CMAKE_PREFIX_PATH
变量。它被视为在其中搜索配置文件的基本路径列表。安装在/opt/somepackage
中的包通常会安装配置文件,例如/opt/somepackage/lib/cmake/somePackage/SomePackageConfig.cmake
。在这种情况下,应将/opt/somepackage
添加到CMAKE_PREFIX_PATH
中。The environment variable
CMAKE_PREFIX_PATH
may also be populated with prefixes to search for packages. Like thePATH
environment variable, this is a list, but it needs to use the platform-specific environment variable list item separator (:
on Unix and;
on Windows).
环境变量CMAKE_PREFIX_PATH
也可以填充前缀以搜索包。与PATH
环境变量一样,这是一个列表,但它需要使用特定于平台的环境变量列表项分隔符(Unix 上使用:
以及 Windows 上使用;
)。The
CMAKE_PREFIX_PATH
variable provides convenience in cases where multiple prefixes need to be specified, or when multiple packages are available under the same prefix. Paths to packages may also be specified by setting variables matching<PackageName>_DIR
, such asSomePackage_DIR
. Note that this is not a prefix, but should be a full path to a directory containing a config-style package file, such as/opt/somepackage/lib/cmake/SomePackage
in the above example. See thefind_package()
documentation for other CMake variables and environment variables that can affect the search.
在需要指定多个前缀或同一前缀下有多个包时,CMAKE_PREFIX_PATH
变量提供了便利。包的路径也可以通过设置与<PackageName>_DIR
匹配的变量来指定,例如SomePackage_DIR
。请注意,这不是前缀,而应该是包含配置样式包文件的目录的完整路径,例如上例中的/opt/somepackage/lib/cmake/SomePackage
。有关可能影响搜索的其他 CMake 变量和环境变量,请参阅find_package()
文档。
将 wxWidgets 库文件链接到 目标(target)
现在,编译器能够找到 wxWidgets 的库代码了,接下来就是使用 wxWidgets 库代码了。需要让项目的目标(target,可能是可执行文件,也可能是库,前者更有可能)和 wxWidgets 库文件链接在一起。如下:
全 CMakeLists.txt
关键点就是上面的几个语句,但一个完整的 CMakeLists.txt
需要考虑的地方更多。如软件图标在各个平台上的差异,以及其他跨平台产生的问题。一个完整的 CMakeLists.txt
如下:
上述文件基本来自 wxWidgets 自带示例 wxWidgets/samples/minimal
。
编译
编译 wxWidgets 最小示例
上面的代码基本都来自 wxWidgets 自带示例 wxWidgets/samples/minimal
。如果想不加修改地编译原版的minimal
(可能是想要比较原版和个人项目),可以使用下面的命令:
来自:源码目录下wxWidgets/docs/doxygen/overviews/cmake.md
END
以上。