LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-04-2003, 11:06 AM   #1
zhangxd6
LQ Newbie
 
Registered: Sep 2003
Posts: 8

Rep: Reputation: 0
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
 
Old 09-04-2003, 03:22 PM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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
 
Old 09-04-2003, 03:30 PM   #3
zhangxd6
LQ Newbie
 
Registered: Sep 2003
Posts: 8

Original Poster
Rep: Reputation: 0
I got
thanks
 
Old 09-07-2003, 11:55 AM   #4
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
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!
 
Old 09-07-2003, 12:20 PM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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++
 
Old 09-07-2003, 02:36 PM   #6
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
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?
 
Old 09-07-2003, 02:53 PM   #7
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with D-Link G630 SuSE 9.2 Dr. Doerke Linux - Wireless Networking 1 08-12-2005 12:40 PM
problems while installing pilot-link TheSpork Linux - Software 6 06-18-2004 05:55 PM
problems removing a symoblic link SerfurJ Linux - Software 3 12-12-2003 02:01 PM
are there problems with D-Link and Linux? degraffenried13 Linux - Networking 4 12-02-2003 10:39 PM
D-Link problems Rutnut Linux - Networking 0 03-08-2002 01:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration