0

I'm aware that this question has been asked multiple times before, but I can't get this to work. For a special case I need to have a global struct that will be filled in one file, and read in another.

The following code gives me a linking error:

Undefined symbols for architecture x86_64:
  "_camera_data", referenced from:
      MyCameraData::return_sensor_shift() in extra-424af5.o
ld: symbol(s) not found for architecture x86_64
clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)

common.h:

#ifndef COMMON_H
#define COMMON_H
struct MyCameraData
{
    float sensor_shift;
    float return_sensor_shift();
};
extern MyCameraData camera_data;
#endif

main.cpp:

#include "common.h"
#include <iostream>

int main(){
    MyCameraData camera_data;

    camera_data.sensor_shift = 5.0;

    std::cout << camera_data.return_sensor_shift() << std::endl;
}

extra.cpp:

#include "common.h"

float MyCameraData::return_sensor_shift(){
    return camera_data.sensor_shift;
}

There must be something I'm overlooking. Any ideas?

zeno
  • 81
  • 8
  • 5
    You declared `camera_data` as `extern`, which means "trust me, it's defined elsewhere". But you never defined it anywhere. – Igor Tandetnik Dec 21 '17 at 18:52
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – user0042 Dec 21 '17 at 18:52
  • Cheers, that was it. Rookie error! – zeno Dec 21 '17 at 18:54
  • In the `main` function you define a *local* variable in the scope of the `main` function only. It is *not* the global variable you declared in the header file. – Some programmer dude Dec 21 '17 at 18:54
  • 1
    Furthermore, your `return_sensor_shift` member function makes no sense. It should not return the member variable from the global object, but from *this* object, simply by doing `return sensor_shift`. It seems you could use [a couple of good beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to read. – Some programmer dude Dec 21 '17 at 18:56
  • A getter `return_sensor_shift();` but the variable is public? –  Dec 21 '17 at 19:26

2 Answers2

2

You could move

MyCameraData camera_data;

from main to global scope in main.cpp to resolve the linker error. However, I suggest creating a file common.cpp and defining the variable in that file. That is cleaner than defining it in main.cpp.

common.cpp:

#include "common.h"
MyCameraData camera_data;

main.cpp:

#include "common.h"
#include <iostream>

int main()
{
    camera_data.sensor_shift = 5.0;
    std::cout << camera_data.return_sensor_shift() << std::endl;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
2

This part is okay:

Common.h

#ifndef COMMON_H
#define COMMON_H
struct MyCameraData
{
    float sensor_shift;
    float return_sensor_shift();
};
extern MyCameraData camera_data;
#endif

Since you declared camera_data as extern in the header file which is only a declaration you are missing its definition.

To fix this add this file:

Common.cpp

#include "Common.h"
MyCameraData camera_data;

And that should fix your problem.

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59