Fortran and C together

Fortran and C together

How do you Link a program so that C can call a fortran function?

I am using 11.1 compiler, for ifort, and icc

and I get
icc -o ctf helloc.o hellof.o -L /tools/intel/fce/11.0.081/lib/intel64/lib -lifcore -limf
hellof.o: In function `hello_':
hellof.f:(.text+0x39): undefined reference to `for_write_seq_lis'

15 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Try the following compile/link sequence:

icc -c helloc.c
ifort hellof.f helloc.o

this is odd, it should work as for_write_seq_lis IS in libifcore.a. Are you mixing 32bit and 64bit code? what does:

file hellof.o

give as a result? - is it 32bit or 64bit object file? If it's 32-bit you'll need to path to .../lib/ia32/lib -lifcore -limf

Its defininately all 64 bit, compiled both the C and Fortran routines with the 64 bit compiler. I compiled helloc.c as icc -c helloc.c, and hellof.f as ifort -c hellof.f.

And then tried that line I have earlier, which im not even sure is correct. If there is another way to link them, or something that could be the real issue I am having.

Best Reply

it looks like you are doing everything OK. You can try:

icc -o ctf helloc.o hellof.o /tools/intel/fce/11.0.081/lib/intel64/lib/libifcore.a -L /tools/intel/fce/11.0.081/lib/intel64/lib -limf

Or if you want to attach helloc.c and hellof.f here I can try myself. But what you are doing looks perfect to me.

ron

Hey it worked with your command!

icc -o ctf helloc.o hellof.o /tools/intel/fce/11.0.081/lib/intel64/lib/libifcore.a -L /tools/intel/fce/11.0.081/lib/intel64/lib -limf

got it to compile. Thanks Ron.

Is it possible to call C++ code from fortran? I can get it working fine with C, but i get linking errors when I try it with C++.

It is possible to do. The reason you get linker errors is because C++ mangles the names of the routines, which is how it performs function overloading.

http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

You need the

extern c

before your function names.

Tim

FORTRAN PROGRAM

PROGRAM TEST
CHARACTER *11 VAR
VAR = 'HELLO WORLD'
CALL CCHECK_(VAR)

END

C PROGRAM

#include

void ccheck_ (char* pgm_name)

{
printf ("STRING IS %s\n", pgm_name);

}

ifort -assume nounderscore -c hello.f
icc -c ccheck.c
ifort -o hello hello.o ccheck.o
./hello
STRING IS HELLO WORLD

This works fine with C, If I try to make the same c program
a .cpp file,

I get

STATWW.o: In function `MAIN__':
STATWW.f:(.text+0x5d): undefined reference to `ccheck_'
ccheck.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'

How do I fix this?

Replace

void ccheck_ (char* pgm_name)

by

extern "C" {
void ccheck_ (char* pgm_name)
}

#include

extern "C" {
void ccheck_ (char* pgm_name);
}

void ccheck_(char* pgm_name)
{
printf ("STRING IS %s\n", pgm_name);

}

I changed the cpp file to this, and still get the

ccheck.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'

#include

extern "C" {
void ccheck_ (char* pgm_name);
}

void ccheck_(char* pgm_name)
{
int *b = new int;
printf ("STRING IS %s\n", pgm_name);

}

If I add some C++ code, I get

ccheck.cpp:(.text+0xb): undefined reference to `operator new(unsigned long)'

So is there a library I have to link in to resolve these?

No, your C function, to be seen as such in a C++ source file, should be

extern "C"{
#include 
void ccheck_(char* pgm_name){
   printf ("STRING IS %sn", pgm_name);
   }
}

Whether to add the underscore to the function name or not depends on how you call the compiler(s) and the OS.

I changed my .cpp file to look like yours, but I still get the

ccheck.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'

How am I supposed to build this? I am doing

ifort -c -assume nounderscore hello.f
icc -c ccheck.cpp
ifort -o hello hello.o ccheck.o

I am on Linux Suse SLES10

You need icpc in place of icc to get automatic linking of libstdc++, just as you would need g++ in place of gcc. If you don't mean C++, use .c rather than .cpp.
If your main program is Fortran, use ifort to link, but -lstdc++ will be required, just as with any combination of Fortran and g++.

Leave a Comment

Please sign in to add a comment. Not a member? Join today