- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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'
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'
1 Solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
14 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Try the following compile/link sequence:
icc -c helloc.c
ifort hellof.f helloc.o
icc -c helloc.c
ifort hellof.f helloc.o
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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++.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html
You need the
extern c
before your function names.
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Replace
[cpp]void ccheck_ (char* pgm_name) [/cpp]by
[bash]extern "C" { void ccheck_ (char* pgm_name) } [/bash]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
#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'
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'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
#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?
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
No, your C function, to be seen as such in a C++ source file, should be
[cpp]extern "C"{ #includevoid 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.[/cpp]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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++.
If your main program is Fortran, use ifort to link, but -lstdc++ will be required, just as with any combination of Fortran and g++.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page