Linker error when trying to use ImageMagick as static libraries with Visual Studio compiler

微笑、不失礼 提交于 2021-02-08 10:12:55

问题


This question has already been asked here, but since I have many more details to provide, I think it is best to start with a new question.

I have trouble linking the ImageMagick lib files in my program that uses the Visual Studio MSVC 2015 compiler. Here are the steps I have followed:

  • I have compiled the static MT runtimes by using the "configure.exe" utility and built in Visual Studio the solution it generated, "VisualStaticMT.sln". This creates lib files such as "CORE_RL_Magick++_.lib" in C:\ImageMagick-6.9.3-2\VisualMagick\lib.

  • In my C++11 program, the configuration relevant to ImageMagick is (I use QtCreator, but with the MSVC2015 compiler):

    INCLUDEPATH += \
        C:\ImageMagick-6.9.3-2\ImageMagick\Magick++\lib \
        C:\ImageMagick-6.9.3-2\ImageMagick
    LIBS += \
        -lC:\ImageMagick-6.9.3-2\VisualMagick\lib\CORE_RL_Magick++_ \
        -lC:\ImageMagick-6.9.3-2\VisualMagick\lib\CORE_RL_wand_ \
        -lC:\ImageMagick-6.9.3-2\VisualMagick\lib\CORE_RL_magick_
    QMAKE_CXXFLAGS += \
        -DMAGICKCORE_HDRI_ENABLE=0 \
        -DMAGICKCORE_QUANTUM_DEPTH=16
    

A piece of the program contains:

#include <Magick++.h>
...
Magick::Image img;
img = Magick::Image(filename);

When compiling I get link errors:

movie.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall Magick::Image::Image(void)" (__imp_??0Image@Magick@@QAE@XZ) referenced in function "public: __thiscall Frame<unsigned char>::Frame<unsigned char>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0?$Frame@E@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)

movie.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall Magick::Image::Image(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (__imp_??0Image@Magick@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall Frame<unsigned char>::Frame<unsigned char>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0?$Frame@E@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)

movie.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall Magick::Image::~Image(void)" (__imp_??1Image@Magick@@UAE@XZ) referenced in function "public: __thiscall Frame<unsigned char>::Frame<unsigned char>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0?$Frame@E@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)

movie.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class Magick::Image & __thiscall Magick::Image::operator=(class Magick::Image const &)" (__imp_??4Image@Magick@@QAEAAV01@ABV01@@Z) referenced in function "public: __thiscall Frame<unsigned char>::Frame<unsigned char>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0?$Frame@E@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)

movie.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall Magick::Image::write(long,long,unsigned int,unsigned int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,enum MagickCore::StorageType,void *)" (__imp_?write@Image@Magick@@QAEXJJIIABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@W4StorageType@MagickCore@@PAX@Z) referenced in function "public: __thiscall Frame<unsigned char>::Frame<unsigned char>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0?$Frame@E@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)

movie.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: struct MagickCore::_Image const * __thiscall Magick::Image::constImage(void)const " (__imp_?constImage@Image@Magick@@QBEPBU_Image@MagickCore@@XZ) referenced in function "public: __thiscall Frame<unsigned char>::Frame<unsigned char>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0?$Frame@E@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
release\corr.exe : fatal error LNK1120: 6 unresolved externals

The "dllimport" error suprises me, as I think there should be no DLL involved with the "static MT runtimes" I am using.

Any idea on how I could solve the problem?

Thanks.


EDIT

A few more things I have tried:

  • Linking the libraires with the lines below in main.cpp gave the same errors:

    #pragma comment(lib, "CORE_RL_Magick++_.lib")
    #pragma comment(lib, "CORE_RL_wand_.lib")
    #pragma comment(lib, "CORE_RL_magick_.lib")
    
  • I tried to add all the 27 lib files located in C:\ImageMagick-6.9.3-2\VisualMagick\lib. If I exclude CORE_RL_exr_.lib, I get the same error. If I include it, I get additional errors that suggest this file should not be added:

    CORE_RL_exr_.lib(IexBaseExc.obj):-1: error: LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease' in main.obj
    

回答1:


probably you are missing some library files. because your program is unable to link library file that's why it gives linker error. Try to link lib file externally .. try this code before main method.. #pragma comment(lib, "CORE_DB_magick++.lib")

you can download this lib file from given below link.

https://github.com/moravianlibrary/Open-DPI-Detector/tree/master/OpenDpiDetector/Libraries/ImageMagick-6.7.6/VisualMagick/lib




回答2:


To be able to link your app with static build of ImageMagick you have to do couple of tricks (as for ImageMagick-7.0.8-11):

  • add preprocessor definition STATIC_MAGICK to your visual studio project
  • add missing input library CORE_DB_croco_.lib/CORE_RL_croco_.lib in the Linker->Input (other libs ImageMagick adds automatically via #pragma)
  • rename CORE_XX_liblzma_.pdb -> CORE_XX_lzma_.lib (ImageMagick links this lib with other name, probably bug), XX = RL or DB



回答3:


The answer to linking the ImageMagick static libraries is to ensure you link all the dependant static libraries as well!

Once you have compiled the solution for ImageMagick static libraries, go to the 'VisualMagick' folder (within the ImageMagick cloned repository) then to to the 'lib' folder there you will see all the *_DB_*.lib and *_RL_*.lib files for Debug and Release.

You need to include those names in the 'Additional Dependencies' section for both Release and Debug.

Another key aspect is to ensure the libraries are built by the same compiler for the same platform architecture as your own application .. (WIN32 or x64).



来源:https://stackoverflow.com/questions/36639667/linker-error-when-trying-to-use-imagemagick-as-static-libraries-with-visual-stud

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!