开发者

mixing programming of fortran and c++/c under visual studio

Many people may have asked this question, however, I have never seen any one gave a satisfactory solution. okay, let me make my question clear: I am trying to call some c++ subroutines/functions from my exsiting fortran code. The p开发者_JAVA技巧oint is i am doing everyting within visual studio 2008 (fortran compiler being intel fortran 11.0). Dose any one can give me a step by step instruction of compiling the two languages in the same project? if not possible to do that in ONE project, what do i do? plz show an example code, probably only a few lines just to demenstrate how to proceed.

Thanks in advance.

any one who want talk about this is welcome. BUT be very clear that i am looking for solutions with visual studio and intel fortran(compiling and debugging with GUI). NO BULLSHIT about compiling with gcc/gfortran in linux. I have read tons of such respond.

Thanks

Peng


You would not be able to call C++ from Fortran. Fortran does not obey the concept of a C++ class, does not have the concept of templates, does not have the concept of exceptions, and would not correctly maintain C++ object lifetimes.

You could expose a C interface from your C++ code, which Fortran could call. You would compile such code as a .dll, and then you would load and call functions inside the dll like you would in any other dll. However, you would have to consult Fortran documentation on how to call functions inside DLLs.


If you only want C features of C++, the general approach is to use extern C in your C++ code. Then you can use the ISO C Binding on the Fortran side and the Fortran language standard requires that the Fortran and C will be interoperable. In the Fortran, you declare an interface to describe the C routine. There are Fortran / C code examples in the gfortran manual ... applicable to all compilers since it is part of the language. There are some Fortran/C examples in the Intel manual under Intel(R) Fortran/C Mixed-Language Programming. In print, see the Fortran 95 book by Metcalf, Reid and Cohen. For the aspects specific to the Intel compilers, there are many questions and answers on the Intel forums.


The Fortran compiler shouldn't matter since they are all COFF object format. If I understand your question correctly you are trying to call c/c++ from fortran but your main gui is VS 2008. Since your main gui is vs 2008 I am further assuming your main body source code is c/c++ and you want to call a fortran routine that calls a c/c++ routine. Given this you need to first compile ONLY your c/c++ functions into an object file using VS 2008. Then compile your fortran routine into an object file referencing those c/c++ functions (with the c/c++ object file) using the Intel Fortran compiler. This should now give you a new object file that combines the fortran and c/c++ functions. Now you can use this object file back with your Visual Studio main body source code. The biggest thing to remember is whenever you compile to object code make sure you statically link the run-time libraries. The compiler switches and syntax for calling external language routines you'll have to find out yourself since there is TON of material on the internet. Here's a good reference http://docs.cray.com/books/S-2179-52/html-S-2179-52/ppgzmrwh.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜