在 cmake 构建中使用外部 DLL
我正在为我的项目编写 cmake 脚本,但遇到了一个问题:
我的项目使用第 3 方库 (FreeImage),它有自己的基于 Makefile 的构建系统。我可以通过简单地运行“make”(我使用的是 gnuwin32)来构建 FreeImage,它将使用 MinGW 构建 FreeImage 并生成:
FreeImage.lib
FreeImage.dll
现在我的问题是双重的:
- 我想从我的 cmake 脚本执行“make”。
- 我想链接到导入库 (FreeImage.lib),并确保 DLL 被复制到正确的位置,以便 EXE 能够运行。
我知道如何链接到 LIB 文件,但我不知道其余的事情。
文件夹结构如下:
MyProject # main directory
MyProject/Libs/FreeImage # FreeImage root directory
MyProject/Libs/FreeImage/Dist # This is where FreeImage outputs go (LIB and DLL)
BTW:我在 Windows 7 上运行。我计划使用 MSVC 和 MinGW 构建我的项目。
谢谢!
编辑: 我现在尝试像这样使用ExternalProject_Add:
ExternalProject_Add(
FreeImage
PREFIX ./Libs/FreeImage
URL ./Libs/FreeImage
BUILD_COMMAND make
)
这让我完成了其中的一部分,但并不完全起作用...它尝试为我配置一些东西并尝试使用nmake...呃
I'm working on the cmake scripts for my project and I've run into a problem:
My project uses a 3rd party library (FreeImage), which has its own Makefile-based build system. I can build FreeImage just fine by simply running "make" (I'm using gnuwin32), which will build FreeImage using MinGW and produce:
FreeImage.lib
FreeImage.dll
Now my problem is twofold:
- I want to execute "make" from my cmake script.
- I want to link to the import lib (FreeImage.lib), and also make sure the DLL gets copied to the correct place so the EXE will run.
I know how to link to the LIB file, but I'm lost on the rest.
The folder structure is like this:
MyProject # main directory
MyProject/Libs/FreeImage # FreeImage root directory
MyProject/Libs/FreeImage/Dist # This is where FreeImage outputs go (LIB and DLL)
BTW: I'm running on Windows 7. I plan to build my project both with MSVC and MinGW.
Thanks!
EDIT:
I'm now trying to use ExternalProject_Add like so:
ExternalProject_Add(
FreeImage
PREFIX ./Libs/FreeImage
URL ./Libs/FreeImage
BUILD_COMMAND make
)
This gets me part of the way there, but doesn't totally work... it tries to configure things for me and tries to use nmake... ugh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,有两种选择:
如果您已将 FreeImage 源代码放入项目的源代码树中,最简单的选择可能是使用 execute_process() 命令。假设 FreeImage 位于项目的源代码树中的“3rdparty/FreeImage/”中,您可以执行类似的操作,
或者,您可以将 dll 从 3rdParty/FreeImage/bin 复制到您自己的 bin 目录中。然后你可以编写一个 FreeImageConfig.cmake 来导入库:
add_library( FreeImage IMPORTED )
set_target_properties( FreeImage 属性 IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/3rdParty/FreeImage/lib )
...
另一个选项是使用 ExternalProject 模块。您还可以查看 Kitware 的这篇文章,了解以下内容的概述这个模块。本质上,您指定获取源代码、配置构建、构建源代码并安装它所需的完整命令链。全部在您自己的 CMakeLists.txt 中
In my opinion, there are two options:
In case you have put your FreeImage sources in your projects' source-tree, the easiest option may be to use the execute_process() command. Assuming FreeImage is in your projects' source-tree in "3rdparty/FreeImage/" you can do something like,
Optionally, you can copy the dll from 3rdParty/FreeImage/bin into you own bin directory. And then you can write a FreeImageConfig.cmake for importing the library:
add_library( FreeImage IMPORTED )
set_target_properties( FreeImage PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/3rdParty/FreeImage/lib )
...
The other option is to make use of the ExternalProject module. You can also take a look at this article from Kitware for an overview of this module. In essence, you specify the full chain of commands needed to get the source, configure the build, build the source and install it. All in your own CMakeLists.txt