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?