I have a problem. I get an error and I’m not sure why it’s happening.
2>Home.obj : error LNK2019: unresolved external symbol "**void __cdecl LogAString(char *,...)**" (?LogAString@@YAXPADZZ) referenced in function "**public: static void __cdecl X::Home::HomeStart(void)**" (?HomeStart@Home@X@@SAXXZ)
2>Widget.obj : error LNK2001: unresolved external symbol "void __cdecl LogAString(char *,...)" (?LogAString@@YAXPADZZ)
2>J:\src\out.dll : fatal error LNK1120: 1 unresolved externals
Here’s my code:
Log.h
#pragma once
#include <iostream>
#include <cstdarg>
void LogAString(char* fmt, ...);
void LogAnError(char* fmt, ...);
Log.cpp
#include "Log.h"
#include <Util/String/String Formatting.h> // defines format(). Does not have any errors or issues.
void LogAString(char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
};
void LogAnError(char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
auto formatted_string = format("ERROR: %s", fmt).c_str();
LogAString(const_cast<char*>(formatted_string), ap);
va_end(ap);
};
Home.cpp (extract)
#include "Home.h"
#include "Log.h"
namespace X {
void Home::HomeStart()
{
while (true)
{
auto number_of_widgets = Widgets::Count();
LogAString("Loading with %d widgets", number_of_widgets);
}
}
} // namespace X
I thought I’ve declared and defined the functions in the header and cpp files respectively. Why am I getting these errors? I’ve been at it for a few hours now, and still not sure why this is happening. Using VC++ on VS 2010.
I’m not using any other external libraries at this point. The compile target is a DLL, “out.dll”.
Did you remember to add
Log.cppto your project?If so, then open the file
log.objin a hex editor. Search for the stringLogAnError. It will be part of a larger decorated string. Use theundnamecommand to undecorate it. Compare it with what the linker cannot resolve. Identify the difference and fix yourLogAnErrorfunction so they match again.