[CW6.1] Undefined Reference Linker Error

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

[CW6.1] Undefined Reference Linker Error

5,583 Views
ColdFireHot
Contributor I
Hi Everyone,
  Newbie time again.
 
I'm getting an error when compiling a CPP application with references to a "C" file wrapper for an assembly routine. The declaration is in a "h" file and the definition is in an "c" file. The "LINKER" error is undefined reference to the "C" function call "mcf5xxx_irq_enable".
If I force a CPP compile then the error moves to the unreferenced assembly call "asm_set_ipl".
How do I get this to compile properly?

Attached is the full project.

Thanks

MainBoot.cpp
#include "Common.h"
#include "MCF5xxx.h"

int main(int /* argc */, char /* **argc */)
{
    mcf5xxx_irq_enable();
    return 0;
}


MCF5xxx.h
...
void mcf5xxx_irq_enable (void);
...


MCF5xxx.c
...
void mcf5xxx_irq_enable (void)
{
asm_set_ipl(0);
}
...


mcf5xxx.s
...
.global asm_set_ipl
.global _asm_set_ipl
...
asm_set_ipl:
_asm_set_ipl:
link A6,#-8
movem.l D6-D7,(SP)

move.w SR,D7 /* current sr */

move.l D7,D0 /* prepare return value */
andi.l #0x0700,D0 /* mask out IPL */
lsr.l #8,D0 /* IPL */

move.l 8(A6),D6 /* get argument */
andi.l #0x07,D6 /* least significant three bits */
lsl.l #8,D6 /* move over to make mask */

andi.l #0x0000F8FF,D7 /* zero out current IPL */
or.l D6,D7 /* place new IPL in sr */
move.w D7,SR

movem.l (SP),D6-D7
lea 8(SP),SP
unlk A6
rts
...
 
Message Edited by t.dowe on 2009-09-04 11:28 AM
Labels (1)
0 Kudos
2 Replies

1,229 Views
CompilerGuru
NXP Employee
NXP Employee

Interesting, one can simply attach a zip file. Did not know this.
Anyway, I linked your sample!
I had to made the following changes.
- Add
#ifdef __cplusplus
extern "C" {
#endif
at the start of MFC5xxx.h and
#ifdef __cplusplus
}
#endif
at the end. C++ mangles the function names, and those functions are accessed (say defined) from C and (used) C++.
- add
#define _UNDERSCORE_
in mcf5xxx.s. You thought right, the C compiler does add an underscore in the front.
- define the function cpu_handle_interrupt.
The code actually calls it, but the definition is missing.
So I added
void cpu_handle_interrupt (int) {
}
at the bottom of mcf5xxx.c. That's definitely not enough for this function to work, but its enough to link :smileywink:

 

CPPTest20060308.zip

Message Edited by t.dowe on 2009-09-04 11:27 AM
0 Kudos

1,229 Views
ColdFireHot
Contributor I

CompilerGuru,

  Thanks for the information.  Let me summarize to help gain a full understanding in a general sense.

1) A "C" function called within a "CPP" file needs the "extern "C" { }" block defined to prevent name mangling.

2) An "S" (assembly) routine called from within a "C" function needs no special handling in the "C" file.

3) A "C" function referenced from the "S" routine needs the underscore added to match the assembler/linker output.

Is the "extern "C" { }" an ANSI Standard that I should read about?  If so where is a good place to acquire the ANSI Standard?

Thanks again for your help.

Mark

 

0 Kudos