Search code examples
visual-c++dllcomlinkeratl

Why won't my external library method resolve?


I have a DLL (example.dll) built in C which has this function in it:

void WINAPI free_job()
{
    lc_free_job(jobPtr);
}

I've built this DLL and linked example.lib into my Visual C++ project. Now I am trying to call the free_job() method on it from an ATL project with a simple COM object. Here is a part of my header that references the library:

#include "..\lib\example.h"

This is where I call it in my implementation:

STDMETHODIMP CCerberusSession::Free(CHAR* licensePath, CerberusErrorDetails* error)
{
    free_job();
    return S_OK;
}

It won't compile. I get the following errors:

Error LNK2019 unresolved external symbol "void __stdcall free_job(void)" (?free_job@@YGXXZ) referenced in function "public: virtual long __stdcall CCerberusSession::Free(char *,struct CerberusErrorDetails *)" (?Create@CCerberusSession@@UAGJPADPAUCerberusErrorDetails@@@Z)

What am I doing wrong and how can I troubleshoot or fix it? Any ideas would be helpful and appreciated.

Edit: I have tried to wrap the include like so:

extern "C" {
#include "..\lib\example.h"
}

But I get the following error if I try to compile it like this:

Error MSB8011 Failed to register output. Please try enabling Per-user Redirection or register the component from a command prompt with elevated permissions.

I am running Visual Studio as an Administrator. What else could be wrong here?


Solution

  • The first issue was I needed to wrap that #include statement (thank you for pointing that out, Hans):

    extern "C" {
    #include "..\lib\example.h"
    }
    

    The second issue was due to regsvr32.exe. It can't find the dependent DLL which my application is importing because that DLL is not in the same output directory as my compiled ATL project. For example, if you check the Output tab in Visual Studio, you will see something like this:

    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(1749,5): warning MSB3073: The command "regsvr32 /s "C:\Code\Cerberus\Debug\Cerberus.dll"" exited with code 3.
    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(1761,5): error MSB8011: Failed to register output. Please try enabling Per-user Redirection or register the component from a command prompt with elevated permissions.
    

    I just needed to include example.dll in the output directory: C:\Cerberus\Debug\.