在 cmake 构建中使用外部 DLL

发布于 2024-11-01 22:53:54 字数 924 浏览 2 评论 0原文

我正在为我的项目编写 cmake 脚本,但遇到了一个问题:

我的项目使用第 3 方库 (FreeImage),它有自己的基于 Makefile 的构建系统。我可以通过简单地运行“make”(我使用的是 gnuwin32)来构建 FreeImage,它将使用 MinGW 构建 FreeImage 并生成:

FreeImage.lib
FreeImage.dll

现在我的问题是双重的:

  1. 我想从我的 cmake 脚本执行“make”。
  2. 我想链接到导入库 (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:

  1. I want to execute "make" from my cmake script.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百合的盛世恋 2024-11-08 22:53:54

在我看来,有两种选择:

如果您已将 FreeImage 源代码放入项目的源代码树中,最简单的选择可能是使用 execute_process() 命令。假设 FreeImage 位于项目的源代码树中的“3rdparty/FreeImage/”中,您可以执行类似的操作,

execute_process( COMMAND make WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/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,

execute_process( COMMAND make WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/3rdParty/FreeImage )

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文