views:

676

answers:

3

Hi I have written a function nabs in assembly file math.nasm as follows

%ifdef USE_x86_ASM
SECTION .text

    cglobal nABS
;*------------------------*
;* int nABS(int a)     * 
;* return value in eax    * 
;*------------------------*
ALIGN 16
    nABS:
     push ebx
     ......
                ......
                 pop ebx     
     ret
%endif

I am calling this function from c function func1 in file myfunc.c
I am using nasm assembler for assembly file I am using X-code 3.1 version and gcc 4.0 compiler I have defined USE_x86_ASM

in Xcode settings Project Settings/Build/NASM BUILD OPTIONs/OTHER FLAGS AS -DUSE_X86_ASM
Also I have defined this pre-processor in header file myfunc.h also
I have declared nABS in myfunc.h as

#define USE_x86_ASM 
int nABS(int a);

and included myfunc.h in myfunc.c
Both myfunc.c and math.nasm are compiled sussessfuly and generate math.o and myfunc.o, but I get a linking error

Undefined symbols:
  "_nABS", referenced from:
      _func1 in myFunc.o

can anyone tell me why am I getting link error?

+1  A: 

Some C compilers add a preceding underscore to C symbols. Just add the underscore to your label in the assembly file (and globl directive, of course).

Mehrdad Afshari
+1  A: 

Mehrad is right This should work

%ifdef USE_x86_ASM
SECTION .text

;*------------------------*
;* int nABS(int a)        *     
;* return value in eax    *     
;*------------------------*
ALIGN 16
global _nABS
    nABS:
        push ebx
        ......
                ......
                 pop ebx        
        ret
%endif
toto
A: 

Hi did the modification suggested in addition I removed %ifdef condition the modified code is as follows

.text
.align 2
.globl _nABS   
.private_extern _nABS

_nABS: push ebx ...... ...... pop ebx
ret still I am getting same linking error the the file is compiled but I get same linking error. I checked that the .o generated is included app.LinkFileList.

Manish