LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   link problems (https://www.linuxquestions.org/questions/programming-9/link-problems-89213/)

zhangxd6 09-04-2003 11:06 AM

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

kev82 09-04-2003 03:22 PM

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 */ }
and all should be well

zhangxd6 09-04-2003 03:30 PM

I got
thanks

eric.r.turner 09-07-2003 11:55 AM

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
extern "C" {
#endif

/* function declaration here */

#ifdef _cplusplus
}
#endif

BTW, the difference between C++ and C (or Fortran) functions is the order that the function arguments are pushed onto the stack. One does them left to right, the other does them right to left. I can never remember which is which!

kev82 09-07-2003 12:20 PM

Quote:

BTW, the difference between C++ and C (or Fortran) functions is the order that the function arguments are pushed onto the stack. One does them left to right, the other does them right to left. I can never remember which is which!
i dont believe so, as far as i know both C and C++ push there arguments starting at the right and going left(so the first argument pops off first), pascal does it the other way round i think but im not sure.

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++

eric.r.turner 09-07-2003 02:36 PM

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?

kev82 09-07-2003 02:53 PM

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.