[CW6.1] Undefined Reference Linker Error

Showing results for 
Search instead for 
Did you mean: 

[CW6.1] Undefined Reference Linker Error

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.


#include "Common.h"
#include "MCF5xxx.h"

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

void mcf5xxx_irq_enable (void);

void mcf5xxx_irq_enable (void)

.global asm_set_ipl
.global _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
Message Edited by t.dowe on 2009-09-04 11:28 AM
Labels (1)
0 Kudos
2 Replies

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" {
at the start of MFC5xxx.h and
#ifdef __cplusplus
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:



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

Contributor I


  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.



0 Kudos