Link errors with GraphicsMagick - c++

I downloaded and compiled GraphicsMagick, 1.3.23, Q16, x64, StaticMT version. I had to convert the Visual Studio 7 solution generated by GraphicsMagick's build utility to Visual Studio 2015 format. I linked my project to CORE_DB_magick_.lib and CORE_DB_Magick++_.lib.
When the linker ran, it produced unresolved external symbols when linking InitializeMagick() and DestroyMagick()
1>wtd.lib(WebController.obj) : error LNK2019: unresolved external symbol __imp_DestroyMagick referenced in function "public: __cdecl Wt::WebController::~WebController(void)" (??1WebController#Wt##QEAA#XZ)
1>wtd.lib(WebController.obj) : error LNK2019: unresolved external symbol __imp_InitializeMagick referenced in function "public: __cdecl Wt::WebController::WebController(class Wt::WServer &,class std::basic_string,class std::allocator > const &,bool)" (??0WebController#Wt##QEAA#AEAVWServer#1#AEBV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##_N#Z)
I can't understand why the symbols are not being linked. Any ideas?

Apparently, GraphicsMagick Static versions do not link properly.

What is your project type? I had a similar problem when trying to link the GraphicsMagick libraries to a DLL.
The clue here is that __imp is the function decoration for DLL imports, so you're trying to link with DLL functions.
The problem is that the header magick/common.h, when linked to a DLL project, reads the current configuration of the Visual Studio pre-processor environment to determine which mode the library is in, which is obviously wrong if you're trying to link static libraries into your DLL, for example. In this case, it defines MagickExport to __declspec(dllimport).
AFAIK this is a bug in the library. For proper static build support, magick/common.h needs to do something like read information from the magick/magick_config.h to determine what mode the library was actually built in and define MagickExport appropriately.
Since your library is statically linked, you can fix this by commenting out everything in the define:
#if defined(MSWINDOWS) && !defined(__CYGWIN__)
and replacing it with:
#define MagickExport
#define ModuleExport
#define MagickGlobal

Related

Symbols in lib not being included

I have been updating a big working C++ Windows application in Visual Studio 2022 into several static libs.
The idea is to share part of the code to an external developer, and not share the complete C++ files of the project. Only part of the code would be shared, and the rest would be .lib's and .h's.
The structure of the entire solution is:
External LIB
Library 1 LIB - Referencing .h's from the External LIB
Application EXE - Using the above libs
When building the application EXE, the link fails with thousands of errors of missing symbols from the External LIB. "LNK2001 unresolved external symbol "__declspec(dllimport) public: bool __thiscall ..."
1>Common.lib(LocalizedString.obj) : error LNK2001: unresolved external symbol "__declspec(dllimport) public: bool __thiscall cocos2d::Value::isNull(void)const " (__imp_?isNull#Value#cocos2d##QBE_NXZ)
1> Hint on symbols that are defined and could potentially match:
1> "__declspec(dllimport) public: bool __thiscall cocos2d::Data::isNull(void)const " (__imp_?isNull#Data#cocos2d##QBE_NXZ)
The EXE is configured to include all the libs (configured in VS project settings: Additional Dependencies).
All the projects are being built with __cdecl (/Gd) and Multi-threaded Debug DLL (/MDd).
If I add to the EXE code some dummy declarations of symbols that are being referred into the "unresolved external symbol" error, Visual Studio adds those symbols and I can see the number of errors decreasing. But this is not a good solution...
It looks like somehow Visual Studios is adding all the symbols automatically.
I believe this whole issue may be related with the following. When building my libs I get the following "warning LNK4006: XXXX already defined in XXXXX.lib(xxhash.obj); second definition ignored".
I have tried different project settings, like "Link Library Dependencies: Yes". But nothing seems to fix it.
I'm stuck with this for two days... Can someone save me?
Found the problem. There was an hidden define in the external lib adding the dllimport to every external class.
#if defined(CC_STATIC)
#define CC_DLL
#else
#if defined(_USRDLL)
#define CC_DLL __declspec(dllexport)
#else /* use a DLL library */
#define CC_DLL __declspec(dllimport)
#endif
#endif

C++ - Unable to link in libyaml using Visual Studio 2019/CMake

Attempting to link in libyaml (0.2.5) using Visual Studio 2019 Enterprise and CMake, as I have a cross-platform (Linux/Windows) project that uses this library. According to the documentation, this library should work in Windows 10. It links in just fine on Linux (64-bit machine).
I'm compiling it as a static lib, and it has no issue generating the .lib file. I've copied it to the appropriate location that I'm linking in from my CMakelists.txt, as well as the header.
When I link it in from my project on Windows:
ts.lib(yamlparser.cxx.obj) : error LNK2019: unresolved external symbol __imp_yaml_parser_initialize referenced in function "public: bool __cdecl YAMLParser::parse_cfg(void)" (?parse_cfg#YAMLParser##QEAA_NXZ)
ts.lib(yamlparser.cxx.obj) : error LNK2019: unresolved external symbol __imp_yaml_parser_set_input_file referenced in function "public: bool __cdecl YAMLParser::parse_cfg(void)" (?parse_cfg#YAMLParser##QEAA_NXZ)
ts.lib(yamlparser.cxx.obj) : error LNK2019: unresolved external symbol __imp_yaml_parser_scan referenced in function "public: bool __cdecl YAMLParser::parse_cfg(void)" (?parse_cfg#YAMLParser##QEAA_NXZ)
This is what I'm doing on the CMake side - the very same thing I'm doing to link in my GoogleTest libraries on both Linux and Windows:
if (UNIX)
...
...
else()
set (MY_LIB_DIR "C:/mylib/lib")
set (MY_INCLUDE "C:/mylib/include")
set (YAML_LIB "${MY_LIB_DIR}/yaml.lib")
set (VCPKG_LIBRARY_LINKAGE static)
endif()
include_directories(${MY_INCLUDE})
add_library(ts
...
...
)
add_executable(myexec main.cxx)
target_link_libraries(myexec ts ${YAML_LIB})
VS isn't giving me many clues here except that there's a linkage problem that I can't quite figure out.
Any advice on how I can debug this or what the problem may be?
The __imp was the clue that my code was trying to include the library dynamically... Opening up yaml.h, I noticed that it tries to export from DLL unless YAML_DECLARE_STATIC is defined. Defined this in CMake and my issue is resolved.

How to build a static Clucene library to avoid any linker errors

I work with Windovs and I need to build a static CLucene library. I downloaded the latest source code and build them into Visual Studio 2010 Project through CMake-gui 3.4.3. When building I used a flag BUILD_STATIC_LIBRARIES = true. I successfully built two libraries: clucene-core-statics and clucene-shared-static. However, when I run the test project cl-test-static occurs a lot of linker errors. The same thing happens when I add these libraries in another test project.
Error Example:
Error 1 error LNK2019: unresolved external symbol
"__declspec(dllimport) public: static wchar_t * __cdecl
lucene::queryParser::QueryParser::escape(wchar_t const *)"
(__imp_?escape#QueryParser#queryParser#lucene##SAPA_WPB_W#Z)
referenced in function "public: void __thiscall Main::Start(void)"
(?Start#Main##QAEXXZ) c:\Users\user\documents\visual studio
2010\Projects\TestClucene\TestClucene\Main.obj TestClucene
Does anyone have any experience in building static CLucene libraries?

LNK2019: Undefined external symbol _Getgloballocale, Visual Studio 2013

I have the following linker error in one project of a Visual Studio 2013 solution:
error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class std::locale::_Locimp * __cdecl std::locale::_Getgloballocale(void)" (__imp_?_Getgloballocale#locale#std##SAPEAV_Locimp#12#XZ) referenced in function "class std::ctype<char> const & __cdecl std::use_facet<class std::ctype<char> >(class std::locale const &)" (??$use_facet#V?$ctype#D#std###std##YAAEBV?$ctype#D#0#AEBVlocale#0##Z)
In all projects I linked the run-time library dynamically (/MD).
I considered the hints in the following posts:
C++ linker unresolved external symbols
What libraries do I
need to link my mixed-mode application to?
Boost -
Unresolved external symbols when compiling linking with /MT instead
of /MD
Link error 2001:unresolved external symbol
Linker errors between multiple projects in Visual C++
but was not able to solve this issue.
Actually, I am not even aware of where in the project _Getgloballocale is used. Maybe it would also help to know the lib in which _Getgloballocale is located.
The projects use the following libraries:
cURL
Protobuf
libboost_thread-vc120-mt-1_56.lib
libboost_system-vc120-mt-1_56.lib
libboost_python-vc120-mt-1_56.lib
libboost_filesystem-vc120-mt-1_56.lib
In all projects I linked the run-time library dynamically (/MD).
As others noted, verifying this might be less obvious than appears. For one, some of your libraries might drag in external dependencies that do rely on a mis-matching runtime.
Suggest you link with /VERBOSE on (in your EXE project, properties / linker / general / show progress), and search the output dump for MSVCR. You might catch a different version (msvcr100.lib) or a different configuration (msvcr120d.lib). Also try to search for LIBCMT - that is the library for static linking of the runtime. These typically appear as part of a /DEFAULTLIB linker directive, and you should be able to understand from the dump in which library context this directive is present.
You can also post here the verbose output (or the relevant snippets), and we can try to help interpret it.

constructor overloading Class get Linker Error Using DLL?

I have created Regular DLLs Dynamically Linked to MFC.
in before build dll in dll project i used add another class. this class provide more method of constructor overloading.
Then I build dll successfully after i used this lib and dll file include project and then go to build and get linker error when using constructor overloading class
My Class Name: Object
Error 11 error LNK2001: unresolved external symbol "public: __thiscall AvinashiAMF::Object::~Object(void)" (??1Object#AvinashiAMF##QAE#XZ) BuleCappServiceUseDynamicDllDlg.obj
Error 10 error LNK2001: unresolved external symbol "public: __thiscall AvinashiAMF::Object::Object(enum AvinashiAMF::ObjectType,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Object#AvinashiAMF##QAE#W4ObjectType#1#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) BuleCappServiceUseDynamicDllDlg.obj
Error 12 fatal error LNK1120: 2 unresolved externals D:\Test Aplications\Visual Studio 2008\Projects\BuleCappServiceUseDynamicDll\Release\BuleCappServiceUseDynamicDll.exe
You're probably not exporting your class. See here http://msdn.microsoft.com/en-us/library/81h27t8c.aspx
Also, a nice tool to check for this kind of issues is depends.exe, included with Visual Studio, which allows you to check for exported and imported symbols.
There are two significantly different dynamic link libraries: implicitly linked and explicitly linked.
In short, implicitly linked is linked at the compile time. You need an import library to link with(.lib), header file with functions prototypes and properly usage of the dllexport and dllexport or use .def file.
dllexport/dllexport are easier to use in this case. System takes care of loading libraries (DLLs).
Explicit linking is a runtime linking. You do not need any import library or headers. You need to know what is the function signature. You have to explicitly call LoadLibrary and GetProcAddress to call the function.
To see how to properly create macros for export/import create empty Win32 project select DLL and make sure the Export symbols check box is checked.
In the main header file you will see explanation how to use macros for import/export.