undefined symbol

what is an undefined symbol error?
Last edited on
It means you haven't written a function, or you haven't created a variable, or you haven't linked against the library or object code that contains the missing function or variable.

It means the linker has looked through all the compiled code you told it to, and it still can't find what it's looking for.
Last edited on
Weird, I have a series of database which are struct's and all of those are contained in one include file. Those structs are used by multiple cpp's. Originally I was getting a duplicate symbol error so I rewrote it to use extern and now it is generating an undefined symbol error. What is the best way to go about fixing this?
In the header file, have the definition of the struct but do not actually create it.

In one single cpp file, create the instance you want to use (and share amongst all the other cpp files).

In all other cpp files that need to use this one struct, declare it extern.

Here's an example:

header2.h
1
2
3
4
struct food{
  int eggs;
  int bananas;
};


one.cpp
1
2
3
4
5
6
7
8
#include "header2.h"

extern food someFood;

void addEggs()
{
  someFood.eggs++;
}


two.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "header2.h"
#include <iostream>
food someFood;
void addEggs();
int main()
{
  someFood.eggs = 4;
  addEggs();
  std::cout << someFood.eggs;
  return 0;
}


would making all the struct's class's instead work as a fix to this? is that a dumb move?
A struct is identical to a class in C++, except for default privacy and default privacy inheritance. It will make no difference.The problem is not what kind of object it is.

The proper way to deal with this would be to have one object created in one place and pass it as a parameter to whichever functions need it.
Last edited on
Well I did what you said above, however I am getting a duplicate symbol error on on of my instances.. These errors are getting real annoying and I dont know what to do
I am getting a duplicate symbol error on on of my instances


You're only supposed to have one instance. Just one. All the rest should be marked with extern.
First off, thank you so much for your help. Secondly I am trying to have one instance accessible from multiple cpp's. I have a header file which has the definition of the structs, I have a cpp which has the the extern definition of the instance extern food somefood, I have another cpp which has the instance just defined food somefood and some functions that use it and I have another cpp which also has food somefood and some functions. What is wrong here?
I have another cpp which has the instance just defined food somefood and some functions that use it and I have another cpp which also has food somefood and some functions. What is wrong here?


Two cpp which define food somefood. Should be just one. All the rest should be marked with extern.

header2.h
1
2
3
4
struct food{
  int eggs;
  int bananas;
};



one.cpp

1
2
3
4
5
6
7
8
#include "header2.h"

extern food someFood;

void addEggs()
{
  someFood.eggs++;
}



two.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "header2.h"
#include <iostream>
food someFood;
void addEggs();
int main()
{
  someFood.eggs = 4;
  addEggs();
  std::cout << someFood.eggs;
  return 0;
}


three.cpp
1
2
3
4
5
6
7
8
#include "header2.h"

extern food someFood;

void subtractEggs()
{
  someFood.eggs--;
}


four.cpp
1
2
3
4
5
6
7
8
#include "header2.h"

extern food someFood;

void addBananas()
{
  someFood.bananas++;
}


five.cpp
1
2
3
4
5
6
7
8
#include "header2.h"

extern food someFood;

void subtractBananas()
{
  someFood.bananas--;
}
Last edited on
Thank you for clarifying that was incredibly helpful.
Note that global variables are not a good thing and it's far better to write it such that functions that need to work on a variable have that variable passed to them, rather than fetching it from global namespace. Like this:

1
2
3
4
5
#include "header2.h"
void subtractBananas(food& someFood)
{
  someFood.bananas--;
}
Topic archived. No new replies allowed.