Undefined Reference to global variable - Linker Error in KDS 3.0

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Undefined Reference to global variable - Linker Error in KDS 3.0

跳至解决方案
3,488 次查看
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 项奖励
回复
1 解答
2,802 次查看
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

在原帖中查看解决方案

3 回复数
2,803 次查看
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

2,802 次查看
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 项奖励
回复
2,802 次查看
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 项奖励
回复