0

Build error LNK2019 happens when building project with 1 cpp file



namespace foo {

    __declspec(dllimport) void Print();

}


void main()
{
    foo::Print();
}

foo is defined.

header (in other folder):

#pragma once

namespace foo {

    __declspec(dllexport) void Print();

}

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "__declspec(dllimport) void __cdecl foo::Print(void)" (_imp?Print@foo@@YAXXZ) referenced in function main sb C:\Users\user\source\repos\foo\sb\Application.obj 1

context if needed: following a VS2017 guide. I don't know if something might have changed.

mmd
  • 1
  • Did you add the lib file where foo is implemented to this project? – Dave S Jun 14 '23 at 22:13
  • @DaveS isn't it supposed to be generated when I build it? – mmd Jun 14 '23 at 22:23
  • 2
    You say "header (in other folder):" -- so you have 2 folders, one creates a DLL, this one uses the DLL? The one project creates a lib file, the other project needs to add the path to that created lib file in order to use it. Just the H file is not enough, you need H + LIB. https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-static-library-cpp?view=msvc-170 – Dave S Jun 14 '23 at 22:24
  • Right. The compiler GENERATED the lib when you built the DLL, but it's not in a place where your EXE project can find it. – Tim Roberts Jun 14 '23 at 22:31
  • ***following a VS2017 guide. I don't know if something might have changed.*** No, Creating a dll has been the same for over 20 years. My advice is to start with this Microsoft Tutorial: [https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170](https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170) – drescherjm Jun 15 '23 at 01:38
  • *"following a VS2017 guide"* On the other hand, if you are learning C++, and don't specifically **have to** use VS2017, a lot of other things have changed in the language since 2017. Using the latest version is *strongly* recommended - saves you from asking "Why doesn't this work?" just to get the answer "It does now!". – BoP Jun 15 '23 at 08:14

0 Answers0