简体   繁体   中英

Unresolved external symbols issue

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".

This is a linker error, not a compiler error. That means you are correct that you properly referenced the header file in your code. As a matter of fact, your code compiled successfully.

But, then the linker went out to find the referenced functions in the libraries it was pointed to, and came back empty handed. Library references are defined in the property sheet for your VC++ project. Is your project outputting an Out.dll? It looks like the linker expects one. I would investigate the Linker section as well as what file your compilation is generating.

Post more info on your build and solution / project configuration, or even better exactly what is in your property sheets if that wasn't enough information.

Did you remember to add Log.cpp to your project?

If so, then open the file log.obj in a hex editor. Search for the string LogAnError . It will be part of a larger decorated string. Use the undname command to undecorate it . Compare it with what the linker cannot resolve. Identify the difference and fix your LogAnError function so they match again.

也许名称空间与此有关,它在没有名称空间X的情况下是否可以工作?

OK so based on your comment :

The compile target is a DLL, "out.dll"

I would assume that you are using this out.dll at some other project. And when you try to do this you get the above linker errors. If this is the case , this happens because you don't export your functions. In addition having global functions like this is a bad practice. You should at least wrap them in some class eg Utils or somethng and declare them static :

Example Log.h :

#pragma once

#include <iostream>
#include <cstdarg>

class __declspec (dllexport) Utils
{
    public:
          static void LogAString(char* fmt, ...);
          static void LogAnError(char* fmt, ...);
};

Log.cpp should remain almost the same.

#include "Log.h"

#include <Util/String/String Formatting.h> // defines format(). Does not have any errors or issues.

void Utils::LogAString(char* fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    vprintf(fmt, ap);
    va_end(ap);
};

void Utils::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);
};

Now when you use your .dll your functions will be exported and available to your other projects. You should also include the Log.h file in your include directories, and make sure that the "out.dll" is in the same output folder of your main project. Additionally you should add the out.lib to the additional libraries.

Hopefully this is your problem. Next time provide more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM