使用 CMAKE 与 MSYS/MinGW 使用 DevIL 图像库链接 dll 文件时出现的问题

发布于 2024-09-15 13:47:31 字数 2757 浏览 2 评论 0原文

实际上,我创建了一个 CMake 脚本来在 win32 平台上使用 MSYS/MinGW 本地化 DevIL 库。我已经扩展了原始 FindDevIL CMake 脚本,以在尝试查找 DevIL 时考虑 MinGW 根:

project (DevILTest)
cmake_minimum_required(VERSION 2.6)

FIND_PACKAGE(OpenGL)
IF(OPENGL_FOUND)
  MESSAGE(STATUS "OpenGL render API found.")
  MESSAGE(STATUS "Detected OpenGL path is : ${OPENGL_LIBRARIES}")   
ENDIF(OPENGL_FOUND)

#Determine DevIL specific header file           
FIND_PATH(IL_INCLUDE_DIR il.h 
      PATH_SUFFIXES include/IL
      PATHS c:/mingw
      DOC "The path the the directory that contains il.h"
)
MESSAGE("Found DevIL includes at: ${IL_INCLUDE_DIR}")

#Determine DevIL library
FIND_LIBRARY(IL_LIBRARY NAMES DEVIL
     PATH_SUFFIXES lib 
     PATHS c:/mingw
     DOC "The file that corresponds to the base il library."
)
MESSAGE("Found DevIL library at: ${IL_LIBRARY}")

SET (Sources
winmain.cpp
)

SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(IL_INCLUDE_DIR /c/binrev/development/mingw/include)

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} 
             ${CMAKE_CURRENT_SOURCE_DIR}/src 
             ${IL_INCLUDE_DIR}
             /usr/include 
             /usr/local/include               
)

add_executable (DevILTest ${Sources})
SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR})
TARGET_LINK_LIBRARIES(DevILTest ${OPENGL_LIBRARIES} ${IL_LIBRARY})

DevIL .dll 文件放置在 bin 子文件夹中,DevIL.lib 和 DevIL.a 文件放置在 MinGW 根的 lib 子文件夹中(c: /明格)。我使用 find_library() 来检查库是否存在。

-- OpenGL render API found.
-- Detected OpenGL path is : glu32;opengl32
-- Found DevIL includes at: C:/mingw/include/IL
-- Found DevIL library at: C:/mingw/bin/DevIL.dll

Find_libary() 始终返回 dll 文件的路径 - 但我无法使用 MinGW 链接到该库:

Linking CXX executable DevILTest.exe
/C/binrev/development/mingw/bin/g++.exe     "CMakeFiles/DevILTest.dir  /winmain.cpp.obj"   -o DevILTest.exe -Wl,--out-implib,libDevILTest.dll.a -Wl,--ma
32 -lopengl32 DevIL.dll -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
CMakeFiles/DevILTest.dir/winmain.cpp.obj:winmain.cpp:(.text+0xcc8): undefined reference to `_imp__ilOriginFunc@4'
CMakeFiles/DevILTest.dir/winmain.cpp.obj:winmain.cpp:(.text+0xce0): undefined reference to `_imp__ilEnable@4'
CMakeFiles/DevILTest.dir/winmain.cpp.obj:winmain.cpp:(.text+0xcf9): undefined reference to `_imp__ilSetInteger@8'

如果我删除 dll 文件并运行 cmake skript,则 DevIL.lib 库已正确本地化,并且不会出现链接器失败:

-- Detected OpenGL path is : glu32;opengl32
-- Found DevIL includes at: C:/mingw/include/IL
-- Found DevIL library at: C:/mingw/lib/DevIL.lib

但是在这种情况下,应用程序在启动时崩溃,同时缺少 dll 文件。如果我现在将它们添加回 mingw 或应用程序根目录,应用程序将运行,但在本地化 dll 文件而不是 .lib 时,cmake 执行再次失败...

有人知道我如何解决这个问题吗? 我将非常感谢任何提示。 此致, 基督教

actually i create a CMake script to localize the DevIL library using MSYS/MinGW on win32 platform. I've extend the origin FindDevIL CMake script to regard the MinGW root when try to find DevIL:

project (DevILTest)
cmake_minimum_required(VERSION 2.6)

FIND_PACKAGE(OpenGL)
IF(OPENGL_FOUND)
  MESSAGE(STATUS "OpenGL render API found.")
  MESSAGE(STATUS "Detected OpenGL path is : ${OPENGL_LIBRARIES}")   
ENDIF(OPENGL_FOUND)

#Determine DevIL specific header file           
FIND_PATH(IL_INCLUDE_DIR il.h 
      PATH_SUFFIXES include/IL
      PATHS c:/mingw
      DOC "The path the the directory that contains il.h"
)
MESSAGE("Found DevIL includes at: ${IL_INCLUDE_DIR}")

#Determine DevIL library
FIND_LIBRARY(IL_LIBRARY NAMES DEVIL
     PATH_SUFFIXES lib 
     PATHS c:/mingw
     DOC "The file that corresponds to the base il library."
)
MESSAGE("Found DevIL library at: ${IL_LIBRARY}")

SET (Sources
winmain.cpp
)

SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(IL_INCLUDE_DIR /c/binrev/development/mingw/include)

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} 
             ${CMAKE_CURRENT_SOURCE_DIR}/src 
             ${IL_INCLUDE_DIR}
             /usr/include 
             /usr/local/include               
)

add_executable (DevILTest ${Sources})
SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR})
TARGET_LINK_LIBRARIES(DevILTest ${OPENGL_LIBRARIES} ${IL_LIBRARY})

The DevIL .dll files are placed in bin subfolder, the DevIL.lib and DevIL.a files in the lib subfolder of MinGW root (c:/mingw). I use find_library() to check if a library exists.

-- OpenGL render API found.
-- Detected OpenGL path is : glu32;opengl32
-- Found DevIL includes at: C:/mingw/include/IL
-- Found DevIL library at: C:/mingw/bin/DevIL.dll

Find_libary() returns allways the path to the dll files - but I can't link against this library using MinGW:

Linking CXX executable DevILTest.exe
/C/binrev/development/mingw/bin/g++.exe     "CMakeFiles/DevILTest.dir  /winmain.cpp.obj"   -o DevILTest.exe -Wl,--out-implib,libDevILTest.dll.a -Wl,--ma
32 -lopengl32 DevIL.dll -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
CMakeFiles/DevILTest.dir/winmain.cpp.obj:winmain.cpp:(.text+0xcc8): undefined reference to `_imp__ilOriginFunc@4'
CMakeFiles/DevILTest.dir/winmain.cpp.obj:winmain.cpp:(.text+0xce0): undefined reference to `_imp__ilEnable@4'
CMakeFiles/DevILTest.dir/winmain.cpp.obj:winmain.cpp:(.text+0xcf9): undefined reference to `_imp__ilSetInteger@8'

If i remove the dll files and run the cmake skript the DevIL.lib library is localized correctly and i go no linker failures:

-- Detected OpenGL path is : glu32;opengl32
-- Found DevIL includes at: C:/mingw/include/IL
-- Found DevIL library at: C:/mingw/lib/DevIL.lib

But when in this case the applicaton crashes on start up while missing the dll files. If i add them now back to mingw or application root the application runs, but fails again on cmake execution while localize the dll files instead of .lib ...

Has anyone an idea how i could solve this issue?
I would be deeply gratefull for any hint.
Best regards,
Christian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

老街孤人 2024-09-22 13:47:31

这个问题就解决了。我必须在我的 CMake 脚本中搜索库的完整名称,即 libDevIL.lib。在这种情况下,可以正确找到库:

#Determine DevIL library
FIND_LIBRARY(IL_LIBRARY NAMES libDEVIL.lib
     PATH_SUFFIXES lib 
     PATHS c:/mingw
     DOC "The file that corresponds to the base il library."
)

This problem is solved. I've to search for the complete name of the library, i.e. libDevIL.lib in my CMake script. In this case the library is found correctly:

#Determine DevIL library
FIND_LIBRARY(IL_LIBRARY NAMES libDEVIL.lib
     PATH_SUFFIXES lib 
     PATHS c:/mingw
     DOC "The file that corresponds to the base il library."
)
女中豪杰 2024-09-22 13:47:31

将其放入项目的根 cmake 文件中,首先查找 .lib 库,然后才查找 .dll 。

IF(WIN32)
   SET(CMAKE_FIND_LIBRARY_SUFFIXES .lib .dll)
ELSE()
   SET(CMAKE_FIND_LIBRARY_SUFFIXES .a)
ENDIF()

我猜他们这样做是为了让一些人免于二进制不兼容问题,因为 mingw 生成的 dll 可以在没有 .lib 文件的情况下被 mingw 使用。不知道为什么这不是默认行为。

put this into your project's root cmake file, to find first .lib libraries and only then .dll .

IF(WIN32)
   SET(CMAKE_FIND_LIBRARY_SUFFIXES .lib .dll)
ELSE()
   SET(CMAKE_FIND_LIBRARY_SUFFIXES .a)
ENDIF()

I guess they did it to save some people from binary incompatibility issues, as mingw generated dlls can be used by mingw without .lib files. No other idea why this is not the default behavior.

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