in function (...) undefined reference to a variable (....)

I'm making my first library for some sensor
In function MPUDataScale()': and when I compile it it says undefined reference to AcX_out'

AcX_Out is a global variable declared in the h file, defined in the MPUsetup function which is implemented in the cpp file

the variables are declared as extern variables

This error actually is getting for almost all the functions with all the variable :sweat_smile:

I looked up the other threads that talked about the same error
but I kind spot my problem in my code yet !

MPU.h

//				Global variables 

		extern double timestep,TimeC;
		extern int check;
		extern double AAngX, AAngY, AAngZ, GAngX, GAngY, GAngZ, AngX, AngY, AngZ;
		extern int accel_x_OC, accel_y_OC, accel_z_OC, gyro_x_OC ,gyro_y_OC, gyro_z_OC;
		
		extern int AcX_out , AcY_out, AcZ_out, GyroX_out, GyroY_out, GyroZ_out, Tmp_out;
		extern double AcX, AcY, AcZ;
		extern double Tmp;
		extern double GyX, GyY, GyZ;

MPU.cpp

void MPUSetup(void){
	timestep=0;
	TimeC=0;
	check =0;
	accel_x_OC=0;
	accel_y_OC=0;
	accel_z_OC=0;
	gyro_x_OC=0;
	gyro_y_OC=0;
	gyro_z_OC=0;
	Wire.begin();
	Wire.beginTransmission(MPU_ADDRESS);// Start transmission with the MPU sensor
	Wire.write(PWRMANAGE1); 			// PWR_MGMT_1 register
	Wire.write(0x00);     		 		// set to zero (wakes the sensor up, enables the temperature sensor)
	Wire.endTransmission();
		
}//MPUSetup


void MPUDataScale(void){
	AcX = AcX_out/ACSCALE; 
	AcY = AcY_out/ACSCALE;
	AcZ = AcZ_out/ACSCALE;
	Tmp = Tmp_out /340.00+36.53;		//equation for temperature in degrees C from DataSheet1 page 31 of register description
	GyX = GyroX_out/GYSCALE;
	GyY = GyroY_out/GYSCALE;
	GyZ = GyroZ_out/GYSCALE;
}//MPUDataScale

I'm not copying the whole code .. however I get the same error for all the functions and with all the variables

my first thought is that it's about the way of variables decleration maybe .. still can't get it

Thanks all in advance :))

I think it's some problem with the function being declared as global variables and being used in the functions without passing them through to the function ??

Shouldn't the global variables be seen in all scopes ?

Sorry. Misread the code.

I'm only showing the global variables decleration part form the header file
and two function of 5 from the cpp code

If that would make it better for you

How would make it easier for you :)) ?

Where are all those extern variables actually defined? What file is that happening in?

not all of them are giving an initial value

e.g:
This is one function i didn’t put up there

void ReadTheData(void){
	Wire.beginTransmission(MPU_ADDRESS);
	Wire.write(ACCELXH);
	Wire.endTransmission();
	Wire.requestFrom(MPU_ADDRESS,14);
	if(Wire.available () == 14) {
		AcX_out=Wire.read()<<8|Wire.read();
		AcY_out=Wire.read()<<8|Wire.read();
		AcZ_out=Wire.read()<<8|Wire.read();
		Tmp_out=Wire.read()<<8|Wire.read();
		GyroX_out=Wire.read()<<8|Wire.read();
		GyroY_out=Wire.read()<<8|Wire.read();
		GyroZ_out=Wire.read()<<8|Wire.read();
	}
}//ReadTheData

it reads the data from registers of a sensor store them in some variables of the shown above

then in function ScaleData >> it just uses the same variables divide them by some value and save them in another ones

C/C++ is case sensitive X is not the same as x nor is C the same as c. AcX-out is not the same as .....

Mark

yes , i'm not having this particular problem

i've been using this very code for so long and it does work
only today i needed to create a library our of it so it would make it easier for further use

the variables declared and the ones defined and used in all the functions are the same

extern Means that the variable will be defined elsewhere. So extern int AcX_out is just informing the compiler that an integer variable with the name AcX_out WILL be defined somewhere else in the code. Usually it should be defined in the .cpp file that comes with the .h

and that's exactly what i've done :confused:

YahyaGilany:
and that’s exactly what i’ve done :confused:

Not in the .cpp you’ve posted at the top of this thread. Your function MPUDataScale(void) has a local variable with the same name, but you haven’t declared any global variables.[/s]

Edit. Sorry that bit was just wrong.

I'm sorry I don't see what variable do you mean ?

if you mean ACSCALE this is actually a number being defined with #define not really a variable

This is the line that the compiler is complaining about. It's the first line of your function `MPUDataScale()

AcX = AcX_out/ACSCALE;

So here you are attempting the read the value of a variable with the name ACX_out. Yet it hasn't been defined anywhere in your code. The reference to ACX_out in your .h file uses the directive extern So this doesn't count. You still need to define it somewhere before you can use it.

YahyaGilany:
In function MPUDataScale()': and when I compile it it says undefined reference to AcX_out’

AcX_Out is a global variable declared in the h file, defined in the MPUsetup function which is implemented in the cpp file .

AcX_out and AcX_Out are two different variables. Looks like you declared AcX_Out and tried to use AcX_out.

johnwasser:
AcX_out and AcX_Out are two different variables. Looks like you declared AcX_Out and tried to use AcX_out.

But he hasn't defined either of them. In fact, if you look through the code posted in #1, he hasn't defined a single global variable anywhere.

His .cpp file should look something like this.

//  This is the bit that's missing from YahyaGilany's .cpp file

double timestep = 42;
double TimeC=42;
int check=42;
double AAngX =42.0;
double AAngY =42.0;
double AAngZ =42.0;
double GAngX =42.0;
double GAngY =42.0;
double GAngZ =42.0;
double AngX =42.0;
double AngY =42.0;
double AngZ =42.0;
int accel_x_OC = 42;
int accel_y_OC =42;
int accel_z_OC =42;
int gyro_x_OC =42;
int gyro_y_OC =42;
int gyro_z_OC =42;
int AcX_out =42; 
int AcY_out =42;
int AcZ_out =42;
int GyroX_out =42;
int GyroY_out =42;
int GyroZ_out =42;
int Tmp_out =42;
double AcX =42.0;
double AcY =42.0;
double AcZ =42.0;
double Tmp =42.0;
double GyX =42.0;
double GyY =42.0;
double GyZ =42.0;



void MPUSetup(void){
	timestep=0;
	TimeC=0;
	check =0;
	accel_x_OC=0;
	accel_y_OC=0;
	accel_z_OC=0;
	gyro_x_OC=0;
	gyro_y_OC=0;
	gyro_z_OC=0;
	Wire.begin();
	Wire.beginTransmission(MPU_ADDRESS);// Start transmission with the MPU sensor
	Wire.write(PWRMANAGE1); 			// PWR_MGMT_1 register
	Wire.write(0x00);     		 		// set to zero (wakes the sensor up, enables the temperature sensor)
	Wire.endTransmission();
		
}//MPUSetup


void MPUDataScale(void){
	AcX = AcX_out/ACSCALE; 
	AcY = AcY_out/ACSCALE;
	AcZ = AcZ_out/ACSCALE;
	Tmp = Tmp_out /340.00+36.53;		//equation for temperature in degrees C from DataSheet1 page 31 of register description
	GyX = GyroX_out/GYSCALE;
	GyY = GyroY_out/GYSCALE;
	GyZ = GyroZ_out/GYSCALE;
}//MPUDataScale