# GNU and NASM

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

## 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 on other sites
#ifdef _cplusplus

should be
#ifdef __cplusplus

##### Share on other sites
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 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 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 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 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 on other sites
Quote:
 Original post by nmiThird 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 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 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)

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26