Linker error when parameters are passed by reference to C++ dll - c++

In my application, I have a dll which exposes a function that takes two vectors:
static int myFunc( vector<double> vec1, vector<double> &vec2 );
When I changed this declaration to
static int myFunc( vector<double> &vec1, vector<double> &vec2 );
I get a linker error saying this:
error LNK2019: unresolved external symbol "__declspec(dllimport) public: static int __cdecl myFunctions::myFunc(class std::vector<double,class std::allocator<double> > &,class std::vector<double,class std::allocator<double> > &)" (__imp_?myFunc#myFunctions##SAHAAV?$vector#NV?$allocator#N#std###std##0#Z) referenced in function "public: void __thiscall MainWindow::modelMeanCurve(void)" (?modelMeanCurve#MainWindow##QAEXXZ)
Why is this behavior and how do I resolve this error so that I can pass the reference to the first argument also?
Thanks,Rakesh.

Well, it is not enough to change the declaration alone. You also have to change the definition of that function and recompile the DLL.
If you simply changed the declaration and left the definition unchanged, you essentially created a new declaration for a function that does not really exist. The DLL still contains the original function with original set of parameters, which is now completely unrelated to you new declaration. This is what the linker is telling you through the above error.

Thank you #Michael Burr, I was referring to a stale copy of the .lib file. I replaced this with the new version and everything built fine.

Related

Unresolved external symbol error on variadic template c++ due to usage of unsigned int

I've declared a simple function with variadic template.
template<typename ...Args>
void Log(const LogLevel level, const char * format, Args ...args);
Upon calling it in the following manner -
Log(LogLevel::debug,
R"(starting x, %d pending call for "%s" with param "%s")",
id, first.c_str(),
second.c_str())
where the variable types are : id (unsigned int), first (std::string) , second (std::string)
I'm getting the following error:
Error LNK2001 unresolved external symbol "public: void __cdecl Log<unsigned int,char const *,char const *>(enum LogLevel,char const *,unsigned int,char const *,char const *)"
When I remove the unsigned int argument from the function call - the error disappears.
AFAIK the variadic template does support different types... so what am I missing?
It's a linker error, so (I suppose) you have declared the template function in a header file and defined it in a c++ (not header) file.
If you use the template function that receive the unsigned int in a different c++ file, the compiler doesn't know which versions of the function to implement.
Simple solution: declare and define the template functions/classes/structs in the headers.
If I'm wrong... please prepare a minimal example to replicate the error.

VS2015: LNK2019 error when linking with Muiload.lib

I'm experimenting the next error when including muiload.h and linking with muiload.lib and calling LoadMUILibrary in Visual Studio 2015:
Muiload.lib(muiload.obj) : error LNK2019: unresolved external symbol
__vsnwprintf referenced in function "long __stdcall StringVPrintfWorkerW(unsigned short *,unsigned int,unsigned int
*,unsigned short const *,char *)" (?StringVPrintfWorkerW##YGJPAGIPAIPBGPAD#Z)
Maybe something wrong in muiload.lib?
Solved adding the additional library legacy_stdio_definitions.lib to the linker input as explained in https://social.msdn.microsoft.com/Forums/en-US/5150eeec-4427-440f-ab19-aecb26113d31/updated-to-vs-2015-and-now-get-unresolved-external-errors?forum=vcgeneral
An alternative to linking against legacy_stdio_definitions.lib is to redefine these function signatures to match their deprecated style:
int (WINAPIV * __vsnprintf)(char *, size_t, const char*, va_list) = _vsnprintf;
int (WINAPIV * __vsnwprintf)(wchar_t *, size_t, const wchar_t*, va_list) = _vsnwprintf;
One benefit of this is that it avoids other possible linker definition issues caused by including the legacy library.
Note that this should be defined in a compiler unit (.cpp) rather than in a header file.

Strange linker error in C++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I'm trying to use the code from this project from CodeProject:
http://www.codeproject.com/Articles/587629/A-Delaunay-triangulation-function-in-C
The problem is that the function which should be used in my project is:
WORD* BuildTriangleIndexList (void *pointList, float factor, int numberOfInputPoints, int numDimensions, int clockwise, int *numTriangleVertices)
which has a body inside the .cpp file, but no declaration in the header, so using the code as-is, I obviously get a compilation error complaining that it can't find the function.
So, I've tried to add the declaration to the header, and it compiles correctly, but the linker gave me:
Errore 1 error LNK2019: riferimento al simbolo esterno "unsigned short * __cdecl BuildTriangleIndexList(void *,float,int,int,int,int *)" (?BuildTriangleIndexList##YAPAGPAXMHHHPAH#Z) non risolto nella funzione "protected: virtual void __thiscall TutorialApplication::createScene(void)" (?createScene#TutorialApplication##MAEXXZ) C:\Progetti\TestShader\TestShader\TutorialApplication.obj TestShader
Thanks in advance.
The function definition is missing, either you link with a library that has the definition of the function or define on your own, no other alternative.
Suspect that the call is in C, so, wrap the definition as:
extern "C" {
// delclaration as well as definition both
}
Another possibility is that is it is C++ member function of a class, make sure all overridden member functions have an implementation (function body), if not put as pure virtual (abstract)

Why am I getting this error ? "unresolved external symbol" [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
This is the error I have been getting the whole time and I've been trying to figure out how to fix it but have failed. I am asking if anyone can point me to the right direction.
WorldServer fatal error LNK1120: 2 unresolved externals
WorldServer error LNK2019: unresolved external symbol "public: class CItemElem * __thiscall CLinkedItemMgr::GetLinkedItem(unsigned long)" (?GetLinkedItem#CLinkedItemMgr##QAEPAVCItemElem##K#Z) referenced in function "private: void __thiscall CDPSrvr::OnLinkedItem(class CAr &,unsigned long,unsigned long,unsigned char *,unsigned long)" (?OnLinkedItem#CDPSrvr##AAEXAAVCAr##KKPAEK#Z)
WorldServer error LNK2019: unresolved external symbol "public: int __thiscall CLinkedItemMgr::AddLinkedItem(class CItemElem *)" (?AddLinkedItem#CLinkedItemMgr##QAEHPAVCItemElem###Z) referenced in function "private: void __thiscall CDPSrvr::OnLinkedItem(class CAr &,unsigned long,unsigned long,unsigned char *,unsigned long)" (?OnLinkedItem#CDPSrvr##AAEXAAVCAr##KKPAEK#Z)
This is the .h
#ifndef __ITEM_LINK__H
#define __ITEM_LINK__H
class CLinkedItemMgr
{
private:
CLinkedItemMgr(){ m_dwLinkedItemCount = 0;};
~CLinkedItemMgr(){};
DWORD m_dwLinkedItemCount;
public:
map<DWORD,CItemElem*> m_mapLinkedItems;
static CLinkedItemMgr *GetInstance()
{
static CLinkedItemMgr instance;
return &instance;
}
int AddLinkedItem(CItemElem *pItem);
CItemElem *GetLinkedItem(DWORD dwIndex);
};
#endif
this is the .cpp
#include "stdafx.h"
#include "ItemLink.h"
int CLinkedItemMgr::AddLinkedItem(CItemElem *pItem)
{
if(!pItem)
return 0;
m_mapLinkedItems.insert(make_pair<DWORD,CItemElem*>(++m_dwLinkedItemCount,pItem));
return m_dwLinkedItemCount;
}
CItemElem *CLinkedItemMgr::GetLinkedItem(DWORD dwIndex)
{
map<DWORD,CItemElem*>::iterator it = m_mapLinkedItems.find(dwIndex);
if(it == m_mapLinkedItems.end())
return FALSE;
return it->second;
}
Your problem is in the cpp here.
#ifdef __ITEM_LINK
#include "ItemLink.h"
#ifdef __ITEM_LINK means "only process the code below if __ITEM_LINK is defined"
And in your case, it is not defined. It only gets defined when "ItemLink.h" is included, and "ItemLink.h" only gets included if it's already defined. You've prevented either from happening first.
Remove the #ifdef line.
It looks like a linking problem.
The compiler knows your class has a function called GetLinkedItem but can't find any definition of that function anywhere. Are you linking properly when compiling your executable?
I bet stopping the compiler before linking doesn't trigger any error.
(e.g. g++ -c ItemLink.cpp).
i'm going to ask the help of someone who's accustomed to visual studio to elaborate more :D
anyway, compiling requires three major steps:
1) applying preprocessor directives, parsing the source code, looking for syntax errors and the like
2) creating an object file from source code (something half-way between source code and executable)
3) linking all the object files making up your project in one executable
your compiling chain fails at the third step.
the compiler expects a certain function to be defined in some .cpp (that has become an object file at step 2 of compiling chain) but can't find it anywhere.
and it can't find it because of that #ifdef in the .cpp file, which tells the preprocessor NOT TO INCLUDE your definitions, since __ITEM_LINK is not defined
i see you changed the .cpp in your question by the way

Unresolved externals using C++ lambdas [duplicate]

This question already has answers here:
Why do I get "unresolved external symbol" errors when using templates? [duplicate]
(3 answers)
Why can templates only be implemented in the header file?
(17 answers)
Closed 9 years ago.
I am getting unresolved externals compile error with following code snippet.
acquire_gray(identity, []{});
samething happens when I try to use auto
auto acquire_callback = [](LPBITMAPINFOHEADER pbi, HANDLE hdib)
{
printf("Callback\n");
};
acquire_gray("", acquire_callback );
but when I pass in null it compiles
acquire_gray(identity, NULL);
This is structure of my program
driver.cpp
#include "bridge.h"
void TB_AcquireImagesStart(HANDLE hNamedPipe, const TB_Message request)
{
acquire_gray("", []{});
}
bridge.h
template<typename T>
void acquire_gray(const string_t& identity, T& callback);
bridge.cpp
template<typename T>
void acquire_gray(const string_t& identity, T& callback)
{
callback();
}
So the two exceptions that I am getting are
Error 12 error LNK1120: 1 unresolved externals
Error 11 error LNK2019: unresolved external symbol "void __cdecl acquire_gray<class <lambda_e125ff607fe0339bba6077ce9c14d586> >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class <lambda_e125ff607fe0339bba6077ce9c14d586> &)" (??$acquire_gray#V<lambda_e125ff607fe0339bba6077ce9c14d586>####YAXABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##AAV<lambda_e125ff607fe0339bba6077ce9c14d586>###Z) referenced in function "void __cdecl TB_AcquireImagesStart(void *,class TB_Message)" (?TB_AcquireImagesStart##YAXPAXVTB_Message###Z)
So my question is what is wrong with my code, and how can I fix this, and why is auto not detecting my lambda type.
You can't put template definitions in .cpp (well you can, but it only makes sense if you're using them in that compilation unit). After compilation, only template instanciations exist.
This is just a common pitfall when using templates. You cannot (or at least should not) separate a template into header (.hpp) and source (.cpp) files. See here for details.