calling external functions in c++

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
slacker

calling external functions in c++

Post by slacker »

in asm i cannot call external functions written in c++ for some reason. why not? this works in c...

extern _isr0
call _isr0

..this works in c but not in c++ unless the function is named "main"

anybody know why or how to call external c++ functions?
_mark

Re:calling external functions in c++

Post by _mark »

It is called name mangling. The C++ compiler actually changes the names of C++ functions becuase they all can be overloaded. The end result is that every function really has a unique name. You can look at what the compiler has named the function in the object code, or I believe some compilers have switches and pragmas to turn it off.

_mark
slacker

Re:calling external functions in c++

Post by slacker »

how do i access this object code? in a hex editor?
_mark

Re:calling external functions in c++

Post by _mark »

don't bother - I was hoping to stimulate you to do some research on your own as I think this is quite interesting. Just add:

extern "C" functionname /variablename

in front of it.

_mark()
slacker

Re:calling external functions in c++

Post by slacker »

so this should do it(from ASM code):

[extern "C" _clearscr]

?
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:calling external functions in c++

Post by distantvoices »

noooo...

in assembly you still call your function [extern _name]

in c++ you import functions declared/defined in another module via extern "C" void/int/... functionname(void/int/...);

the "C" tells the C++ Compiler that this is a reference to an ordinary c function.

stay safe
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
slacker

Re:calling external functions in c++

Post by slacker »

yea i figured out something was wrong.

;----------------ASM CODE-------------------
[extern _function]
call _function


;----------------C++ CODE--------------------
extern "C" void function()
{
print("calling functions in c++);
}
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:calling external functions in c++

Post by distantvoices »

not really:

extern "C" int yourfunction(void); -> declaration. Definition is in an other module. the linker puts the right adress there.

you call it in your function in c++ this way:

definition:
void burschi(void){
yourfunction();
}

everything clear?
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:calling external functions in c++

Post by Pype.Clicker »

and if you don't want to use extern "C", but have the C++ name used instead, you still can use "objdump -x" on your object file to discover what are the mangled names of the object you want to access...

The mangled name usually include the name (of course), the namespace (so that it translates myClass::variable into variable__Q7myClass) and the parameters type for the function (so that overloading can apply... myClass::function(void*) will be mangled in something like function__Q7myClasspv)
Post Reply