Page 1 of 1
calling external functions in c++
Posted: Sun Apr 20, 2003 8:45 am
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?
Re:calling external functions in c++
Posted: Sun Apr 20, 2003 8:50 am
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
Re:calling external functions in c++
Posted: Sun Apr 20, 2003 8:52 am
by slacker
how do i access this object code? in a hex editor?
Re:calling external functions in c++
Posted: Sun Apr 20, 2003 8:53 am
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()
Re:calling external functions in c++
Posted: Sun Apr 20, 2003 2:39 pm
by slacker
so this should do it(from ASM code):
[extern "C" _clearscr]
?
Re:calling external functions in c++
Posted: Sun Apr 20, 2003 2:46 pm
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
Re:calling external functions in c++
Posted: Sun Apr 20, 2003 3:01 pm
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++);
}
Re:calling external functions in c++
Posted: Sun Apr 20, 2003 3:06 pm
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?
Re:calling external functions in c++
Posted: Mon Apr 21, 2003 5:52 am
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)