c++, calling asm functions
c++, calling asm functions
how would i call external asm functions that i have written in another file. i need to declare these external functions some how so i dont get a compile error and i was wondering how do i do this and how do i actually call them?
Re:c++, calling asm functions
im using:
in my asm file(nasm):
global gdtload
in my cpp file:
extern void gdtload();
but when i call it in my cpp file:
gdtload();
the linker gives me an error saying:
undefined reference to 'gdtload()'
i think it might think the parentheses are part of the function name but i cant use parentheses in nasm???
in my asm file(nasm):
global gdtload
in my cpp file:
extern void gdtload();
but when i call it in my cpp file:
gdtload();
the linker gives me an error saying:
undefined reference to 'gdtload()'
i think it might think the parentheses are part of the function name but i cant use parentheses in nasm???
Re:c++, calling asm functions
Code: Select all
; Assembly file
function:
; code
ret
Code: Select all
// C++ file
// declare function that is present in another file for linking
extern void function(); // or whatever it returns
int main()
{
function(); // call the function
return 0;
}
[edit]
Okay, so I wasn't completely right the first time
You said that you declare it as "global gdtload" inside the asm file.
This is a silly question I know, but, then again, I've missed simpler and stupider things, but are you actually linking the two (or more) things together after compiling them?
Apart from that, I have no other useful advice.
[/edit]
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:c++, calling asm functions
iirc, you should tell C++ that your asm function is
or it will be looking for the mangled name instead of a flat name (e.g. something like __7gdtLoad1vv. Don't rely on me to mangle it like GCC).
Code: Select all
extern "C" gdtLoad();
Re:c++, calling asm functions
ok i found the problem but i dont know how to fix it
gpp trys calling the function __Z7gdtloadv instead of gdtload.
so gpp looks for that function name in my asm file. when i change it to that name in my asm file, then it links fine. But how do i get gpp to look for gdtload instead of __Z7gdtloadv in the asm file ??
gpp trys calling the function __Z7gdtloadv instead of gdtload.
so gpp looks for that function name in my asm file. when i change it to that name in my asm file, then it links fine. But how do i get gpp to look for gdtload instead of __Z7gdtloadv in the asm file ??
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:c++, calling asm functions
btw, if you're aiming at C++, i suggest you got a look at the tutors/forums at www.invalidsoftware.net. They might help you too.
Re:c++, calling asm functions
pypeclicker's extern "C" advice should do the trick. Also keep in mind that g++ may or may not need a leading underscore in functions that come from asm.
so for example:
hope this helps
proxy
so for example:
Code: Select all
.global function
; Assembly file
function:
; code
ret
// C++ file
// declare function that is present in another file for linking
extern "C" void function(); // or whatever it returns
int main()
{
function(); // call the function
return 0;
}
proxy
Re:c++, calling asm functions
when i do extern "C", it looks for the functions in the cpp file and now i get a compile error:
gdload was not declared in the scope
gdload was not declared in the scope
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:c++, calling asm functions
hmm ... you might wish to pick up your reference C++ book for that one. I can't remember if a specific namespace needs to be used for C externs or not.
Re:c++, calling asm functions
I'd check for typos first.
You should declare 'extern "C"' normally in the global namespace.
The only thing it does is specify linkage, which in this case means disabling name-mangling.
You should declare 'extern "C"' normally in the global namespace.
The only thing it does is specify linkage, which in this case means disabling name-mangling.
Re:c++, calling asm functions
well, the extern "C" is definitely mandatory unless he wants to specify the function by it's mangled name in the asm file (which would be nothing but annoying).
What compiler error do you get when you use extern "C"? did you try adding the leading underscore the asm declaration as i advised?
proxy
What compiler error do you get when you use extern "C"? did you try adding the leading underscore the asm declaration as i advised?
proxy