c++extern

Calling extern function from dll declared in (.exe) - error LNK2019: unresolved external symbol


This is my first question, so hello all!

I'm having an issue with extern function "extern void TestExtern();" it is defined as extern in .h file of my shared library (.dll) and is called from cpp. That function is declared (implemented) in cpp file of my application (.exe) project.

This is output with Core Show Progress set to /VERBOSE

1>Microsoft (R) Windows (R) Resource Compiler Version 10.0.10011.16384
1>
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>
1>
1>Creating C:\Users\danij\AppData\Local\Temp\lnk{13F88671-C637-469A-93A9-78E4106F389E}.tmp
1>
1>Using codepage 1252 as default
1>
1>C:\Users\danij\AppData\Local\Temp\lnk{2B156ACF-B9B2-4480-81AD-3C450DB4EB02}.tmp.
1>Writing 24:2, lang:0x409, size 381
1>Invoking cvtres.exe:
1> /machine:amd64
1> /verbose
1> /out:"C:\Users\danij\AppData\Local\Temp\lnk{97C07E2B-0967-433E-A12F-000334BDC2DB}.tmp"
1> /readonly
1> "C:\Users\danij\AppData\Local\Temp\lnk{13F88671-C637-469A-93A9-78E4106F389E}.tmp"
1>Microsoft (R) Windows Resource To Object Converter Version 14.39.33523.0
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>
1>adding resource. type:MANIFEST, name:2, language:0x0409, flags:0x30, size:381
1>Core.obj : error LNK2019: unresolved external symbol "void __cdecl TestExtern(void)" (?TestExtern@@YAXXZ) referenced in function "int __cdecl TestingRestructure(void)" (?TestingRestructure@@YAHXZ)
1>Unused libraries:
1>  Vendor\SDL3_Image\lib\x86_64\SDL3_Image.lib
1>  Vendor\SDL3_Mixer\lib\x86_64\SDL3_Mixer.lib
1>  Vendor\SDL3_TTF\lib\x86_64\SDL3_TTF.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x64\gdi32.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x64\winspool.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x64\comdlg32.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x64\advapi32.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x64\shell32.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x64\ole32.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x64\oleaut32.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x64\uuid.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x64\odbc32.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x64\odbccp32.lib
1>  C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\lib\x64\OLDNAMES.lib
1>..\Binaries\windows-x86_64\Debug\Core\Core.dll : fatal error LNK1120: 1 unresolved externals
1>Done building project "Core.vcxproj" -- FAILED.
2>------ Build started: Project: AppTest, Configuration: Debug x64 ------
2>AppTest.cpp
2>AppTest.vcxproj -> D:\GithubProjects\janjitest\janji\Binaries\windows-x86_64\Debug\AppTest\AppTest.exe
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 12:55 PM and took 00.816 seconds ==========

Here is my Solution structure:

  Solution:
     Core:
        Core.h
        Core.cpp
     AppTest:
        AppTest.h
        AppTest.cpp

Core.h

#pragma once

#include "Defines.h"

COREAPI int TestingRestructure();

extern void TestExtern();

Core.cpp

#include "Core.h"
#include "Defines.h"

int TestingRestructure()
{
    TestExtern();

    return 0;
}

AppTest.h

#pragma once

AppTest.cpp

#include "AppTest.h"

#include <Core/Core.h>

int main(void)
{
     return TestingRestructure();
}

void TestExtern()
{
    
}

My idea here is that I want to call function from dll which is implemented in exe. What am I doing wrong here?

Thank you for your time,

Daniel.

I tried to call extern function defined in .h file of my dll and call it from cpp but implement it inside of exe cpp file. What I got is error LNK2019: unresolved external symbol and expected that function is properly linked because its defines as extern.


Solution

  • Unlike shared objects (so) or static libs, a DLL must get all its "missing" symbols on linkage, since it is a closed compilation unit.

    You left an extern symbol on the DLL which you didn't supply on the DLL linkage time, and it caused the DLL linkage to fail. you did supply the missing symbol in the executable, but it was too late, the DLL already failed to link and was not created.