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

时间:2010-08-21 12:20:08

标签: winapi mingw cmake devil

实际上我创建了一个CMake脚本,用于在win32平台上使用MSYS / MinGW本地化DevIL库。在尝试查找DevIL时,我扩展了原始FindDevIL CMake脚本以考虑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子文件夹中,即MinGW根目录(c:/ mingw)的lib子文件夹中的DevIL.lib和DevIL.a文件。我使用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或应用程序root应用程序运行,但在cmake执行时再次失败,同时本地化dll文件而不是.lib ...

有谁知道如何解决这个问题? 我会非常感激任何提示。 最好的祝福, 基督教

2 个答案:

答案 0 :(得分:0)

此问题已解决。我要搜索库的完整名称,即我的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."
)

答案 1 :(得分:0)

将它放入项目的根cmake文件中,找到第一个.lib库,然后才能找到.dll。

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

我猜他们这样做是为了节省一些人的二进制不兼容问题,因为mingw生成的dll可以被没有.lib文件的mingw使用。没有其他想法为什么这不是默认行为。