Undefined Reference to global variable - Linker Error in KDS 3.0

cancel
Showing results for 
Search instead for 
Did you mean: 

Undefined Reference to global variable - Linker Error in KDS 3.0

Jump to solution
1,466 Views
kevynschwab
Contributor II

I am having issues building out my project. The console gives the error: "undefined reference to 'uartCommunication'", where uartCommunication is an external global structure. Here is the code that instantiates the structure:

/*

  * UartCommunication.h

  */

typedef struct {

  uint16_t timeOfFlightDifference;

  uint16_t timeOfFlightDown;

  uint16_t timeOfFlightUp;

} uartCommunication_parameters;

void uartCommunication_sendTimeOfFlightData(uartCommunication_parameters *uart);

UartCommunication.h is then included in another source file, where the error occurs. Here is the inclusion and declaration:

#include "UartCommunication.h"

extern uartCommunication_parameters uartCommunication;

uartCommunication_parameters *uart;

I have traced the error to this line of code, which upon commenting out removes the error:

uartCommunication_sendTimeOfFlightData(&uartCommunication);

That line of code is present in the function, Scheduler_serviceInterrupt, as indicated by the error below:

'Invoking: Cross ARM C++ Linker'

arm-none-eabi-g++ -mcpu=cortex-m0plus -mthumb -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections  -g3 -T "C:/Users/kschwab/workspace.kds/Ultrasonic Flow Sensor/Project_Settings/Linker_Files/ProcessorExpert.ld" -Xlinker --gc-sections -L"C:/Users/kschwab/workspace.kds/Ultrasonic Flow Sensor/Project_Settings/Linker_Files" -Wl,-Map,"Ultrasonic Flow Sensor.map" -specs=nano.specs -specs=nosys.specs -o "Ultrasonic Flow Sensor.elf"  ./Sources/Events.o ./Sources/Scheduler.o ./Sources/SpiCommunication.o ./Sources/UartCommunication.o ./Sources/exit.o ./Sources/main.o ./Sources/myMain.o  ./Project_Settings/Startup_Code/startup.o  ./Generated_Code/AS1.o ./Generated_Code/ASerialLdd1.o ./Generated_Code/BitIoLdd1.o ./Generated_Code/BitIoLdd2.o ./Generated_Code/BitIoLdd3.o ./Generated_Code/BitIoLdd4.o ./Generated_Code/BitIoLdd5.o ./Generated_Code/BluLedDout.o ./Generated_Code/Cpu.o ./Generated_Code/GrnLedDout.o ./Generated_Code/Maxim_pullUpResistor.o ./Generated_Code/PE_LDD.o ./Generated_Code/RedLedDout.o ./Generated_Code/RtiTimer.o ./Generated_Code/SInt.o ./Generated_Code/SMasterLdd1.o ./Generated_Code/SlaveSelect.o ./Generated_Code/TU1.o ./Generated_Code/TimerIntLdd1.o ./Generated_Code/UltrasonicFlowSPI.o ./Generated_Code/Vectors.o ./Generated_Code/WAIT1.o  

./Sources/Scheduler.o: In function `Scheduler_serviceInterrupt':

C:\Users\kschwab\workspace.kds\Ultrasonic Flow Sensor\Debug/../Sources/Scheduler.c:44: undefined reference to `uartCommunication'

collect2.exe: error: ld returned 1 exit status

make: *** [Ultrasonic Flow Sensor.elf] Error 1

The name of the linker file is ProcessorExpert.ld.

I have no knowledge of how to even approach reading a linker file, much less tinker with it to fix this issue. Any help or direction would be much appreciated!

0 Kudos
1 Solution
780 Views
BlackNight
NXP Employee
NXP Employee

Hello,

I do not see any definition  of an object of named 'uartCommunication' in the sources you are sharing. So the linker is correct with its error message.

You have this:

extern uartCommunication_parameters uartCommunication;

But this is a *declarartion*, not a *definition*.

You need to have something like this

uartCommunication_parameters uartCommunication;

in your application (.c file) (and of course properly initalize it).

C/C++ refresher: 'extern' for variables only declare the name, but do not define (allocate memory) for it.

I hope this helps,

Erich

View solution in original post

3 Replies
781 Views
BlackNight
NXP Employee
NXP Employee

Hello,

I do not see any definition  of an object of named 'uartCommunication' in the sources you are sharing. So the linker is correct with its error message.

You have this:

extern uartCommunication_parameters uartCommunication;

But this is a *declarartion*, not a *definition*.

You need to have something like this

uartCommunication_parameters uartCommunication;

in your application (.c file) (and of course properly initalize it).

C/C++ refresher: 'extern' for variables only declare the name, but do not define (allocate memory) for it.

I hope this helps,

Erich

View solution in original post

780 Views
kevynschwab
Contributor II

I reread your answer and saw the C/C++ refresher, then accordingly changed the code in UartCommunication.c to:

#include "UartCommunication.h"

uartCommunication_parameters uartCommunication;

By simply deleting the extern, and indeed the code does now compile. Thank you for your help!

0 Kudos
780 Views
kevynschwab
Contributor II

I apologize, I forgot to include this code, which is in UartCommunication.c:

#include "UartCommunication.h"

extern uartCommunication_parameters uartCommunication;

Shouldn't that satisfy the definition?

0 Kudos