link problems
I tried to use C to call a function writing in Fortran.first I comple the subroutine.f to obj file ,say subroutine.o .then I linked the main function writing by C using command : g++ main.c subroutine.o -lm -o file.it doesnot work .I checked the function in both obj files .I found the name of subroutine are different.I tried using gcc to link them.it works fine.So my question is :How can I link them using g++ complier since I will add functions using C++ classes.
Thanks |
you need to tell the compiler that the functions exported from the fortran object file should be linked C-style not C++ style, the same problem occurs when linking a C object with c++, simply enclose the function definition in
Code:
extern "C" { /* function definition here */ } |
I got
thanks |
You can use the _cplusplus macro to make it so that the same source code file can compiled with either a C or C++ compiler. When compiled with a C++ compiler, the _cplusplus macro is set, so the function is declared to use C linkage:
Code:
#ifdef _cplusplus |
Quote:
the difference between C and C++ style linkage is name mangling. C++ compilers add the argument types and other info to the symbol name thus the function 'int p(int x)' in C would be exported as simply p, but in c++ it is exported as _Z1pi(different compilers mangle differently though). this allows functions with the same name to exist in c++ extern "C" { /* symbols */ }tells the c++ compiler to export the symbols with c linkage which is not generally a good idea because it turns off all of the features that name mangling allows. you should only extern "C" functions which are exported/imported by the executable else you are unecessarily(sp?) turning off valuable parts of C++ |
Do you know of any sites that have a good discussion of name mangling? Googling for one didn't seem to produce any good results.
Ah, yes. I think you are correct that the order that arguments are pushed onto the stack are the same (right to left) for both C and C++. Between the Pascal convention and the C/C++ convention there's a difference in who cleans up the stack (the function before it returns, or the caller after the function returns) right? |
1) not really but have a play with c++filt and nm and see what you can figure out.
2) you are probably correct about who cleans up the stack, its been a long time since ive done any assembly or pascal, im afraid i really cant remember. |
All times are GMT -5. The time now is 02:07 PM. |