Linking Error in C++ on visual studio 2013 - c++

Following error is coming when I am compiling the project "proj1" code which is using xyz.lib (it is a different project that got compiled successfully).
Error 3 error LNK2019: unresolved external symbol "int __cdecl Vsnprintf16(unsigned short *,unsigned int,unsigned short const *,char *)" (?Vsnprintf16##YAHPAGIPBGPAD#Z) referenced in function "int __cdecl eastl::Vsnprintf(wchar_t *,unsigned int,wchar_t const *,char *)" (?Vsnprintf#eastl##YAHPA_WIPB_WPAD#Z) File : xyz.lib(abc.obj)
abc.cpp has calls to function sprintf .
When I am moving all the code of abc.h and abc.cpp to some other lets say def.h and def.cpp file which is already present in xyz project then all works fine, no linking error. I don't know why.
I have used all the includes that were used in file def.cpp in abc.cpp but same error.
When I am removing the sprintf() calls from abc.cpp then also all works fine.
Please if anyone can suggest why this is happening. Thanks

I have searched msdn and also VS2015 and VS2005 source code folders and found no definition or declaration of Vsnprintf16.
I have not used eastl but it looks like you should define this function by yourself, you can find examples in following links:
https://github.com/BSVino/Digitanks/blob/master/common/eastl.cpp
https://github.com/electronicarts/EASTL/blob/master/test/source/main.cpp
for reference I include it here:
// EASTL also wants us to define this (see string.h line 197)
int Vsnprintf8(char* pDestination, size_t n, const char* pFormat, va_list arguments)
{
#ifdef _MSC_VER
return _vsnprintf(pDestination, n, pFormat, arguments);
#else
return vsnprintf(pDestination, n, pFormat, arguments);
#endif
}
int Vsnprintf16(char16_t* pDestination, size_t n, const char16_t* pFormat, va_list arguments)
{
#ifdef _MSC_VER
return _vsnwprintf((wchar_t*)pDestination, n, (wchar_t*)pFormat, arguments);
#else
char* d = new char[n+1];
int r = vsnprintf(d, n, convertstring<char16_t, char>(pFormat).c_str(), arguments);
memcpy(pDestination, convertstring<char, char16_t>(d).c_str(), (n+1)*sizeof(char16_t));
delete[] d;
return r;
#endif
}

Related

Errors LNK 2019 and LNK2028 for one function in a class, other works fine

I normally work in c# and am out of my wits for this one . I used Walkthrough: Creating and Using a Dynamic Link Library (C++) to create a Dynamic Link Library.
I have defined two methods as shown below
DeveloperConsoleManager.h
#pragma once
#include "atlstr.h"
#ifdef DEVCONSOLEMANAGER_EXPORTS
#define DEVCONSOLEMANAGER_API __declspec(dllexport)
#else
#define DEVCONSOLEMANAGER_API __declspec(dllimport)
#endif
namespace DeveloperConsoleManager
{
class DeveloperConsoleLogic
{
public:
// Returns a + b
static DEVCONSOLEMANAGER_API double Add(double a, double b);
static DEVCONSOLEMANAGER_API bool CheckforValidFile(CString fileName);
};
}
DeveloperConsoleManager.cpp
// DeveloperConsoleManager.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "DeveloperConsoleManager.h"
namespace DeveloperConsoleManager
{
double DeveloperConsoleLogic::Add(double a, double b)
{
return a + b;
}
bool DeveloperConsoleLogic :: CheckforValidFile(CString fileName)
{
return false;
}
}
I use these methods in a .cpp file in a different project (type: Application (.exe)). When I Build the solution, there are following linker errors
Warning 1 warning C4273: 'DeveloperConsoleManager::DeveloperConsoleLogic::Add' : inconsistent dll linkage e:\md_69\developerconsolemanager\developerconsolemanager.cpp 10
Warning 2 warning C4273: 'DeveloperConsoleManager::DeveloperConsoleLogic::CheckforValidFile' : inconsistent dll linkage e:\md_69\developerconsolemanager\developerconsolemanager.cpp 16
Error 3 error LNK2028: unresolved token (0A0004F1) "public: static bool __cdecl DeveloperConsoleManager::DeveloperConsoleLogic::CheckforValidFile(class ATL::CStringT > >)" (?CheckforValidFile#DeveloperConsoleLogic#DeveloperConsoleManager##$$FSA_NV?$CStringT#_WV?$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z) referenced in function "public: void __thiscall CSaSsiConsoleUi::UploadSsiCheck(void)" (?UploadSsiCheck#CSaSsiConsoleUi##$$FQAEXXZ) E:\MD_69\DeveloperConsoleUI\SaSsiConsoleUI.obj
Error 4 error LNK2019: unresolved external symbol "public: static bool __cdecl DeveloperConsoleManager::DeveloperConsoleLogic::CheckforValidFile(class ATL::CStringT > >)" (?CheckforValidFile#DeveloperConsoleLogic#DeveloperConsoleManager##$$FSA_NV?$CStringT#_WV?$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z) referenced in function "public: void __thiscall CSaSsiConsoleUi::UploadSsiCheck(void)" (?UploadSsiCheck#CSaSsiConsoleUi##$$FQAEXXZ) E:\MD_69\DeveloperConsoleUI\SaSsiConsoleUI.obj
Error 5 error LNK1120: 2 unresolved externals E:\MD_69\Debug\DeveloperConsoleUi.exe
There is no linker error for the "Add" method.
I have already included "DeveloperConsoleManager.lib" in Linker -> Input -> Additional Dependencies. Please help me find out what exactly am I doing wrong.
I would be glad to add any additional information needed.
Thanks to #Igor Tandetnik and the awesome thing that is internet, I figured it out. I am adding it as an answer so that some one else might benefit.
The problem was with CString. The project in which the function was defined was a dynamic link library (dll) and the call was being made from an MFC application. Now, the issue was that, MFC uses for CString while the non-MFC dll uses .
CString in is defined as:
typedef ATL::CStringT< TCHAR, StrTraitMFC_DLL< TCHAR > > CString;
while in is defined as:
typedef CStringT< TCHAR, StrTraitATL< TCHAR > > CString;
This, as you can clearly see is different. The workaround I used was using CAtlString instead of CString . However, please feel free to suggest any better way if you come across.

included header for static library giving dllimport error

I have the code presented later using Xerces-c, which can be built as a static or dynamic library. Failing to include of course results in a compiler error, however when I add #include <xercesc/util/PlatformUtils.hpp> visual studio 2012 gives me a linker errors saying:
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl xercesc_3_1::XMLPlatformUtils::Initialize(char const * const,char const * const,class xercesc_3_1::PanicHandler * const,class xercesc_3_1::MemoryManager * const)" (__imp_?Initialize#XMLPlatformUtils#xercesc_3_1##SAXQBD0QAVPanicHandler#2#QAVMemoryManager#2##Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl xercesc_3_1::XMLPlatformUtils::Terminate(void)" (__imp_?Terminate#XMLPlatformUtils#xercesc_3_1##SAXXZ) referenced in function __catch$_main$0
1>main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static char const * const xercesc_3_1::XMLUni::fgXercescDefaultLocale" (__imp_?fgXercescDefaultLocale#XMLUni#xercesc_3_1##2QBDB)
Based on the dllimport part of the error it seems that it's failing to find a dll. This is confirmed by that when I build Xerces-c as a dynamic library and link to it the error goes away. However if I build Xerces-c as a static library and link to it the same error remains. So my question is why am I getting an error asking for a dll when I'm including and linking to a static library?
using namespace xercesc;
int main(int argc, char* argv[])
{
std::ifstream inputFile(argv[1]);
char c = inputFile.get();
while (inputFile.good()) {
std::cout << c;
c = inputFile.get();
}
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
// Do your failure processing here
return 1;
}
// Do your actual work with Xerces-C++ here.
//XercesDOMParser parser;
//parser.useScanner(XMLUni::fgDGXMLScanner);
XMLPlatformUtils::Terminate();
// Other terminations and cleanup.
return 0;
}
You need to compile your application with XERCES_STATIC_LIBRARY preprocessor macro to disable DLL import/export for Xerces library.
Also check that you link against static version of .lib files.

error LNK2019: unresolved external symbol ___iob_func referenced in function "void __cdecl Padding(int)"

Using the FTDI API compiles and links fine under Visual Studio 2012.
but under VS 2014, it gives:
Error LNK2019: unresolved external symbol ___iob_func referenced in function "void __cdecl Padding(int)"
Have the standard libraries changed?
Yes, the standard libraries have changed, and FTDI doesn't seem to care - at least not as of CDM2.12.18 driver version.
The problem is described in the answers to this question.
The void __cdecl Padding(int) function from devcon.obj within ftd2xx.lib is the culprit. It references one of stdin, stdout or stderr, given as macros. The contents of these macros changed.
Since we don't really expect any I/O from the FTDI library, we might as well provide the simplest implementation possible:
FILE* __cdecl _imp____iob_func() { return 0; }
If you want a version that does what it's supposed to do:
FILE* __cdecl _imp____iob_func()
{
struct _iobuf_VS2012 { // ...\Microsoft Visual Studio 11.0\VC\include\stdio.h #56
char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname; };
// VS2015 has FILE = struct {void* _Placeholder}
static struct _iobuf_VS2012 bufs[3];
static char initialized = 0;
if (!initialized) {
bufs[0]._ptr = stdin->_Placeholder;
bufs[1]._ptr = stdout->_Placeholder;
bufs[2]._ptr = stderr->_Placeholder;
initialized = 1;
}
return (FILE*)&bufs;
}

Convert from BSTR to char*

I have seen:
Convert BSTR to CHAR* and CHAR* to BSTR in C
Problems converting BSTR to char *
Convert BSTR to char*
using these question I am attempting to convert a BSTR to a char* via
#include "comutil.h"
STDMETHODIMP CServer::Initialise(BSTR strCmdFilePath,
VARIANT_BOOL bDiagErr, VARIANT_BOOL bProcErr, BSTR* RESULT)
{
char *p = _com_util::ConvertBSTRToString(strCmdFilePath);
...
}
but I am getting:
Error 1 error LNK2019: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)" (?ConvertBSTRToString#_com_util##YGPADPA_W#Z) referenced in function "public: virtual long __stdcall CServer::Initialise(wchar_t *,short,short,wchar_t * *)" (?Initialise#CServer##UAGJPA_WFFPAPA_W#Z)
Why am I getting this error?
Your project is not linking the required library. Which is comsuppw.lib for the Release build, comsuppwd.lib for the Debug build. Do note that you can always see the required library in the MSDN article. It is annotated at the bottom of the article. "Header" tells you what you need to #include, "Lib" tells you what you need to link.
There's an easier way for this library, the best way to get the linker instruction embedded so it is automatic is by #including the .h file that contains the #pragma comment. Fix:
#include <comdef.h> // Added
#include <comutil.h>

LNK2019: unresolved external symbol but I have coded the function

Can somebody help me with this? I'm using Visual Studio 2010
I'm getting this message and I don't know how to solve this.
1> Generating Code...
1>dct.obj : error LNK2019: unresolved external symbol "public:
__thiscall Amostras::Amostras(class std::basic_string,class std::allocator >)"
(??0Amostras##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
referenced in function _main
1>C:\Users\redneck\documents\visual studio
2010\Projects\dct\Debug\dct.exe : fatal error LNK1120: 1 unresolved
externals
Here is some of the *.cpp file:
class Amostras {
public:
int original[10][257];
int idct[10][257];
float dct[10][257];
int grupos;
Amostras::Amostras(void)
{
for (int i=0;i<10;i++)
{
this->original[i][0]=0;
this->dct[i][0]=0.0;
this->idct[i][0]=0;
}
this->grupos=0;
}
Amostras::Amostras(string arquivo)
{
int n_samples=0,linha=0,coluna=0;
int cont;
..
and here is the *.h
class Amostras {
public:
int original[10][257];
int idct[10][257];
float dct[10][257];
int grupos;
Amostras::Amostras();
Amostras::Amostras(string arquivo);
void Amostras::mostra(void);
};
main
int main(void)
{
Amostras *amostra = new Amostras("in.txt");
dct(amostra,0);
show(amostra,0);
amostra->mostra();
return 0;
}
hope it helps, i'm running out of options here :(
Solution:
So what I did was just putting the class just in *.h and then including the *.h in the class *.cpp that only has the methods and functions of that class. It worked!
This linker error usually means that you have prototyped a function but forgotten to define it. Make sure that you have implemented the function
Amostras::Amostras(string arg);
somewhere, and that when you were linking your code that the object file containing that implementation was linked in.
Hope this helps!
you forgot to define the Amostras::Amostras(string arg);
though declared in your *.h file
Amostras::Amostras(string arg)
{
}
copy the above code in your *.cpp file
OR
you can also do this by commenting the line from your *.h file.
//Amostras::Amostras(string arg);
whoa! do you have a *.h file ? if you are working in only *.cpp then lemme know.