Fortran call from C++

  • Thread starter Dennis Kernighan
  • Start date
D

Dennis Kernighan

On Microsoft's pages there is an example of making Fortran call from
C++, see http://tinyurl.com/2692f . If I put C code in the test.c and
Fortran code in FORSUBS.FOR and press F5 in C++, I get following
errors:

------- error statement --------

fortrantester error LNK2019: unresolved external symbol "void
__stdcall
PYTHAGORAS(float,float,float *)" (?PYTHAGORAS@@YGXMMPAM@Z) referenced
in
function _main

fortrantester error LNK2019: unresolved external symbol "int __stdcall
FACT(int)" (?FACT@@YGHH@Z) referenced in function _main

fortrantester fatal error LNK1120: 2 unresolved externals

------- error statement --------

So. Is the problem that Fortran compiler is missing? Where could I get
one and how will I tell to C++ that it should use it?

DK
 
J

Jeff Schwab

Dennis said:
On Microsoft's pages there is an example of making Fortran call from
C++, see http://tinyurl.com/2692f . If I put C code in the test.c and
Fortran code in FORSUBS.FOR and press F5 in C++, I get following
errors:

------- error statement --------

fortrantester error LNK2019: unresolved external symbol "void
__stdcall
PYTHAGORAS(float,float,float *)" (?PYTHAGORAS@@YGXMMPAM@Z) referenced
in
function _main

fortrantester error LNK2019: unresolved external symbol "int __stdcall
FACT(int)" (?FACT@@YGHH@Z) referenced in function _main

fortrantester fatal error LNK1120: 2 unresolved externals

------- error statement --------

So. Is the problem that Fortran compiler is missing? Where could I get
one and how will I tell to C++ that it should use it?

DK


Wel,, Dennis Kernighan, I don't think the C++ standard attaches any
particular, Fortran-related behavior to the pressing of F5... I think
you need to try one of the MS support groups, or perhaps even read your
linker's documentation.
 
C

Charles LaCour

Dennis Kernighan said:
On Microsoft's pages there is an example of making Fortran call from
C++, see http://tinyurl.com/2692f . If I put C code in the test.c and
Fortran code in FORSUBS.FOR and press F5 in C++, I get following
errors:

Posting some a small executable source file wouldn't hurt.
 
G

Gianni Mariani

Dennis said:
On Microsoft's pages there is an example of making Fortran call from
C++, see http://tinyurl.com/2692f . If I put C code in the test.c and
Fortran code in FORSUBS.FOR and press F5 in C++, I get following
errors:

------- error statement --------

fortrantester error LNK2019: unresolved external symbol "void
__stdcall
PYTHAGORAS(float,float,float *)" (?PYTHAGORAS@@YGXMMPAM@Z) referenced
in

The symbol here "?PYTHAGORAS@@YGXMMPAM@Z" is a C++ mangled name. There
is a standard way to tell the C++ compiler to not use "external linkage"
- this is by using the ' extern "C" ' syntax. See below.
function _main

fortrantester error LNK2019: unresolved external symbol "int __stdcall
FACT(int)" (?FACT@@YGHH@Z) referenced in function _main

fortrantester fatal error LNK1120: 2 unresolved externals

------- error statement --------

So. Is the problem that Fortran compiler is missing?

You'll obciously need fortran libraries, but you don't need a fortran
compiler to link with a fortran library.

Where could I get
one and how will I tell to C++ that it should use it?
... I'll let you figure this out ..


The right way to do this using C++ is below:

#include <cstdio>

extern "C"
{
extern int FACT (int n);
extern void PYTHAGORAS (float a, float b, float *c);
};

main()
{
float c;
std::printf("Factorial of 7 is: %d\n", FACT(7));
PYTHAGORAS (30, 40, &c);
std::printf("Hypotenuse if sides 30, 40 is: %f\n", c);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top