473,480 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Fortran and C++

hi,
how can i use existing library in Fortran for my C++ program.
I have try f2c but it is rather cubersome... Can I compile the Fortran code
into Object file and then link to C++ code?
My thanks
TF
Jul 22 '05 #1
7 2155
To Forum wrote:
How can I use existing library in Fortran for my C++ program.
I have try f2c but it is rather cubersome.
Can I compile the Fortran code into Object file and then link to C++ code?


Yes.
But the details depend upon your target platform.

platform = machine architecture, operating system,
C++ compiler and Fortran compiler.
Jul 22 '05 #2
On Sat, 10 Jul 2004 01:51:01 +0200, "To Forum" <to*****@ifrance.com>
wrote in comp.lang.c++:
hi,
how can i use existing library in Fortran for my C++ program.
I have try f2c but it is rather cubersome... Can I compile the Fortran code
into Object file and then link to C++ code?
My thanks
TF


The C++ language does not define a binding to any other language
except for C. You need to ask in a group that supports your platform,
or your specific FORTRAN and C++ compilers to see if they provide this
ability.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #3
To Forum wrote:
Can I compile the Fortran code
into Object file and then link to C++ code?
My thanks


In practice, usually yes. You'll have to

a) wrap the declaration inside 'extern "C"', as if you were
using C code
b) remember how Fortran passes parameters (like ints are
passed by pointer, not by value)

Also, you'll *probably* have to append an underscore ('_') to
C++ occurrences of the Fortran name and write them in
lower-case.

I also vaguely recall that Fortran indexes the arrays the
other way round (apart from indexing from 1), you might take
that into account.

An example:

// This uses the function DSYEVD from LAPACK, compiled with
// Fortran. In Fortran-land this function takes parameters
// of type: CHARACTER, CHARACTER, INTEGER, DOUBLE PRECISION,
// INTEGER, DOUBLE PRECISION, DOUBLE PRECISION,
// INTEGER, INTEGER, INTEGER, INTEGER.
// Notice how they are all passed by pointer, not by value

extern "C" void dsyevd_(char *job, char *uplo, int *n, double *a,
int *lda,double *w, double *work,
int *lwork,int *iwork, int *liwork, int *info);

HTH, use google,
- J.
Jul 22 '05 #4
Jack Klein wrote:
To Forum wrote:
How can I use existing library in Fortran for my C++ program.
I have try f2c but it is rather cubersome.
Can I compile the Fortran code into Object file and then link to C++ code?


The C++ language does not define a binding to any other language
except for C.


The C++ programming language does *not* define a *binding*
to any other computer programming language. Not even C.
It provides a mechanism -- extern "C" --
to *help* with *linkage* to external C functions.
It does *not* guarantee that a call to function
compiled by a C compiler or even another C++ compiler
will be successful.
Jul 22 '05 #5
"To Forum" <to*****@ifrance.com> wrote in message news:<40***********************@nan-newsreader-04.noos.net>...
hi,
how can i use existing library in Fortran for my C++ program.
I have try f2c but it is rather cubersome... Can I compile the Fortran code
into Object file and then link to C++ code?
My thanks
TF


I think its possible - depending on your compiler though. If your
C++ and fortran compilers are from the same vendor, then it is
almost certainly possible. If they are from different vendors,
it might be possible - for example a few vendors provide fortran
compilers whose object files can be linked to VC++. You should try
asking in the newsgroup for your appropriate compiler.
One other thing - Fortran and C++ uses different calling conventions,
so that has to be taken care of.

-Arijit
Jul 22 '05 #6
"To Forum" <to*****@ifrance.com> wrote in message news:<40***********************@nan-newsreader-04.noos.net>...
hi,
how can i use existing library in Fortran for my C++ program.
I have try f2c but it is rather cubersome... Can I compile the Fortran code
into Object file and then link to C++ code?
My thanks
TF

Ultimately check your compiler manuals. One thing to watch out for is
that Fortran 2003 (now in its final stages of standardization) defines
means to interoperate with C. Several F95 compilers already implement
this as an extension. If your compiler implements this, then, combined
with the appropriate use of extern "C", you may be able to do what you
want with a fair degree of long term portability.
Jul 22 '05 #7
William Clodius wrote:
To Forum wrote:
How can I use existing library in Fortran for my C++ program.
I have try f2c but it is rather cubersome.
Can I compile the Fortran code into Object file and then link to C++ code?

Ultimately check your compiler manuals.
One thing to watch out for is that Fortran 2003
(now in its final stages of standardization) defines means
to interoperate with C. Several F95 compilers already implement
this as an extension. If your compiler implements this, then, combined
with the appropriate use of extern "C", you may be able to do what you
want with a fair degree of long term portability.


Unfortunately, Fortran 03 interoperability is one sided.
It helps Fortran 03 programmers call C library functions
but it really won't help C or C++ programmers call Fortran subprograms.
It certainly will *not* help C and C++ programmers
call legacy Fortran 77/90/95 subprograms.
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
3365
by: Carl | last post by:
"Nine Language Performance Round-up: Benchmarking Math & File I/O" http://www.osnews.com/story.php?news_id=5602 I think this is an unfair comparison! I wouldn't dream of developing a numerical...
15
2069
by: Nick Coghlan | last post by:
Thought some folks here might find this one interesting. No great revelations, just a fairly sensible piece on writing readable code :) The whole article:...
5
3993
by: NM | last post by:
Hi all I am trying to link C++ and Fortran.I am not very familiar with Fortran. I have been successful in linking C++ and fortran using g++ and ifc for simple program. So adding _ to at the end of...
4
2835
by: NM | last post by:
Hello All I am writing some progam that involves both C++ and Fortran. Some of the existing code is in Fortran. The main program will be in C++ and it will call some Fortran subroutine. All the...
1
6388
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
2
3306
by: NM | last post by:
Hello all, I am supposed to do some mixed programming with c++ and fortran. I was succeeful in exchanging the 2D arrays from fortran to c++ and the other way, but was unable to that same with...
11
8606
by: John Smith | last post by:
Ok, I know this is a common topic, but I didn't find any post that solves my current problem. I'm using Microsoft Visual C++ 6.0 (SP5), and Intel Fortran 7.1 Compiler for Windows. I'm trying to...
81
7215
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
9
3804
by: travisperkins03 | last post by:
Hi, I have read somewhere that C code sometimes cannot be compiled to be as efficient as FORTRAN, eg for matrix multiplication, because a C compiler cannot make the assumptions about arrays that...
2
4399
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
0
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7043
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6737
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.