Compiling Fortran77 with Julia - fortran

I have a bunch of Fortran77 code that I need to use for my research but I'm having trouble compiling it to run on my MacBook so I turned to Julia. I'm new to the language but for the life of me I can't figure out how to execute a Fortran script directly in Julia. All I want is to have a program that runs a F77 script and hands control directly to Fortran. I would just rewrite it with Julia or Numpy but there's about 10,000 lines of code and less than 200 lines of comment and I don't have time for that.

It seems from the wording of your question like you want to use Julia to directly call Fortran "scripts" – presumably Fortran .f source files – is that accurate?
As others have indicated in the comments, Fortran is not a scripting language: you cannot directly execute Fortran source files; instead you must use a Fortran compiler (e.g. gfortran, ifort) to translate Fortran programs into native libraries or executables for the system you want to run programs on. Julia will not help with this in any way as Julia is not a Fortran interpreter or compiler – it can neither run Fortran code directly nor convert Fortran source files into executables/libraries.
If, however, you already have a Fortran shared library compiled (.so file on Linux, .dylib on macOS, .dll on Windows), you can call it easily from Julia, as described in Integrating Fortran code in Julia. If you can compile Fortran source code to an executable (as opposed to a shared library), then you do not need anything else to run it – executables, by definition, are standalone.
Most projects in compiled languages like Fortran or C/C++ come with Makefiles or other mechanisms to help invoke a compiler to generate the appropriate binary artifacts (executables and/or libraries).

Related

What is Cmake file and why do we use it when we have VIsual studio

Sorry for asking such basic question. Being a beginner in C++, I'm puzzled about a lot of things:
if we have Visual stuido and other IDEs, as well as Cygwin as compilers, what kind of roles do CMake play in helping a C++ programmer to develop programmes? Can we just write C++ programmes without using C Make?
I understand header files are like prototypes of functions that will be used in C++ files, or declarations of them, so I assume all header files should be accompanied by actual library files in the form of C++ files, as that's where the functions know what to do when being called. However,this is not usally the case, as there are header only libraries such as PyBind11.
What is bash? is it like another Windows command prompt? Whats its relationship wth C++?
Why do we have function macros and what is its advantages over normal functions?
Thank you very much!
CMake is a buildsystem.
if we have Visual stuido and other IDEs, as well as Cygwin as compilers, what kind of roles do CMake play in helping a C++ programmer to develop programmes? Can we just write C++ programmes without using C Make?
CMake allows for crossplatform development. It allows you to create projectfiles/Makefiles/... from a CMakeLists.txt file ==> The file suitable for the platform is created.
If you use CMake, you can simply program on windows (CMake will generate Visual Studio Solution Files) and if somebody tries to compile it on linux, CMake will generate Makefiles.
Using CMake, you only have one buildsystem and don't have to write buildfiles for every platform you support
I understand header files are like prototypes of functions that will be used in C++ files, or declarations of them, so I assume all header files should be accompanied by actual library files in the form of C++ files, as that's where the functions know what to do when being called. However,this is not usally the case, as there are header only libraries such as PyBind11.
I don't understand the question, but a header-only library hasn't to be linked to your program, as all the source code is in the headers.
What is bash? is it like another Windows command prompt? Whats its relationship wth C++?
bash is a shell, that executes commands. It's like the command prompt, only sometimes a bit better. It has no relationship to C++.
Why do we have function macros and what is its advantages over normal functions?
Macros have rarely advantages over functions, because the clutter the code, increase the compilation times (Because the preprocessor inserts a copy of the macro everytime, it is applied==>More code, slower compilation).
The only advantage I can think of, is in a situation like this for conditional compilation:
#ifdef DEBUG
#define LOG(x) printf(x)
#else
#define LOG(x)
#endif
what kind of roles do CMake play in helping a C++ programmer to develop programmes?
Can we just write C++ programmes without using C Make?
Yes. It is possible to write C++ programs without any tools. It is possible to even compile C++ without any build system.
However, building complex C++ programs without a build system is difficult and tedious. Build systems help to keep the complexity under control.
But using a platform specific build system hinders compilation on systems that don't have that specific build system.
CMake solves this dilemma by being able to generate builds for several build systems on various platforms. So CMake is a generator for build systems.
so I assume all header files should be accompanied by actual library files in the form of C++ files
Your assumption is not quite correct. It is a decent rule of thumb that applies often, but not always.
What is bash?
bash is a shell. It is the most commonly used shell on POSIX systems. On windows, bash can be used through WSL or Cygwin.
is it like another Windows command prompt?
Windows command prompt is another shell. It is used only on windows.
Whats its relationship wth C++?
There is no close relationship between bash and C++.
Bash is often used to write shell scripts. Such shell scripts are sometimes used to help with building complex C++ programs.
Why do we have function macros
For text replacement within the source code.
what is its advantages over normal functions?
Functions cannot replace text in source code.
Use cases for macros (functional or otherwise) are rare. They are sometimes overused, which is not a good thing. Generally, functions have many advantages over macros. Among the advantages of functions are type safety and scoped names.
CMake is a kind of a build system. Build systems help to organize all files in the project, handle proper order of compilation, linking, and other steps that are necessary to obtain a binary (executable or library). They allow describing the process of building in an abstract way, reducing dependencies between project and other build tools, e.g. you can easily change the compiler.
CMake is a special flavor of the build systems because it doesn't handle building by itself. It generates scripts of other build systems, e.g. Makefiles on Linux. That allows for a multiplatform approach to building applications.
Considering Your questions about macros and headers, I would suggest a good C++ book
Bash is a system shell with its own scripting language, allowing for running system scripts. It is mostly used on Linux based systems, however it can be used also on windows.

If C++ compiles to machine code, why do we need to install a 'runtime'?

At the end of the compilation process, the program is in a .exe file in machine code. So shouldn't the machine be able to run it without having to install something like MS Visual Studio C++? Basically, I am making a program with mingw and want to share it with someone else. I do not understand why I can not just send them the .exe file. Clarification will be appreciated.
C++ compiles your code to machine code. If your program is self-contained, that is all you need. However, more complex running programs often relies on additional compiled code, which is made available to your program through a library.
Generally, libraries come in two "flavors" - static and dynamic. Static libraries are "baked into" your compiled code. This is not ideal, because multiple programs include identical code, leading to code duplication. Dynamic libraries, on the other hand, are shared among all programs using them, leading to more efficient use of space.
Installing runtime adds dynamic libraries for use by all programs compiled with C++.
Your program likely calls many functions from the standard library that you didn't write yourself. You need the runtime libraries for that. Your code probably also needs code run before main to setup the basic environment that's expected for a C++ program - the runtime libs do that for you. Also after main ends, various cleanup needs to happen according to the standard (and your program probably also depends on this) and the compilers runtime libraries take care of this.
Your code does not exist in a vacuum (it can, but then it's no longer a standard hosted C++ program). It depends on and relies on the standard runtime libs to provide the environment the C++ standard says you can expect.

Difference between code object and executable file

I'm a C++ beginner and I'm studying the basics of the language. There is a topic in my book about the compiler and my problem is that I can not understand what the text wants to say:
C++ is a compiled language so you need to translate the source code in
a file that the computer can execute. This file is generated by the
compiler and is called the object code ( .obj ), but a program like
the "hello world" program is composed by a part that we wrote and a part
of the C++ library. The linker links these two parts of a program and
produces an executable file ( .exe ).
Why does my book tell that the file that is executed by the computer is the one with the obj suffix (the object code) and then say that it is the one with the exe suffix?
Object files are source compiled into binary machine language, but they contain unresolved external references (such as printf,for instance). They may need to be linked against other object files, third party libraries and almost always against C/C++ runtime library.
In Unix, both object and exe files are the same COFF format. The only difference is that object files have unresolved external references, while a.out files don't.
The C++ specification is a technical document in English. For C++11 have a look inside n3337 (or spend a lot of money to buy the paperback ISO standard). In theory you don't need a computer to run a C++ program (you could use a bunch of human slaves, but that would be unethical, inefficient, and unreliable).
You could have a C++ implementation which is an interpreter, not a compiler (e.g. Ch by SoftIntegration)
If you install Linux on your laptop (which I recommend doing to every student) then you could have several free software C++ compilers, in particular GCC and Clang/LLVM (using g++ and clang commands respectively). Source files are suffixed .cc, or .cxx, or .cpp, or even .C (I prefer .cc), and you could ask the compiler to handle a file of some other suffix as a C++ source file (but that is not conventional). Then, both object files (suffixed .o) and executables share the same ELF format. Conventionally, executables don't have any suffix (e.g. g++ is a binary executable, not doing much except starting other processes like cc1plus -the compiler proper-, as -the assembler-, ld -the linker- etc...)
In all cases I strongly recommend:
to enable all warnings and debug info during compilation (e.g. use g++ -Wall -g ....)
to improve your source code till you got no warnings
to learn how to use the debugger (gdb)
to be able to build your program on the command line
to use a version control system like git
to use a good editor like emacs, gedit, geany, or gvim
once you are writing programs in several source files, learn how to use a builder like make
to learn C++11 (or even perhaps C++14) rather than older C++ standards
to also learn other programming languages (Ocaml, Scheme, Haskell, Prolog, Scala, ....) since they would improve your thinking and your way of coding in C++
to study the source code of several free software coded in C++
to read the documentation of every function that you are using, e.g. on cppreference or in man pages (for Linux)
to understand what is undefined behavior (the fact that your program sometimes work does not make it correct).
Concretely, on Linux you could edit your Hello World program (file hello.cc) with gedit or emacs (with a command like gedit hello.cc) etc..., compile it using g++ -Wall -g hello.cc -o hello command, debug it using gdb ./hello, and repeat (don't forget to use git commands for version control).
Sometimes it makes sense to generate some C++ code, e.g. by some shell, Python, or awk script (or even by your own program coded in C++ which generates C++ code!).
Also, understand that an IDE is not a compiler (but runs the compiler for you).
The basic steps for creating an application from a C or C++ source file are as follows:
(1) the source files are created (by a person or generated by a program), (2) the source files are compiled (which is really two steps, Preprocessor and compilation) into object code, (3) the object files that are created by the C/C++ compiler are linked to create the .exe
So you have these steps of transforming one version of the computer program, the source files, to another, the executable. The C++ source is compiled to produce the object files. The object files are then linked to produce the executable file.
In most cases there are several different programs involved in the compile and link process with C and C++. Each program takes in certain files and creates new files.
C/C++ Preprocessor takes in source code files and generates source code files
C/C++ Compiler takes in source code files and generates object code files
the linker takes in object code files and libraries and generates executable files
See What is the difference between - 1) Preprocessor,linker, 2)Header file,library? Is my understanding correct?
Most compiler installations have a program that runs these various applications for you. So if you are using gcc then gcc program will run first the C++ Preprocessor then then C++ compiler and then the linker. However you can modify what gcc does with command line options to tell it to only run the C++ Preprocessor or to only compile the source files but not to link them or to only link the object code files.
A brief history of computer languages and programming
The languages used for programming computers along with the various software development tools have evolved over the years.
The first computers were programmed with numbers entered by switches on a console.
Then people started developing languages and software that could be used to create software more easily and quicker. The first major development was creating assembler language where each line of source was converted by a computer program into a machine code instruction. Along with this came the development of linkers (which link pieces of machine code together into larger pieces). Assemblers were improved by adding a macro or preprocessor facility somewhat like the C/C++ Preprocessor though designed for assembly language.
Then people created programming languages that looked more like people written languages rather than assembler (FORTRAN and COBOL and ALGOL for instance). These languages were easier to read and a single line of source might be converted into several machine instructions so it was more productive to write computer programs in these languages rather than assembler.
The C programming language was a later refinement using lessons learned from the early programming languages such as FORTRAN. And C used some of the same software development tools that already existed such as linkers which already existed. Still later C++ was invented, starting off as a refinement of C introducing object oriented facilities. In fact the first C++ compiler was really a C++ translator which translated C++ source code to C source code which was then compiled with a C compiler. However modern C++ is compiled straight to machine code in order to provide the full functionality of the C++ standard with templates, lambdas, and all the other things with C++11 and later.
linkers and loaders
When you run a program you run the executable file. The executable file contains several kinds of information. The first is the machine instructions that are the result of compiling the C++ source code. The other is information that the loader uses in order to know how to load the executable into memory.
In the old days, long long ago all libraries and object files were linked together into an executable file and the executable file was loaded by the loader and the loader was pretty simple.
Then people invented shared libraries and dynamic link libraries and this required the linker to be more complex and the loader to be more complex.
The linker became more complex because it had to be able to recognize the difference between using a shared library and a static library and be able to generate an executable file that not only contains the linked object code but also information for the loader about any dynamic libraries.
The loader became more complex because not only does the loader have to load the executable file into memory so that it can start running, the loader must also find any shared libraries or dynamic link libraries that are also needed and load those too. And the loader also has to do a certain amount of linking of the additional components, the shared libraries, so the loader does a lot more than it used to do.
See also
Difference between shared objects (.so), static libraries (.a), and DLL's (.so)?
What is an application binary interface (ABI)?
How to make a SIMPLE C++ Makefile
Object code (within an object file): Output from a compiler intended as input for a linker (for the linker to produce executable code).
Executable: A program ready to be run (executed) on a computer

Octave - .m-File Compiler?

I know that with Matlab it is possible to compile the scripts / functions as an executable which can then be a standalone version together with Matlab Compiler Runtime.
Is there any possible way to compile .m-files associated with octave as an executable program in a way that the .m-files aren't needed anymore for it to run?
I would like to a have a standalone version of the script (possibility together with octave libraries / dlls) without the necessity of the .m-Files.
I believe it is, see for example How do I create a simple Octave distributable without installing Octave, which references the relevant part of the documentation. However, I have never tried it, and it doesn't appear to be that straightforward (at first impression at least).

How to use C++ compiler by Matlab Mex compilation tool

I am trying to build a source code bundle containing m files and c++ (cpp) source files in Matlab.
The source folder has a simple Matlab Script to compile all cpp files in one folder:
function compileDir_simple(Cdir)
if nargin<1
Cdir=pwd;
end
files = dir(fullfile(Cdir,'*.cpp'));
oldDir=pwd;
cd(Cdir);
for j=1:length(files)
try
cm = sprintf('mex -largeArrayDims %s',files(j).name);
disp(cm);
eval(cm);
catch
disp(lasterr);
disp('IGNORE if the file is a C++ file which is not a mex file (ie without a mexFunction inside)');
end
end
cd(oldDir);
Inside, it uses "mex -largeArrayDims". However, my problem is, when I evaluate that statement Matlab tries to build the selected files by a C compiler which is contained in MATLAB itself. When I call mex -setup I see:
mex -setup
Please choose your compiler for building external interface (MEX) files:
Would you like mex to locate installed compilers [y]/n? y
Select a compiler:
[1] Lcc-win32 C 2.4.1 in D:\MATLAB\R2010a\sys\lcc
[0] None
Compiler:
I installed Bloodshed C++ compiler and added its bin folder ( to the Windows Environment variables, but still I cannot see my C++ compiler in the list of installed compilers. Only Lcc-win32 appears. It will be appropriate to state that it is my first experience on compiling mex files.
A mex file on Windows is just a DLL that exports a function named mexFunction. In principle you can compile mex files with any compiler that can create Windows DLLs.
However, to do so using the mex function in MATLAB requires that MATLAB knows about your compiler. And by default MATLAB only has knowledge of a limited number of compilers. Your chosen compiler is not one of them.
Bloodshed is based on mingw. Which means that you should be able to use the Gnumex project to create a mexopts.bat file for use with your compiler.
However, I would be a little sceptical of using Bloodshed here. It is a C++ IDE and I'm not sure you particular need that. I suspect that all you are looking for is a compiler. In which case you would likely be best served by installing plain mingw.
Bloodshed C++ is not a supported compiler. Check http://www.mathworks.co.uk/support/compilers/R2014a/index.html for a list of supported compilers. Older releases are available from the same page.