Linker error

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Linker error

1,189件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Dave Such on Tue Jan 20 14:16:25 MST 2015

Hello,

Very new to XPresso workbench.  Have been using IAR for several years with LPC2292/2294.  I have several projects that are successfully built using IAR.  As a way of introducing myself to Xpresso, I decide to take one of my smaller ones and see what it takes to port over to this compiler.

After about a week of porting about 20 source files, I can now compile all of them without errors or warnings.  However, when it goes to link it, I am getting several errors from main.o about -- undefined reference to --.  These are referring to variables / functions that are defined in other header files, used in main.c.  I have searched through this forum as well as the FAQ and have not found anything to help me.  After trying several things and going through the options, I am at a loss.

I decided to simplify this.  I created a new project using the wizard.  I added one c file with its h file.  Added one function in it, defined it in the h file along with a variable.  I then called this function from main() along with incrementing the variable.  Same results.  Both files compile but I get the linker error.

Any help/suggestions would be appreciated.  Here is a sample from the console

15:57:09 **** Incremental Build of configuration Debug for project Test ****
make all
Building file: ../src/hello.c
Invoking: MCU C Compiler
arm-none-eabi-gcc -D__REDLIB__ -DDEBUG -D__CODE_RED -I"C:\Documents and Settings\Dave\My Documents\LPCXpresso_7.3.0_186\workspace\DPA4plus\src" -O0 -g3 -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -fdata-sections -mcpu=arm7tdmi -D__REDLIB__ -specs=redlib.specs -MMD -MP -MF"src/hello.d" -MT"src/hello.o" -MT"src/hello.d" -o "src/hello.o" "../src/hello.c"
Finished building: ../src/hello.c

Building file: ../src/main.c
Invoking: MCU C Compiler
arm-none-eabi-gcc -D__REDLIB__ -DDEBUG -D__CODE_RED -I"C:\Documents and Settings\Dave\My Documents\LPCXpresso_7.3.0_186\workspace\DPA4plus\src" -O0 -g3 -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -fdata-sections -mcpu=arm7tdmi -D__REDLIB__ -specs=redlib.specs -MMD -MP -MF"src/main.d" -MT"src/main.o" -MT"src/main.d" -o "src/main.o" "../src/main.c"
Finished building: ../src/main.c

Building target: Test.axf
Invoking: MCU Linker
arm-none-eabi-gcc -nostdlib -Xlinker -Map="Test.map" -Xlinker --gc-sections -mcpu=arm7tdmi -T "Test_Debug.ld" -o "Test.axf"  ./src/cr_startup_lpc22.o ./src/crp.o ./src/hello.o ./src/main.o  
./src/main.o: In function `main':
C:\Dave2\60xxwork\Test\Debug/../src/main.c:36: undefined reference to `ulDebug1'
collect2.exe: error: ld returned 1 exit status
make: *** [Test.axf] Error 1

15:57:10 Build Finished (took 1s.547ms)

ulDebug is the variable defined in hello.h

Thanks


0 件の賞賛
返信
5 返答(返信)

1,166件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Dave Such on Thu Jan 22 08:30:19 MST 2015

Thanks for the reply's.  Yes, as suggested, it does now build.  So I changed the test program to more closely represent the method our projects are coded and it builds as well.  At least I can eliminate that as the problem.  Back to grabbing the shovel and keep digging.

I've attached the new test program if anyone is interested.

0 件の賞賛
返信

1,166件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Wed Jan 21 00:48:00 MST 2015
The problem is as MikeSimmonds says. You have declared ulDebug1 in hello.h (extern unlsigned long ulDebug1) but you have not defined it in hello.c (i.e. add unsigned long ulDebug1).

MikeSimmonds Note: You should never define a variable in a header - every time you include the header it will be redefined, resulting in multiple copies of the variable with the same name. Only ever declare variables in headers.

See
http://www-ee.eng.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.4.html
for an explanation.
0 件の賞賛
返信

1,166件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MikeSimmonds on Tue Jan 20 14:33:44 MST 2015
If I remember my "C", the hello.h should be declaring not defining variables and/or function.

I.e Hello.c
...
int aVariable = 42; // if it should be initialised [UK spelling!]
...


In Hello.h
...
extern    int aVariable;
...


And, of course, Main.c
...
#include "Hello.h"
...

void MyFunc(void)
{
...
    aVariable = 1492; // set foreign variable
...
}
...


Such constructs build and link for me [IDE 7.5]

Perhaps you made it 'static' by mistake?

Mike



0 件の賞賛
返信

1,165件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Dave Such on Tue Jan 20 14:31:43 MST 2015

New to forums too.  Not sure if this is proper way to "post your project" or not but it is attached to this reply as a zip file.

Thanks
0 件の賞賛
返信

1,165件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Tue Jan 20 14:25:37 MST 2015
Please post your project,so we can debug what you are doing wrong.
0 件の賞賛
返信