-1

I have defined an extern and a struct in my header file, however whenever i try to use use functions in my my CPP file, I get this linker error:

LNK2001 unresolved external symbol "struct API Api" (?Api@@3UAPI@@A)

struct API
{
    HMODULE Kernel32;
    HMODULE User32;
    HMODULE Ws32_32;
    HMODULE Advapi32;
};

extern API Api;
user207421
  • 305,947
  • 44
  • 307
  • 483
MinCarve
  • 75
  • 1
  • 3
  • 15
  • Well, where did you define the object that `extern` declaration promises should exist? – StoryTeller - Unslander Monica Jan 14 '18 at 08:52
  • The linker says that it can't find a definition of an object named `Api` of type `struct API`. – Ulrich Eckhardt Jan 14 '18 at 08:52
  • nope, the answer didnt answer my question – MinCarve Jan 14 '18 at 11:53
  • There are 27 answers, and [one of them](https://stackoverflow.com/a/12574403/1889329) addresses the very issue you were asking about. – IInspectable Jan 14 '18 at 21:20
  • Nope, this issue was with structs, and that link shows the same code as my original post lmao – MinCarve Jan 14 '18 at 21:22
  • Well declaring API as Api in a cpp file is different to no declaration whatsoever, his code is identical to my question's code, as you can see, the only difference is int from struct, the issue has been resolved, so your comments are unnecessary – MinCarve Jan 14 '18 at 21:27
  • This issue has nothing to do with a missing declaration. If that were the case, you'd get a compiler error. The linker is complaining about a missing definition. Have a look at [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242/1889329) to learn about the difference. – IInspectable Jan 14 '18 at 21:31
  • ??? lol. Try compile this code without the declaration, you newb. You will get a Lnk error – MinCarve Jan 14 '18 at 21:33
  • There is no code in your question, that I could compile. If there was, [I'd get a compiler error](http://coliru.stacked-crooked.com/a/4bd0a2953bb29b6a). – IInspectable Jan 14 '18 at 21:43
  • That is not a coherent response. My header "code" is right below my error – MinCarve Jan 14 '18 at 21:50
  • You cannot compile headers. Header files do not constitute compilation units. That, too, is explained in some of the books I suggested earlier. Then again, being a *"newb"*, what do I know? – IInspectable Jan 14 '18 at 22:07
  • Did I say that you can compile headers? – MinCarve Jan 14 '18 at 22:09
  • Why, yes! I complained, that there were no code I could compile, which prompted you to respond with: *"My header 'code' is right below my error"*. Either you are deliberately trying to be incoherent, or you strongly suggested, that I were to compile a header by itself. At any rate, you have all the information you need. This question was marked as a duplicate, because it is. And it was down-voted because it *"does not show any research effort"*. Friendly advice for the future: Leave out the personal attacks, unless you are prepared to see your account temporarily suspended. – IInspectable Jan 14 '18 at 22:14
  • I didn't leave any personal attacks, other than a simple "newb". You could have tried including the header file in your mainsource.cpp file, or whatever you called it, and try compile your program with an extern API Api, and without it, instead of trying that, you rambled about how header files cannot be compiled, which is information that I already knew. It wasn't obligatory for you to ****post an entire thread of comments talking about how my original post had an imperfection in it, I already accepted an answer, which shows a bit of ignorance from your part – MinCarve Jan 14 '18 at 22:25
  • My bad, I meant an API Api declaration, instead of extern API Api – MinCarve Jan 14 '18 at 22:29
  • That is a *definition*, not a *declaration*. The ignorance here is to post an answer, when an answer already exists. Instead, this question should have been marked as a duplicate. That's how Stack Overflow works. The issue with your question is, that it doesn't come with a [mcve]. – IInspectable Jan 14 '18 at 22:32

1 Answers1

1

The code you have shown does declare that external symbol.
The code does not however define the external symbol.
At least one of the code files you compile for building your program has to define it, e.g. like this:

API Api;

Note the absence of extern here in this one case.

All other code files then see the declaration you have shown and get compiled.
Finally the linker can have all those code files access the struct variable, based on the one case of definition.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54