GNU and NASM

Started by
9 comments, last by Last Attacker 18 years, 9 months ago
Hi, I tried to write a function in assembly and got it to compile in nasm and I want to call on that function from c++. I got the compilations to work... its just the linking that gives me undefined references. Its just a test run before I want to start writing a few assembly stuff and linking them into c++. I've written a simple test.cpp

#include <stdio.h>
#include <stdlib.h>

#include "assem.h"

int main()
{
   int i = getFive();
   
   printf("%d\n", i);
   
   system("pause");
   
   return 0;
}




Then I have a assem.h

#ifndef ASSEM_H
#define ASSEM_H
   
   #ifdef __cplusplus
      extern "C" {
   #endif
   
   int getFive();
   
   #ifdef _cplusplus
   }
   #endif
   
#endif




Now I have assem.asm

   section .data
   
   section .text
      global _getFive
      
      _getFive:
         mov eax, 5
         ret




Then I've made a makefile but it basically does this: g++ -c test.cpp -o test.o nasm -fwin32 assem.asm -o assem.o g++ test.o assem.o -o test.exe Then I get an undefined reference to getFive() Can anyone help me? Thanks [Edited by - Last Attacker on July 26, 2005 3:27:43 AM]
"Take delight in the Lord and He will give you your heart's desires" - Psalm 37:4My Blog
Advertisement
#ifdef _cplusplus

should be
#ifdef __cplusplus

(Two underscores instead of one.)
Sorry I wrote the stuff from my memory.
But I remember using a double _ in the cplusplus thing when I coded it at home and it didn't work.

Edit:
Is my global and label name correctly? I really want to know whats wrong because I want to write a few optimized implementations for our 3rd year project at university and I really don't like AT&T for assembly.

Thanks

[Edited by - Last Attacker on July 26, 2005 7:34:35 AM]
"Take delight in the Lord and He will give you your heart's desires" - Psalm 37:4My Blog
Hello,

Make sure your naming the function exactly as the linker expects it. So for example, it could be requesting _getFive instead of getFive. Maybe even getFive@0, it depends on what the linker expects. I hope this helps,

JVFF
ThanQ, JVFF (Janito Vaqueiro Ferreira Filho)
Propably.
What naming convention does the GNU C++ compiler use?
I'm using the GNU compiler.

I read just about all over the internet that when using the __cplusplus then I just have to add the single _ infront of the function name.

Thanks
"Take delight in the Lord and He will give you your heart's desires" - Psalm 37:4My Blog
First step is do get the name gcc expects to see:

nm test.o

This should print all the names in your object file, the line containing getFive contains the symbol name gcc expects.


Second step is to check what name nasm produced, so:

nm assem.o

Does it match the other name ? Maybe an underscor too much or too less ?


Third step:

Did you use the correct object file format ? This depends on if you use cygwin, djgpp or mingw. Just try different object formats, one should work if the names match.
Hello,

It's been a while since I used gcc. I found a file in the internet about it, and I think it expects an underscore before the function. The link:

http://www.delorie.com/djgpp/doc/ug/asm/calling.html

Hope this helps,
JVFF
ThanQ, JVFF (Janito Vaqueiro Ferreira Filho)
Quote:Original post by nmi
Third step:

Did you use the correct object file format ? This depends on if you use cygwin, djgpp or mingw. Just try different object formats, one should work if the names match.


I just tried the OP's example on mingw, and it works without problems.

Programs used:
- gcc version 3.4.2 (mingw-special)
- nasm 0.98.38

Commands used:
g++ -c test.cpp -o test.o
nasmw -fwin32 assem.asm -o assem.o
g++ test.o assem.o -o test.exe


Problems with Windows? Reboot! - Problems with Linux? Be root!
Thanks. After using the nm program I found that the function name was TOTALLY diffrent... well the prefix and suffix was atleast diffrent. Now it finally works.

Thanks, again!
"Take delight in the Lord and He will give you your heart's desires" - Psalm 37:4My Blog
It was ages ago I tried something like that, but I think you can tell the linker what name to expect:
int getFive() asm("_getFive");

or something like that..
(I know you solved the problem, but this seems like a nicer solution than using nm)

This topic is closed to new replies.

Advertisement