After removing the CRT from my DLL I have gotten these weird errors.
Here are they:
LNK2001 unresolved external symbol "void __cdecl std::_Xbad_alloc(void)" (?_Xbad_alloc#std##YAXXZ)
LNK2001 unresolved external symbol "void __cdecl std::_Xlength_error(char const *)" (?_Xlength_error#std##YAXPBD#Z)
LNK2001 unresolved external symbol "void __cdecl std::_Xout_of_range(char const *)" (?_Xout_of_range#std##YAXPBD#Z)
unresolved external symbol __dtest
unresolved external symbol __fdtest
unresolved external symbol __invalid_parameter)noinfo_noreturn
unresolved external symbol __stdio_common_vsnprintf_s
unresolved external symbol __stdio_common_vsprintf
unresolved external symbol __std_terminate
If I understand correctly the Xbad_alloc and Xlength_error are because of the new and delete operators? In that case I create my class instance like this:
class sc_Core
{
public:
static sc_Core *Instance(void);
func A();
public:
void *operator new(size_t si)
{
return HeapAlloc(GetProcessHeap(), NULL, si);
}
void operator delete(void *pv) throw()
{
HeapFree(GetProcessHeap(), NULL, pv);
}
private:
sc_Core(void) { }
~sc_Core(void) { }
sc_Core(const sc_Core&) { }
sc_Core(sc_Core&&) {}
sc_Core& operator=(const sc_Core&) {}
sc_Core& operator=(sc_Core&&) {}
static sc_Core *p_Instance;
};
// Global Scope
sc_Core *sc_Core::p_Instance = nullptr;
sc_Core *sc_Core::Instance(void)
{
if (p_Instance == nullptr)
p_Instance = new sc_Core();
return p_Instance;
}
If anyone knows how these occur or how I can fix them it would be very much appreciated!
There's nothing weird about those errors.
Compilers are allowed to assume that they can generate code that rely on their runtime libraries. If you're going omit the runtime library at the link step, then you'll have to provide definitions for the missing symbols in your own code.
Turning off certain compiler features may reduce the number of missing symbols. You could try, for example, turning off exception handling support to see if the internal exception classes are still expected. In debug builds, the compiler may expect even more support from the runtime that in release builds.
Related
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.
I'm trying to get started with C++ but I keep getting this error. I know which parts of my code is generating it, but I think that at least one these parts shouldn't generate them.
I am creating a class called Text that is functioning in a way similar to the std::string class, just to experiment and get a better understanding of value semantics.
Anyhow, these are my files:
Text.h:
#ifndef TEXT
#define TEXT
class Text {
public:
Text(const char *str);
Text(const Text& other);
void operator=(const Text& other);
~Text();
private:
int size;
char* cptr;
};
#endif
Text.cpp:
#include "Text.h"
#include <cstring>
#include <iostream>
using namespace std;
Text::Text(const char* str) {
size = strlen(str) + 1;
cptr = new char[size];
strcpy(cptr, str);
}
Text::Text(const Text& other) {
size = other.size;
cptr = new char[size];
strcpy(cptr, str);
}
void Text::operator=(const Text& other){
delete [] cptr;
size = other.size;
cptr = new char[size];
strcpy(cptr, other.ctpr);
}
Text::~Text() {
delete [] cptr;
}
Main.cpp:
#include <iostream>
#include "Text.h"
using namespace std;
Text funk(Text t) {
// ...
return t;
}
int main() {
Text name("Mark");
Text name2("Knopfler");
name = funk(name);
name = name2;
return 0;
}
So what's causing the error is the function funk, and the first two lines in the main function. I get why it's complaining on the first two lines in the main function, because there are no function called "name" or "name2". But what I'm trying to do is declaring and initialize an object in one line (I'm and old Java guy :p), is this even possible in C++? I can't find anything online indicating this.
The funny thing is that this code is more or less copied from some code my lecturer executes just fine during a lecture. And he has certainly not declared any functions named "name" and "name2" either. Any reasonable explanation for this?
But why is the function funk generating this error as well? All I am doing is returning a copy of the object that I'm sending in.
Thanks in advance!
Edit: Here comes the full error messages. There are five of them. "SecondApplication" is the name of my project.
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Text::Text(char const *)" (??0Text##QAE#PBD#Z) referenced in function _main C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication
Error 2 error LNK2019: unresolved external symbol "public: __thiscall Text::Text(class Text const &)" (??0Text##QAE#ABV0##Z) referenced in function "class Text __cdecl funk(class Text)" (?funk##YA?AVText##V1##Z) C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication
Error 3 error LNK2019: unresolved external symbol "public: void __thiscall Text::operator=(class Text const &)" (??4Text##QAEXABV0##Z) referenced in function _main C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication
Error 4 error LNK2019: unresolved external symbol "public: __thiscall Text::~Text(void)" (??1Text##QAE#XZ) referenced in function "class Text __cdecl funk(class Text)" (?funk##YA?AVText##V1##Z) C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication
Error 5 error LNK1120: 4 unresolved externals C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\Debug\SecondApplication.exe 1 1 SecondApplication
You'll get the link errors (not compilation errors) that you see if you, for instance, forget to add "Text.cpp" to your project so it doesn't get compiled and linked.
There are two errors in the code - one is in the copy constructor, and one is in the assignment operator.
Since the compiler didn't complain about the two errors, I suspect you forgot to add the file to the project.
I'm getting these error messages
2>main.obj : error LNK2019: unresolved external symbol "public: __thiscall CEngine::CEngine(void)" (??0CEngine##QAE#XZ) referenced in function _WinMain#16
2>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall CEngine::SetWindowSize(int,int,char const *,int)" (?SetWindowSize#CEngine##QAEXHHPBDH#Z) referenced in function _WinMain#16
2>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall CEngine::Begin(void)" (?Begin#CEngine##QAEXXZ) referenced in function _WinMain#16
2>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall CEngine::GetDisplayWidth(void)" (?GetDisplayWidth#CEngine##QAEHXZ) referenced in function _WinMain#16
2>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall CEngine::GetDisplayHeight(void)" (?GetDisplayHeight#CEngine##QAEHXZ) referenced in function _WinMain#16
2>C:\Users\ethan\Desktop\C++ Projects\delveenginetest\Debug\delveenginetest.exe : fatal error LNK1120: 5 unresolved externals
This is my solution:
Solution 'delveenginetest' (2 projects)
DelveEngine
Include
delve.h
Engine.h
SetupSDL.h
stdafx.h
Engine.cpp
Main.cpp
SetupSDL.cpp
This is the code for Engine.h
#pragma once
#include "SetupSDL.h"
class CEngine
{
public:
CEngine(void);
~CEngine(void);
void SetWindowSize(int winW, int winH, const char* GameName, int windowMode);
void Begin(void);
int GetDisplayWidth(void);
int GetDisplayHeight(void);
private:
int deskW;
int deskH;
bool playing;
CSetupSDL* sdl_setup;
};
Code for Engine.cpp
#include "Include/stdafx.h"
#include "Include/Engine.h"
CEngine::CEngine(void)
{
playing = true;
deskW = GetSystemMetrics(SM_CXSCREEN);
deskH = GetSystemMetrics(SM_CYSCREEN);
}
CEngine::~CEngine(void)
{
}
void CEngine::SetWindowSize(int winW, int winH, const char* GameName, int windowMode)
{
// set up SDL for use
sdl_setup = new CSetupSDL(winW, winH, GameName, windowMode);
}
void CEngine::Begin(void)
{
while (playing && sdl_setup->GetMainEvent()->type != SDL_QUIT)
{
sdl_setup->Begin();
sdl_setup->End();
}
playing = false;
}
int CEngine::GetDisplayWidth(void){ return deskW; }
int CEngine::GetDisplayHeight(void){ return deskH; }
The DelveEngine project builds successfully, whereas the delveenginetest project fails.
What's wrong? I've looked everywhere for a reason, can't find one that suits me.
Despite the fact you're not providing all the sufficient information for a correct diagnosis of your problems, I'll try to share what I could imagine that might be the reasons for the linker errors:
I suppose the project delveenginetest you mention is meant to set up unit tests for the classes from the DelveEngine project.
Since you have a Main.cpp in your DelveEngine project, I'd guess it's simply build as an executable (successfully).
Your delveenginetest needs to link to the classes provided from the DelveEngine project, but that's actually not possible, since the .exe from DelveEngine can't be used for linking, you'll need a library to import it to another executable (the unit testing framework).
I'd recommend to separate out your classes/source files from DelveEngine project to make up a static or shared library, that can be linked from an application and the test framework simultaneously from within a single VS solution:
Solution 'DelveEngine' (3 projects)
DelveEngineLib (project [.lib/.dll])
Include
delve.h
Engine.h
SetupSDL.h
Engine.cpp
SetupSDL.cpp
DelveEngine (project [.exe])
Main.cpp
delveenginetest (project [.exe])
Main.cpp (TestFramework main definition)
Since I'm not very versed with it I don't know actually, if VS 2013 supports to setup projects consuming virtual resources (think of links to sources in the actual build environment), but this could be an alternative how to setup application and unit tests without need of an extra library.
hey guys i am working on an application and i removed the CRT to save alot of space in the executable and making it as small as possible :) the thing is that when i removed the CRT i also received tons of errors on unresolved external and i was able to remove most of them just by adding a few operators like these:
void * __cdecl operator new(unsigned int bytes) {
return HeapAlloc(GetProcessHeap(), 0, bytes);
}
void __cdecl operator delete(void *ptr) {
if(ptr) HeapFree(GetProcessHeap(), 0, ptr);
}
extern "C" int __cdecl __purecall(void) {
return 0;
}
extern "C" const DWORD_PTR __security_cookie = 0xE64EBB40;
extern "C" void __fastcall __security_check_cookie(DWORD_PTR cookie) {
if (cookie != __security_cookie)
__asm int 3;
}
but now i am stuck with the last three errors and i have no clue on how to solve them, and one that i am really curious of is the _memmove error ? i am not using the memmove operator anywhere in my code so i have not clue why i am receiving it :P
atleast here are the errors, i would be very greatefull for your answers.
Error 2 error LNK2001: unresolved external symbol "void __cdecl std::_Xbad_alloc(void)" (?_Xbad_alloc#std##YAXXZ) C:\Users\Fluttershy!\documents\visual studio 2012\Projects\PincelStub\PincelStub\PincelStub.obj
Error 3 error LNK2001: unresolved external symbol "void __cdecl std::_Xlength_error(char const *)" (?_Xlength_error#std##YAXPBD#Z) C:\Users\Fluttershy!\documents\visual studio 2012\Projects\PincelStub\PincelStub\PincelStub.obj
Error 4 error LNK2001: unresolved external symbol _memmove C:\Users\Fluttershy!\documents\visual studio 2012\Projects\PincelStub\PincelStub\PincelStub.obj
VC++ probably uses those internally (the first two to signal for error conditions and _memmove, well, to move memory blocks around in plain old struct assignments, for instance). The first two, I would just define as {}, but as for the last, I would try fiddling with optimization options (something about "intrinsic functions" etc.) or reimplement it fully (not just stub it).
I'm not sure why you want to shoot yourself in the leg, but whatever. The source of the CRT sits there, so you can just search it for the missing stuff and copy. the memmove function should be obvious. The other two looks like connected to standard exceptions.
I have this piece of code (file luascript.cpp):
bool LuaInterface::initState()
{
m_luaState = luaL_newstate();
if(!m_luaState)
return false;
luaL_openlibs(m_luaState);
#ifdef __LUAJIT__
luaJIT_setmode(m_luaState, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_ON);
#endif
registerFunctions();
if(!loadDirectory(getFilePath(FILE_TYPE_OTHER, "lib/"), false, true))
std::clog << "[Warning - LuaInterface::initState] Cannot load " << getFilePath(FILE_TYPE_OTHER, "lib/") << std::endl;
lua_newtable(m_luaState);
lua_setfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
m_runningEvent = EVENT_ID_USER;
return true;
}
the declaration (file luajit.h):
LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode);
and the error is:
1>luascript.obj : error LNK2019: unresolved external symbol _luaJIT_setmode referenced in function "public: virtual bool __thiscall LuaInterface::initState(void)" (?initState#LuaInterface##UAE_NXZ)
1>C:\Users\GUIAKI\Documents\trunk.r5918\vc10\Debug\tfs.exe : fatal error LNK1120: 1 unresolved externals
How can I solve it?
Simply remove that line.
You can't link against plain Lua, if you keep it. And if you link against LuaJIT, the JIT compiler is enabled by default, anyway. That line of code is utterly pointless.
Seems like you forgot to link the library being part of "luaJIT" (never heard of it or used it).
There should be a lib file you'll have to add to your project as an additional dependency (linker settings).
Also keep in mind to include the correct headers ("lua.hpp" for C++, "luajit.h" for C).