Using a DLL that links to a static lib - c++

I'm trying to build a solution in Visual C++ where I have a front-end project that references a DLL project that I created. In the DLL project I link to a static library (that I have not written) that has static objects and definitions. Everything builds fine but I have linking problems.
I have a couple of questions. First, I should only get unresolved symbols for objects that I reference in the front-end that are not exported, right? I want the DLL to be the only interface to the static library and do not directly reference any part of it in the front-end, and yet I get a number of unresolved symbols from this library. There symbols seem to be #included and at least some not directly linked by the DLL project. I suspect it has to do with the static declarations in the static lib but how can I deal with these?
Some of the unresolved symbol errors:
2>AnalysisVis.obj : error LNK2001: unresolved external symbol "public: __thiscall SharkException::SharkException(char const *,int,char const *)" (??0SharkException##$$FQAE#PBDH0#Z)
2>AnalysisVis.obj : error LNK2001: unresolved external symbol "public: static class Bernoulli Rng::coinToss" (?coinToss#Rng##2VBernoulli##A)
2>AnalysisVis.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall ChromosomeT<bool>::operator<(class Chromosome const &)const " (??M?$ChromosomeT#_N##$$FUBE_NABVChromosome###Z)

The exported symbols are mangled. If the static lib was compiled using a different compiler (or compiler version) than the one you are using, it is possible that the symbols your application is expecting to see were defined in the static lib using a different name mangling scheme. You can use the following command to get the name mangling used in the static lib and then compare it to the one in the error message:
>pushd <path_to_msvc_dir>\Microsoft Visual Studio X.0\VC\bin
>dumpbin /all [static_lib_path] > out.txt
>type out.txt | find /I "SharkException"
>type out.txt | find /I "coinToss"
>type out.txt | find /I "ChromosomeT"
BTW, does the DLL that uses the static lib compiles cleanly with the same compiler your application/solution does?

Related

How to define macro which correctly sets the __declspec

Using Visual Studio 2017, I am trying to build my latest project which imports libraries, which in turn import methods and functions from .dll files.
When building my project, I get a list of errors like this:
error LNK2001: unresolved external symbol "__declspec(dllimport) void __cdecl UserTracking(void *)" (__imp_?UserTracking##YAXPEAX#Z)
error LNK2001: unresolved external symbol "public: bool __cdecl EACServer::Destroy(void)const " (?Destroy#EACServer##QEBA_NXZ)
error LNK2001: unresolved external symbol "public: bool __cdecl EACServer::Initialize(void)const " (?Initialize#EACServer##QEBA_NXZ)
...
All of the functions listed are from imported libraries.
As an example, the EACServer::Initialize method is defined as so in EACServer.h:
bool Initialize() const;
In the code I am compiling, this function is used as so (the appropriate header files are imported in the .h file ofc):
this->eacServer = EACServer();
this->eacServer.Initialize();
The class definition of EACServer is basic:
class EACServer : IRoot {
...
}
I have been told that these errors are thrown because I am missing the macro which correctly sets the __declspec.
How can I find/implement this macro?
Turns out that although I added the paths to my libraries in the linker additional library directories, I had neglected to add the .lib files in the linker additional dependencies.

unresolved external symbol is due to .lib or .dll?

I'm struggling to try to use OpenEXR in my Visual Studio 2015 project.
The OpenEXR library is successfully built. But when I build my project, I got unresolved symbol error.
I suppose the error is due to the linker failing to find the library. But I don't know if it's in .lib or .dll?
I've googled for this and it is said that for static libraries, the implementation is all in .lib file.
But for dynamically linked libraries, the implementation is in .dll file. So which caused the unresolved symbol error? .lib or .dll?
Error:
7>core.lib(ImageIO.obj) : error LNK2019: unresolved external symbol "private: static unsigned short const * const half::_eLut" (?_eLut#half##0QBGB) referenced in function "bool __cdecl Tungsten::ImageIO::saveExr(class Tungsten::Path const &,float const *,int,int,int)" (?saveExr#ImageIO#Tungsten##YA_NAEBVPath#2#PEBMHHH#Z)

Link errors with GraphicsMagick

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

Problems with SOCI

I'm trying to use the SOCI library for easy MySQL access but I'm stuck at installing the library.
What I've done so far is :
Made the project and compiled it with CMake
Linked my project to the lib/release directoy that's created after compilation.
Linked my project to the MySQL C Connector lib directory
and here are my additional include directories :
http://puu.sh/6qGNP.png
But somehow, when I compile the example program, I get a bunch of linker errors like these :
1>main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall soci::details::standard_into_type::~standard_into_type(void)" (??1standard_into_type#details#soci##UAE#XZ) referenced in function "public: virtual __thiscall soci::details::into_type::~into_type(void)" (??1?$into_type#H#details#soci##UAE#XZ)
1>main.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall soci::details::standard_into_type::post_fetch(bool,bool)" (?post_fetch#standard_into_type#details#soci##MAEX_N0#Z)
Where did I mess up?
From the documentation:
Required Client Libraries
The SOCI MySQL backend requires MySQL's libmysqlclient client library.
Note that the SOCI library itself depends also on libdl, so the minimum set of libraries needed to compile a basic client program is:
-lsoci_core -lsoci_mysql -ldl -lmysqlclient
This has always worked for me. If you want to link them from the build location a -L switch with the path to the libraries will need to be passed to the compiler.

How to Link to a .lib file in Visual C++ 2010? Without referencing the project?

I just have a problem that I have been trying to fix for the longest time.
I have a static library project in visual c++, and I want another project to be able to link to it. Up until now, I have simply been adding a reference to the static library project, which automatically links the library.
I want to be able to link to the library using only the header files and the .lib file. However, I get a "Unresolved external symbol" error.
I thought I was doing it right - I specified the include directory, the library directory, and went into the linker input properties and provided the lib as an additional dependency.
I am able to reference other static libraries this way (like SDL), so why do I get errors when I try to reference mine?
Thanks for the help.
Is the problem that its not referencing the actual lib file, or is something within the lib itself?
These are the error messages I get:
Error 2 error LNK2019: unresolved external symbol "public: void __thiscall XEngine::XCore::XScreen::init(class XEngine::XCore::XGame &)" (?init#XScreen#XCore#XEngine##QAEXAAVXGame#23##Z) referenced in function "void __cdecl XEngine::XEngineInit(class XEngine::XCore::XScreen &,class XEngine::XCore::XGame &)" (?XEngineInit#XEngine##YAXAAVXScreen#XCore#1#AAVXGame#31##Z) C:\Users\Xander Masotto\Documents\Visual Studio 2010\Projects\Pong\Pong\source.obj Pong
Error 3 error LNK2019: unresolved external symbol "public: __thiscall XEngine::XCore::XScreen::~XScreen(void)" (??1XScreen#XCore#XEngine##QAE#XZ) referenced in function "void __cdecl XEngine::XEngineInit(class XEngine::XCore::XGame &)" (?XEngineInit#XEngine##YAXAAVXGame#XCore#1##Z) C:\Users\Xander Masotto\Documents\Visual Studio 2010\Projects\Pong\Pong\source.obj Pong
Error 4 error LNK2019: unresolved external symbol "public: __thiscall XEngine::XCore::XScreen::XScreen(void)" (??0XScreen#XCore#XEngine##QAE#XZ) referenced in function "void __cdecl XEngine::XEngineInit(class XEngine::XCore::XGame &)" (?XEngineInit#XEngine##YAXAAVXGame#XCore#1##Z) C:\Users\Xander Masotto\Documents\Visual Studio 2010\Projects\Pong\Pong\source.obj Pong
Make sure that you are exporting the functions, classes, and variables in your library that you want exposed to other applications (i.e. your dll or exe). By default they are not exposed.
The ground work to do this is typically layed out when you create the project for your library.
#ifdef TESTLIB_EXPORTS
#define TESTLIB_API __declspec(dllexport)
#else
#define TESTLIB_API __declspec(dllimport)
#endif
With the code above generated during project creation there are only two more things for me to do to expose functions,classes, or variables:
1) Make sure that I have TESTLIB_EXPORTS defined as a preprocessor. Project settings: C++/Preprocessor/PreprocessorDefinitions
2) Use the TESTLIB_API define on each function,class, or variable i want exposed:
class TESTLIB_API Order {
void doSomething();
};