- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Is there a tutorial here that explains how to call C code routines from Fortran (and would tell you all the tricky behind the scene things that you have to do)? I'm having do luck locating decent help on this.
Thanks times infinity,
Bill
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Bill,
First, may I suggest reading the compiler documentation about BIND(C) and the features available in intrinsic module ISO_C_BINDING? You can also read the Fortran standard (linked on this forum's main page) about those features.
There is also an extensive section on mixed-language programming in the compiler documentation, though it has not yet been updated to mention the standard C interoperability features.
Last, there are several worked examples provided with the product in Samples\en_US\Fortran\MixedLanguage.zip
I would encourage you to use the language's C interoperability features which make it fairly painless to call C and be called by C. The one thing you do have to watch for is that if your C source has a .cpp file type, it will be treated as C++ and the routine names "mangled". You can deal with this by using:
extern "C"
before the declarations of the routines in your C source.
First, may I suggest reading the compiler documentation about BIND(C) and the features available in intrinsic module ISO_C_BINDING? You can also read the Fortran standard (linked on this forum's main page) about those features.
There is also an extensive section on mixed-language programming in the compiler documentation, though it has not yet been updated to mention the standard C interoperability features.
Last, there are several worked examples provided with the product in Samples\en_US\Fortran\MixedLanguage.zip
I would encourage you to use the language's C interoperability features which make it fairly painless to call C and be called by C. The one thing you do have to watch for is that if your C source has a .cpp file type, it will be treated as C++ and the routine names "mangled". You can deal with this by using:
extern "C"
before the declarations of the routines in your C source.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Bill:
The Fortran to C and reverse in the Intel samples are pretty good. I modified the sample to teachmyself some C++ quickly. I attach the VS 2010 sln and the code as a zip file.
It is pretty self explanatory. The power function is from K&R, but it just to show a return value so the code is stripped out.
Regards
JMN
The Fortran to C and reverse in the Intel samples are pretty good. I modified the sample to teachmyself some C++ quickly. I attach the VS 2010 sln and the code as a zip file.
It is pretty self explanatory. The power function is from K&R, but it just to show a return value so the code is stripped out.
Regards
JMN
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Gentlemen,
Thanks so much for the responses. Learning by example works best for me so the Fortran calls C example is just what I needed.
I've been trying to emulate the Fortran_Calls_C example, Clib and Fmain. I can load the example into VS2008 and it builds and runs okay. Unfortunately, with my emulated version I'm gettting some linker errors that are causing me fits. (Error messages listed below.) My guess is that there is some VS2008 project properity setting in my emulation that is causing the problem. So far though all the properties that I've checked between the two solutions are the same.
Scratching my head ...
Bill
Build log:
1>------ Build started: Project: C_Part, Configuration: Debug Win32 ------
1>Compiling...
1>do_something.cpp
1>Creating library...
1>Build log was saved at "file://c:\Documents and Settings\dossow1\My Documents\Visual_Fortran_Workspace\F90_Calls_C\C_Part\Debug\BuildLog.htm"
1>C_Part - 0 error(s), 0 warning(s)
2>------ Build started: Project: F90_Part, Configuration: Debug Win32 ------
2>Compiling with Intel Visual Fortran Compiler XE 12.0.4.196 [IA-32]...
2>F90_Part.f90
2>Linking...
2>C_Part.lib(do_something.obj) : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
2>MSVCRTD.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)
2>MSVCRTD.lib(ti_inst.obj) : error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info@@AAEAAV0@ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)
2>LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library
2>Debug\F90_Part.exe : fatal error LNK1169: one or more multiply defined symbols found
2>
2>Build log written to "file://C:\Documents and Settings\dossow1\My Documents\Visual_Fortran_Workspace\F90_Calls_C\F90_Part\Debug\BuildLog.htm"
2>F90_Part - 3 error(s), 2 warning(s)
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Gentlemen,
I found the error!!
Under the project properties for the C code project, which was creating a static library, I had to set Configuration Properties | C/C++ | Code Generation | Runtime Library to "Multi-threaded Debug (/MTd)". I had it set to "Multi-threaded Debug DLL (/MTd)". My eyes glossed right over that "DLL" yesterday.
Thanks again for your help!!
Bill

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