How to compile single fortran objects using translator f2c? - c

I have the following problem: I want to simulate some control engineering system. As it is quite complicated I used the computer to derive some (complicated) equations which can only be exported to fortran77 code. On my development pc this is no problem (linux machine).
No I want to do it in hardware and here I have to use a windows OS. Unfortunately matlab does not support gfortran on windows. Therefore I can not compile the fortran files on that box.
My idea was now to translate to C as a C compiler is available at matlab on win. Any other (better) options?
I have several SUBROUTINES in the manner of the following.
SUBROUTINE sys_rhs (x, v, dx, param)
REAL*8 x(6)
C code is coming here
RETURN
END SUBROUTINE
Is it good practice and does it work to use the function sys_rhs__ in the interface function written in C?
The problem is, that I can not test it as I do not have massive access to the windows machine. So it should work if I try it and I should not need to experiment a lot.
Also what lib's do I need on windows? Where can I get them?
Thanks
Christian

Intel sells a pretty good fortran compiler (http://software.intel.com/en-us/articles/fortran-compilers/) which they used to give away for free for testing ... ;-)
PS No, Im not linked to this company in any way!

You might wanna check the gnumex project, which allows to compile MEX-files using the GNU GCC compiler suite (using either MinGW or Cygwin), with the ability to use g77 to compile Fortran programs.

The answer was quite simple: I had some syntax errors in my fortran code. gfortran does no such strict syntax checking and therefor interpreted the fortran code correctly. After removing the typos I succceded transcoding with f2c.
Thanks

there is a better solution to all this
use your linux machine or your windows machine to implement the function in c/c++ and then generate a dll file.
In matlab you can load the DLL and make a call to the functions inside it.
here is a simple tutorial that show how to call the dll with matlab.
http://www.mathworks.com/matlabcentral/fileexchange/15065-example-of-loading-c-dll-and-performance-comparison

Related

How can I compile ANSI C99-based MEX code delivered with Linux makefiles under Win64 MATLAB?

It seems I've got a real problem here due to my lack of any knowledge about Linux systems:
I have downloaded some open source code, which
is written in C
uses complex.h, so I assume it is ANSI C99
comes with makefiles designed for compilation under Linux systems
provides interfaces to IDL, MATLAB, Python etc.
I am indeed familiar about compiling C/MEX files under Windows-based MATLAB environments, but in this case I don't even know where to start. The project is distributed in several folders and consists of dozens of source and header files. And, to begin with, the Visual Studio 2010 compiler I've used to compile MEX files until now does not comply with the C99 standard, i.e. it does not recognize the complex.h header.
Any help towards getting this project compiled would be highly appreciated. In particular, I have the following questions:
1) Is there any possibility to automatically extract compilation information from the MEX files and transfer it to Windows reality?
2) Is there any free compiler being able to compile C99 stuff, which is also easy to embed in MATLAB?
I have done this (moved in-house legacy code inc. mex files to Win64). I can't recommend the experience.
You will have to recompile, no way around it.
Supported compilers for mex depend on your MATLAB version
This File Exchange entry for using Pelles C may be a starting point (if it works with your version of MATLAB).
I am guessing that there is a main makefile which then works through the makefiles in the subdirectories - have a read through the instructions for compiling under Linux, it will give you some idea of what's going on and may also discuss what to do if you want to change compiler. Once you've found a compatible compiler, the next stage is to understand what the makefiles are doing and edit them accordingly (change paths, compiler, compiler flags, etc.)
Then, from memory (it was a while ago), you get to enjoy a magical mystery tour through increasingly obscure compiler errors. Document everything because if you do get it working, you won't be in a mood to do this twice.
MATLAB R2016b on Windows now supports the MinGW compiler. I'm successfully using this to compile code written primarily for Linux/gcc. I installed this from the Add-On menu in MATLAB (search MinGW).
For my case, I'm building with the legacy code tool. The only thing I needed to do differently than normal was to tell the compiler to support c99 via a compiler flag. This does the trick:
legacy_code('compile', def, {'CFLAGS=-std=c99'})
I had trouble getting the flag command just right (I had some extra quotes that apparently broke things), and asked The MathWorks, so credit is due to their support team for this.
If you are using mex, I would expect to do something very similar.
I would guess that the makefiles are irrelevant for your application; you will need to tell the mex or legacy_code function about all of the files necessary to build the whole application or link against pre-built libraries (which it sounds like you don't have).
I hope this helps!

Tool with GUI used to detect segfault in mixed C and Fortran code

I am working with C code functions that call legacy Fortran code subroutines. The Fortran code subroutines also call other C code functions. The code that I am working with is not the prettiest, but it is fairly challenging to re-write everything from scratch.
Unfortunately, a segfault is occurring somewhere within the program, and I am wondering if there is a software tool with a nice GUI that can be used to accurately pinpoint the location in both C and Fortran code.
Although I am developing the software program on Windows using the Open Watcom compiler to compile both C and Fortran code, I'm willing to move the code to my GNU/Linux box if there is a FOSS tool that can be used.
Alternately, I would wonder if the Intel Composer XE suite could be used to visually pinpoint the location of the segmentation violation. The Intel Composer XE suite includes both C and Fortran compilers.
Since I am working with C and Fortran code, I need some sort of indication where in the code the segfault is occurring. I don't especially want to use calls to the printf() function.
This isn't a GUI, but valgrind is an excellent memory debugging tool. It will tell you not just which line a segfault is on (which you should be able to do with any debugger), but also each time you access uninitialized or unallocated memory, or make a variety of other errors. You do need to port to Linux to use it though.
The Visual Studio integration provided with the Intel Visual Fortran product on Windows allows GUI debugging of mixed Fortran and C code.
On Linux Intel ship their own GUI based debugging environment (idb) with their compiler products.

Automatic generation of Fortran 2003 bindings from C library headers (using iso_c_bindings intrinsic module)

Is there a tool to automatically generate Fortan bindings from C library header, using intrinsic iso_c_bindings module from Fortran 2003 standard?
I am not interested in translating C to Fortran, but only generating bindings.
An automatic tool was used to get the gtk-fortran bindings. It is a Python script cfwrapper.py. You might be able to adapt it to for your needs, although for my small problems I finally chose to make the bindings by hand.
Another project to consider is h2m: https://github.com/dan-nagle/h2m
However, at this time, on macOS, I cannot successfully compile h2m.

Writing C code in Visual C++ on VS2010

I appreciate the differences are negligible, but im doing some number crunching and so i want to use C. Ive just created a project in VS2010, chosen a C++ project and written some C. All executes fine, but
is this being created and executed in the fast(er) C compiler or the C++ because its a C++ project?
How can i specify that the code i wish to write is actually C and to be compiled and run as C?
The Visual Studio C++ compiler will treat all .c files as C language files and compile them as such.
Additional reference:
By default, the Visual C++ compiler
treats all files that end in .c as C
source code, and all files that end in
.cpp as C++ source code. To force the
compiler to treat all files as C
regardless of file name extension, use
the /Tc compiler option.
http://msdn.microsoft.com/en-us/library/bb384838.aspx
You are just being silly now. C is not guaranteed to be faster than C++ in any way - it's all compiled to native machine instructions in the end. If you want a true performance leap you should use another compiler, Intels for example, or use the GPU or something like that.
What will actually give you more speed is to use Intel's compiler, which is available as a plugin. The real-world differences are significant, especially for number crunching. The difference between C and C++ is dubious.
Here's a good place to start: link text
Since you're number crunching, you should consider using SIMD extensions, if possible. Using SIMD on Intel's compiler, vs. straight MS C compiled code, will give you some serious gain.

How to use multiple development languages

I program in Delphi (D7 and D2006) on Windows XP (migrating in the near future to Windows 7). I need to use a mathematical library for some of the work I am doing and most of the math libraries (I am inclining towards Mathematica at present) I have looked at will produce compiled C code. Such code will provide specific functionality to my main programs.
I have a very basic question - given this development setup - how do I start utilising the compiled c code from Delphi? I really need baby steps to get me started on the process.
I've done quite a bit of this with my FE product OrcaFlex. You have two options to link to your C code from Delphi: static or dynamic. I link statically because it makes distribution and versioning much easier. But it's really quite a trick to get it to work statically and you have to rely on a number of undocumented aspects of Delphi.
I suspect that for your needs dynamic linking is best. Basically you need to compile and link your C code into a DLL. I recommend using the Borland C compiler to do this. You can use the free command line version BCC55 to do this. The advantage of using Borland C is that it makes the same assumptions about the 8087 floating point unit as Delphi does. If you build with MSVC then you will find that MS have elected not to raise floating point exceptions. Borland C does raise floating point exceptions. This is a bit of a corner case but it becomes relevant if you are trying to ship a product that you need to be robust.
You should know that the C code will, by default, use the C calling convention and I'd just stick with that. You bring it into Delphi by declaring the external routine as cdecl calling convention.
The other thing you need to take care on is defining a clear interface between the two modules. You need to make sure that exceptions don't cross the module boundary and that you don't pass any special types (e.g. Delphi strings) across the boundary. So for a string use a PChar (or even better PAnsiChar or PWideChar to be sure that it won't change meaning when you upgrade to Delphi 2009 and later).
I have been very happy with the SDL Library from Lohninger (http://www.lohninger.com/mathpack.html). It is written in Delphi and compiles right into your application, so there are no bundling or calling convention problems or floating point usage differences, as discussed by other responses in this thread.
Take a look at what he includes. If you're lucky, your needs will be met by his library and you'll be able to use it!
If you currently have Mathematica installed, go to the documentation centre and lookup guide/CLanguageInterface otherwise that guide is available on the web and have a good read there.
My understanding is that Mathematica can generate C-programs that link up with the Mathematica engine via MathLink if you need full function, or if you only need lower-level features then it is capable of generating code that can be statically linked with compiled Mathematica libraries. So that standalone code is possible.
See the Code Generator documentation.
If you can convert the C programs in to DLLs, then accessing such external functions from Delphi is relatively simple with external declarations.
function MathematicaRoutine(const x : double) : double; external 'MyInterface.dll';
There are bound to be a great number of complexities in getting this to work if you need to achieve a static bind, for use where Mathematica is not installed, if indeed it is possible. I have never attempted it.
You can mix your project with Delphi and C++ (Builder) code using RAD Studio. Put the automatically created C code into a C++ Builder file (.cpp) and for the rest add Delphi files.

Resources