Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Callin assembler function from C++ 1

Status
Not open for further replies.

cppprogrammer

Programmer
Dec 18, 2007
9
0
0
Hello!

My problem is that i dont know how to call an assembler function from a C++ program. I wonder i someone can see what i have done wrong in the code below?
It is intel assembler on Linux ubuntu platform and i use the g++ compiler.

I get this error message when i link the c objectfile and the assembler file:

g++ function.s function_call.o -o function_call

function_call.o: In function `main':
function_call.cpp:(.text+0x84): undefined reference to `my_function()'


This is my code


Code:
C++ code:

#include<iostream>

extern void my_function();

int main()
{
	my_function();
}


Assembler code:

	.file	"function.s"

	.data
msg:    .string "Hello\n"
	len = . - msg - 1 
	
	.text
	.align 2

.globl _my_function
	.type	_my_function, @function

_my_function:
	pushl   $len
	pushl   $msg
	pushl   $1
	call    write
	addl    $12, %esp    
	pushl   $0
	call    exit
	ret

.LFE2:
	.size	main, .-main
	.size	_my_function, .-_my_function
.globl __gxx_personality_v0
	.ident	"GCC: (GNU) 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)"
	.section	.note.GNU-stack,"",@progbits
 
Perhaps you also need to disable C++ name mangling.

[tt]extern "C" void my_function();[/tt]

You can also use this command,
[tt]nm function_call.o[/tt]
to find out the actual name of the symbols being generated.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Thanks for the hint.
Unfortunatly i still get the same error message.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top