Diagnosing unresolved externals in a static lib - c++

I have a number of DLLs building and linking against my application without issues, but this static lib is giving me troubles.
Present in it is just one class. The class is declared with __declspec(dllexport) when building the lib and __declspec(dllimport) when including the class's header in the application.
When I attempt to build the application with an instance of MyClass, I get:
error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall MyClass::~MyClass(void)"
error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall MyClass::MyClass(void)"
And if I attempt to build the application with a pointer to MyClass, I get the same unresolved externals plus two additional warnings:
warning LNK4217: locally defined symbol ??0MyClass##QAE#XZ (public: __thiscall MyClass::MyClass(void)) imported in function _main
warning LNK4217: locally defined symbol ??1MyClass##UAE#XZ (public virtual __thiscall MyClass::~MyClass(void))
Searching the web for LNK4217 suggests incorrect usage of the /M[TD]d? cl.exe switch. I recreated the Visual Studio solution and dragged over the sources to make sure it was provisioned correctly. For reference, both the Lib and app have /MDd.

Related

Unresolved external symbol ATL::IAtlMemMgr after upgrading to Visual Studio 2019

I am currently upgrading our source code from VS2012 to VS2019. One project which uses MFC does not link successfully:
fatal error LNK1120: 4 unresolved externals
error LNK2001: unresolved external symbol "public: void * __cdecl ATL::IAtlMemMgr::Allocate(unsigned __int64)" referenced in function "public: virtual struct ATL::CStringData * __cdecl ATL::CAtlStringMgr::Allocate(int,int)"
error LNK2001: unresolved external symbol "public: void __cdecl ATL::IAtlMemMgr::Free(void *)" referenced in function "public: virtual void __cdecl ATL::CAtlStringMgr::Free(struct ATL::CStringData *)"
error LNK2001: unresolved external symbol "public: void * __cdecl ATL::IAtlMemMgr::Reallocate(void *,unsigned __int64)" referenced in function "public: virtual struct ATL::CStringData * __cdecl ATL::CAtlStringMgr::Reallocate(struct ATL::CStringData *,int,int)"
error LNK2001: unresolved external symbol "public: virtual struct ATL::CStringData * __cdecl ATL::IAtlStringMgr::Reallocate(struct ATL::CStringData *,int,int)"
We are building with Multi-Byte Character Set (MBCS). My first thought was, that we are missing the the mbcs libraries. But as mentioned here, the libraries are installed by default, "when you select MFC and ATL support".
I added atlbase.h in the code and added atls.lib manually as additional dependency, but that did not solve the problem.
How can I figure out which library is missing?
Edit 1:
Let's take a look into atlmem.h:
__interface __declspec(uuid("654F7EF5-CFDF-4df9-A450-6C6A13C622C0")) IAtlMemMgr{
public:
_Ret_maybenull_ _Post_writable_byte_size_(nBytes) void* Allocate(_In_ size_t nBytes) throw();
As far as I see, this is one of the symbols which is not found. As can be red about the __interface keyword, it implicitly makes the functions pure virtual. These kind of linker errors might be caused by non pure virtual function declarations.
Might there be a bug which makes the functions in the __interface not pure virtual?
After excluding nearly all source files and commenting out a lot of functionality, I could see that the linker errors are correlated to <afxwin.h>. Moving <afxwin.h> to the top of each source files removed the linker errors.
However, I am interested to know why compiling with VS2012 gives another behavior compared to VS2019.

Visual Studio Build fails when using a class from another project

I am working on a C++ application and I cannot build it because of LNK2001 and LNK2019 errors.
It has 4 projects: Client, Depry, Miscellaneous and Server. I want to have a base exception class in Miscellaneous and to be able to inherit from it in the Client and Server projects.
I tried adding the "include" folder from the Miscellaneous project to the Client's Additional Dependencies and I also added Miscellanous to Client's project dependencies. It did not work.
Example Errors:
Error LNK2019 unresolved external symbol "public: virtual __cdecl DepryException::~DepryException(void)" (??1DepryException##UEAA#XZ) referenced in function "public: virtual void * __cdecl DepryException::`scalar deleting destructor'(unsigned int)" (??_GDepryException##UEAAPEAXI#Z) ChatClient E:\SEng\CPlusPlus\Depry\Client\ChatClient.obj 1
Error LNK2001 unresolved external symbol "public: virtual char const * __cdecl DepryException::what(void)const " (?what#DepryException##UEBAPEBDXZ) ChatClient E:\SEng\CPlusPlus\Depry\Client\ChatClient.obj 1

Solving "locally defined symbol imported" errors in VC++ 2015

I have a small command line program that uses Maxim's OneWire Public Domain API and WinUSB. It builds absolutely fine on my laptop using Win7/VS2013. Now I'm trying to move it to Win8.1/VS2015 on my new desktop PC, and I can't get it to build. Here are the errors reported:
1>------ Build started: Project: readtemp5, Configuration: Debug Win32 ------
1>presshum.obj : warning LNK4217: locally defined symbol _printf imported in function _print_array
1>readtemp.obj : warning LNK4049: locally defined symbol _printf imported
1>presshum.obj : warning LNK4217: locally defined symbol _sprintf_s imported in function _ReadPressHum
1>readtemp.obj : warning LNK4049: locally defined symbol _sprintf_s imported
1>temp10.obj : warning LNK4049: locally defined symbol _sprintf_s imported
1>presshum.obj : warning LNK4217: locally defined symbol _sprintf imported in function _ReadPressHum
1>temp10.obj : warning LNK4049: locally defined symbol _sprintf imported
1>readtemp.obj : warning LNK4217: locally defined symbol _fprintf imported in function _main
1>readtemp.obj : error LNK2019: unresolved external symbol __imp____iob_func referenced in function _main
1>readtemp.obj : error LNK2019: unresolved external symbol __imp__scanf referenced in function _mainx
1>readtemp.obj : error LNK2019: unresolved external symbol __imp__sscanf referenced in function _main
1>readtemp.obj : error LNK2001: unresolved external symbol _owErrorStack
1>readtemp.obj : error LNK2001: unresolved external symbol _owErrorPointer
1>readtemp.obj : error LNK2001: unresolved external symbol _owErrorMsg
1>C:\Users\RowanB\Documents\Visual Studio 2015\Projects\readtemp5\readtemp5\Debug\readtemp5.exe : fatal error LNK1120: 6 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
What does the error "locally defined symbol imported" actually mean? How do I find which line in which file is causing the problem?
What are the rules surrounding the symbol names with underscores? How do I work out what the original symbol names (i.e. the ones that I will see in source files) are?
I guess that the problem is probably that some header file or library or DLL is missing from the desktop PC, or are installed in a different place, and/or that I have not set up the project properties correctly to locate all the files needed. But how do I find out exactly what's wrong, and how to fix it?
I installed WDK on the new PC but it didn't install in the same place as on the laptop (C:\WinDDK). I can't actually find where it put it. Where are the WDK files?
Thanks - Rowan
I had similar errors when switching between 2012 and 2015 compilers withing one working directory (going back and forth between two source control commits where one was still built with 2012 and another with 2015). The fix was simply to clean the solution and rebuild. If you copied the entire folder to your new PC, it is possible that intermediate build files from 2013 cause the problem.

XDispatch C++ Unresolved Externals

I'm work with GCD in C++ with xdisptach, libdispatch in Visual Studio 2012 on Windows 7.
I am declaring a class with a global variable that is a dispatch queue. Other functions in the class call the queue's function. Everything compiles fine, except when i instantiate the queue in the constructor.
xdispatch::queue* dispatch_queue;
AsyncNode()
{
dispatch_queue = new xdispatch::queue(Name);
}
When dispatch_queue = new xdispatch::queue(Name); is commented out, it all compiles fine. Otherwise i get the following errors.
Error 50 error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall xdispatch::queue::~queue(void)" (__imp_??1queue#xdispatch##UAE#XZ) referenced in function "public: virtual void * __thiscall xdispatch::queue::`scalar deleting destructor'(unsigned int)" (??_Gqueue#xdispatch##UAEPAXI#Z)
Error 49 error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall xdispatch::queue::queue(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (__imp_??0queue#xdispatch##QAE#ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) referenced in function "public: __thiscall AsyncNode::AsyncNode(void)" (??0AsyncNode##QAE#XZ)
Error 53 error LNK2001: unresolved external symbol "public: virtual void __thiscall xdispatch::object::resume(void)" (?resume#object#xdispatch##UAEXXZ)
Error 51 error LNK2001: unresolved external symbol "public: virtual void * __thiscall xdispatch::queue::native(void)const " (?native#queue#xdispatch##UBEPAXXZ)
Error 52 error LNK2001: unresolved external symbol "public: virtual struct dispatch_queue_s * __thiscall xdispatch::queue::native_queue(void)const " (?native_queue#queue#xdispatch##UBEPAUdispatch_queue_s##XZ)
This is the main site for xdispatch, but i can't find anything in terms of a forum or help with xdispatch in particular. There are many for objective-c.... :/
http://opensource.mlba-team.de/xdispatch/docs/current/index.html
First of all, the behavior regarding deleting the "problematic" line is normal - when you're defining a pointer you're not invoking any function, and that is the reason you are not getting the "unresolved external errors".
When you're initializing the variable by calling the constructor, you then run into problems because you're trying to call a function which is not available at link time.
When working with external libraries using DLLs, you must link to the appropriate import libraries, usually supplied as "lib" files. In your case, there is a folder named "lib" in the zip package. Also, the DLLs must availble at run-time - by putting them in the folder of the executable, or adding them to the PATH environment variable.
In order to link to a library, follow these steps (taken from MSDN):
Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.
Click the Linker folder.
Click the Input property page.
Modify the Additional Dependencies property.

Linking to V8 does not work in release mode

I built the v8 libraries on visual studio 2005 in release mode, put the resulting .lib files (in release mode) and compiled my project against them.
While everything is working fine in debug mode (compiling and running correctly), in release mode I get the following link errors (as if the lib files are not there).
error LNK2001: unresolved external symbol "public: class v8::Local<class v8::Boolean> __thiscall v8::Value::ToBoolean(void)const " (?ToBoolean#Value#v8##QBE?AV?$Local#VBoolean#v8###2#XZ)
error LNK2001: unresolved external symbol "public: bool __thiscall v8::Value::IsInt32(void)const " (?IsInt32#Value#v8##QBE_NXZ)
error LNK2001: unresolved external symbol "public: bool __thiscall v8::Value::IsNumber(void)const " (?IsNumber#Value#v8##QBE_NXZ)
error LNK2001: unresolved external symbol "public: bool __thiscall v8::Value::IsBoolean(void)const " (?IsBoolean#Value#v8##QBE_NXZ)
error LNK2001: unresolved external symbol "public: bool __thiscall v8::Value::IsObject(void)const " (?IsObject#Value#v8##QBE_NXZ)
error LNK2001: unresolved external symbol "public: bool __thiscall v8::Value::IsArray(void)const " (?IsArray#Value#v8##QBE_NXZ)
error LNK2001: unresolved external symbol "public: bool __thiscall v8::Value::IsFunction(void)const " (?IsFunction#Value#v8##QBE_NXZ)
error LNK2001: unresolved external symbol "public: bool __thiscall v8::Value::IsNull(void)const " (?IsNull#Value#v8##QBE_NXZ)
My linker properties have the following additional depencies.
msvcrt.lib ntstc_msvcrt.lib Psapi.lib winmm.lib Ws2_32.lib $(SolutionDir)\external_libs\release\v8.lib
and my $(SolutionDir)\external_libs\release folder containst the files:
v8.lib, v8_base.lib, v8_snapshot.lib
Any help would be appreciated.
In Visual Studio the build options are separate for debug and release builds. You probably set everything up properly for debug mode, but not for release mode, hence the errors.
I forget exactly how things are layed out in VS2005, but in VS2008, there is a combobox at the top of the solution properties dialog that lets you choose between debug/release/all configurations.
In other words, you will have to add the additional dependencies to the linker properties section in release mode. Alternatively, in the future, make all changes with the configuration set to All configurations.
Likely cause: The command switches and defines you used to build the v8 lib don't match those that you are using to build the code consuming it.
As an aside, if you are using the vs2005 CRT, you don't need this: ntstc_msvcrt.lib
Martyn