ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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!
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++
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.