Home c++ Error LNK2001: unresolved external symbol

Error LNK2001: unresolved external symbol

Author

Date

Category

I’m doing a textbook assignment. You need to create 3 files:

my.h:

# pragma once
extern int foo;
void print_foo ();
void print (int);

my.cpp:

# include & lt; std_lib_facilities.h & gt;
#include "my.h"
void print_foo ()
{
  cout & lt; & lt; foo & lt; & lt; '\ n';
}
void print (int i)
{
  cout & lt; & lt; i & lt; & lt; '\ n';
}

use.cpp:

# include & lt; iostream & gt;
#include "my.h"
int main ()
{
  foo = 7;
  print_foo ();
  print (99);
  char cc;
  std :: cin & gt; & gt; cc;
}

Errors:

my.obj: error LNK2001: unresolved external symbol “” int foo “(? foo @@ 3HA)”
use.obj: error LNK2001: unresolved external symbol “” int foo “(? foo @@ 3HA)”
E: \ Projects \ OWN \ C++ \ Programming. Principles and Practice Using C++ \ 8 \ Task \ 1 \ Debug \ 1.exe: fatal error LNK1120: Unresolved External Items: 1


Answer 1

Well, where is the definition foo ? E you only ad, in free translation

extern int foo;

sounds like “work, assuming that int foo; is, only somewhere else. I’ll define it there.”

But the promise was not fulfilled – there is no simple one in any .cpp-file

int foo;

or there

int foo = 10;

Keep what you promised 🙂

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions