Author Topic: Undefined Refererence to nasm Procedure from C++  (Read 5656 times)

Offline dalfonso01

  • Jr. Member
  • *
  • Posts: 44
Undefined Refererence to nasm Procedure from C++
« on: September 07, 2014, 04:24:26 PM »
Hi,
I found another post where there was a note to avoid _ before the procedure name with elf. I cannot find the reason this do not work.
Sorry if the question is trivial.
More I would know if I should expect some change using the g++  and generally speaking a native c++ instead than c during this operations.

Thanks

This is the c++

Code: [Select]
#include<cstdio>
 
 int main(void) {
     int x = 25, y = 70;
     int value ;
     extern int test1(int,int,int);
     value = test1(x, y, 5);
     printf("Result = %d\n", value);
 
     return 0;
 }

This is the nasm

Code: [Select]
section .text ; Section containing code
 
 global test1
 
 test1:
         enter 0,0
         mov eax,[ebp+8] ; get argument 1 (x)
         add eax,[ebp+12] ; add argument 2 (y)
         sub eax,[ebp+16] ; substract argument 3 (5)
         leave
         ret


This is the make result:

Code: [Select]
nasm -f elf -g -F dwarf hll1-asm.asm -l hll1-asm.lst
gcc -o hll1 hll1-cpp.o hll1-asm.o
hll1-cpp.o: In function `main':
hll1-cpp.cpp:(.text+0x31): undefined reference to `test1(int, int, int)'
collect2: ld returned 1 exit status
make: *** [hll1] Error 1

Thanks
Fabio D'Alfonso
« Last Edit: September 07, 2014, 04:27:58 PM by dalfonso01 »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Undefined Refererence to nasm Procedure from C++
« Reply #1 on: September 07, 2014, 05:28:30 PM »
Hi Fabio,

C++ is above my pay grade, but by flailing around with it, I got this to work:
Code: [Select]
#include<cstdio>
 
     extern "C" int test1(int,int,int);

 int main(void) {
     int x = 25, y = 70;
     int value ;
     value = test1(x, y, 5);
     printf("Result = %d\n", value);
 
     return 0;
 }

I think you have to tell g++ "-g" also to get debug info passed through.

Best,
Frank


Offline dalfonso01

  • Jr. Member
  • *
  • Posts: 44
Re: Undefined Refererence to nasm Procedure from C++
« Reply #2 on: September 07, 2014, 06:45:38 PM »
Hi,
thanks.

I see now that with a .c extension it works (with <stdio.h>) without the "C" , while the compilation with cpp extension gives the error. It is related to the support to overloading support to function from c++ and produces some different internal name.

Thanks
Fabio D'Alfonso

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Undefined Refererence to nasm Procedure from C++
« Reply #3 on: September 08, 2014, 11:22:54 PM »
When writing assembler which will be called from C or C++ you need to understand how the compiler is going to mangle the name of the method.  To see what g++ is looking for you would add the -S switch on the command line and look at the output assembler generated.  Then you would have to name your assembler method identically in order for it to link properly.

The resulting name is specific to GCC.  If you were to use another compiler then the method signature will be mangled in a completely different manner.  The C++ specification itself does not define how to mangle names.

Given that scenario most folks writing assembly which will be called by either C or C++ use the simple extern "C" specification to define the function interface.  Now your method can be called by many different compilers.  Note, however, than you still need to be aware of whether the C convention for you platform requires an underscore prepended to the name.  For example, MS Windows does use the underscore whilst Linux GCC does not.