Jump to content
  • Advertisement
Sign in to follow this  
Last Attacker

GNU and NASM

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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


Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!