×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Callin assembler function from C++

Callin assembler function from C++

Callin assembler function from C++

(OP)
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

RE: Callin assembler function from C++

Perhaps you also need to disable C++ name mangling.

extern "C" void my_function();

You can also use this command,
nm function_call.o
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.

RE: Callin assembler function from C++

(OP)
Thanks for the hint.
Unfortunatly i still get the same error message.
 

RE: Callin assembler function from C++

(OP)
I dont see my function when i do:
nm function_call.o

RE: Callin assembler function from C++

(OP)
Now its working!
Thanks again!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close