Compile the Fortran program in Windows using gfortran - linux

I have a Fortran program(.f) that I have written in Ubuntu Linux. I compiled the written Fortran program in Linux by using the below command and it it successfully executed.
gfortran -o program program.f
Now I want to execute the same Fortran program in Windows 10 Can it be executed in window system? If so please suggest me a way to do it.
I tried gfortran -o program program.f in Windows command window,but it fails.

To compile Fortran code for Windows you need a Fortran compiler for Windows. Microsoft neither provides a built-in one nor offers one for sale. Third-party compilers are available, including gfortran, but you'll need to install one yourself. If you want to use gfortran in particular, or if you like it simply because you don't have to spend money to get it, then I would recommend obtaining it as part of mingw-w64. Alternatives are available from multiple vendors, some free of charge, but most for sale.
Note also that Windows expects executables to be named with an .exe extenstion, so you would want to use a variation on gfortran your compilation command:
gfortran -o program.exe program.f

If you want to use gfortran on Windows, I suggest you install MSYS2, which has a bash terminal, and a package manager that can install gcc and gfortran, as well as lapack and many other libraries.
There is also a separate distribution of mingw-w64 that can be installed without MSYS2, but I don't recommend it, as the last files there have gcc-8.1.0, from 2018 (apart from a recent build by Ray Linn that includes the Ada, but not the Fortran compiler).
Another compiler that is now free is Intel Fortran : you have to install Microsoft Visual Studio Community, Intel oneAPI Base Toolkit and Intel oneAPI HPC Toolkit. More information here. Available on Linux, macOS and Windows (of course, Visual Studio is needed only on Windows). Intel oneAPI is at least partly open source, not sure about the Fortran compiler.
MSYS2 is a much smaller package (in terms of disk pace needed), and is used by several other free projects: R (Rtools), Octave and Strawberry Perl all include parts of it, including the gcc compilers.

Related

How do I compile and link a 32-bit Windows executable using mingw-w64

I am using Ubuntu 13.04 and installed mingw-w64 using apt-get install mingw-w64. I can compile and link a working 64-bit version of my program with the following command:
x86_64-w64-mingw32-g++ code.cpp -o app.exe
Which generates a 64-bit app.exe file.
What binary or command line flags do I use to generate a 32-bit version of app.exe?
That depends on which variant of toolchain you're currently using. Both DWARF and SEH variants (which come starting from GCC 4.8.0) are only single-target. You can see it yourself by inspecting the directory structure of their distributions, i.e. they contain only the libraries with either 64- or 32-bit addressing, but not both. On the other hand, plain old SJLJ distributions are indeed dual-target, and in order to build 32-bit target, just supply -m32 flag. If that doesn't work, then just build with i686-w64-mingw32-g++.
BONUS
By the way, the three corresponding dynamic-link libraries (DLLs) implementing each GCC exception model are
libgcc_s_dw2-1.dll (DWARF);
libgcc_s_seh-1.dll (SEH);
libgcc_s_sjlj-1.dll (SJLJ).
Hence, to find out what exception model does your current MinGW-w64 distribution exactly provide, you can either
inspect directory and file structure of MinGW-w64 installation in hope to locate one of those DLLs (typically in bin); or
build some real or test C++ code involving exception handling to force linkage with one of those DLLs and then see on which one of those DLLs does the built target depend (for example, can be seen with Dependency Walker on Windows); or
take brute force approach and compile some test code to assembly (instead of machine code) and look for presence of references like ___gxx_personality_v* (DWARF), ___gxx_personality_seh* (SEH), ___gxx_personality_sj* (SJLJ); see Obtaining current GCC exception model.

how to make excutable files based on source codes

I write some programs on linux with C
I want to run these programs on many remote computers, which are installed with fedora or ubuntu
I compiled the program with gcc on local machine, however the excutable file is not workable on remote machines.
for example: I use
gcc -o udp_server udp_server.c
on local machine to get a excutable binary file udp_server and then I copy it to a remote machine and run it there, the error is:
-bash: ./udp_server: /lib64/ld-linux-x86-64.so.2: bad ELF interpreter: No such file or directory
the local machine: fedora
Fedora release 16 (Verne)
Kernel \r on an \m (\l)
3.6.10-2.fc16.x86_64 GNU/Linux
the remote machine:
Fedora release 12 (Constantine)
Kernel \r on an \m (\l)
2.6.32-36.onelab.x86_64 GNU/Linux
on these remote machines, there are no gcc compiler
so I hope I can make some excutable files so that they can be executed on those remote machines
so what kind of excutable files should I make, and how to make them?
any recommenation tools or procedures?
thanks!
To run a program written in C, you must first compile it to produce an executable file. On Linux, the C compiler is typically the "Gnu C Compiler", or gcc.
If you compile a C program on Linux, it should usually run on any other Linux computer. However, a few conditions must be met for this to work:
A compiled executable is compiled for a specific processor architecture. For example, if you compile for x86-x64, the program will not run on x86 or PowerPC.
If the program uses shared libraries, these must be installed on the target system. The C library, "libc" is installed everywhere, other libraries may not be.
As to how to compile: For a simple program, you can invoke gcc directly. For more complex programs, some build tool is advisable. There are many to choose from; two popular choices are GNU make (the traditional solution), and CMake.
To distribute the program: If it is only a single executable, you can just copy this executable around. If the program consists of multiple files (images, data files, etc.), you should package it as a software package. This allows users to install it using a package manager such as RPM or dpkg. How to do this is explained in various packaging guides for the different Linux distributions.
Finally, a piece of advice: You seem to know very little about software development in general and in C in particular. Consider reading some tutorial on programmin in C - this will answer these (and many other) questions. There are countless books and online tutorials - I can recommend "The C book", by gbdirect.
The issue you see is you are missing a dynamic library on the target machine. To see which libraries you need you need to use "ldd" program. Example (I just execute it against standard program "test" which is in every single linux distribution):
$ ldd /usr/bin/test
linux-vdso.so.1 => (0x00007fff5fdfe000)
libc.so.6 => /lib64/libc.so.6 (0x00000032d0600000)
/lib64/ld-linux-x86-64.so.2 (0x00000032cfe00000)
On Fedora and RHEL you can find which RPM package you want to install using the following command
$ rpm -q --whatprovides /lib64/ld-linux-x86-64.so.2
glibc-2.16-28.fc18.x86_64
And then you need to install it:
$ yum -y install glibc-2.16-28.fc18.x86_64
I dont use Ubuntu / Debian, not sure how to do this. Please note that on 32bit systems libraries for 64bits are not avaiable, but on 64bit systems these libraries have usualla i686 tag and are installable.
Usually, you can execute your program on different machines as long as you keep the architecture. E.g. you cannot execute 64bit program on a 32bit machine, and also vice versa (you can workaround this by installing 32bit libs but thats maybe too difficult).
If you have different distributions, or different versions of same linux distribution, this might be problem - you need to make sure you have all the dependencies in the same major versions.
Or you can link libraries statically which is not usual in the linux world at all, but you can do this. Learn how to use GCC and then you will find out how to do that.

Compiling Basic C-Language CUDA code in Linux (Ubuntu)

I've spent a lot of time setting up the CUDA toolchain on a machine running Ubuntu Linux (11.04). The rig has two NVIDIA Tesla GPUs, and I'm able to compile and run test programs from the NVIDIA GPU Computing SDK such as deviceQuery, deviceQueryDrv, and bandwidthTest.
My problems arise when I try to compile basic sample programs from books and online sources. I know you're supposed to compile with NVCC, but I get compile errors whenever I use it. Basically any sort of include statement involving CUDA libraries gives a missing file/library error. An example would be:
#include <cutil.h>
Do I need some sort of makefile to direct the compiler to these libraries or are there additional flags I need to set when compiling with NVCC?
I followed these guides:
http://hdfpga.blogspot.com/2011/05/install-cuda-40-on-ubuntu-1104.html http://developer.download.nvidia.com/compute/DevZone/docs/html/C/doc/CUDA_C_Getting_Started_Linux.pdf
To fix the include problems add the cuda include directory to your compilation options (assuming it is /usr/local/cuda/include):
nvcc -I/usr/local/cuda/include -L/usr/local/cuda/lib test.cu -o test
cutil is not part of the CUDA toolkit. It's part of the CUDA SDK. So, assuming you have followed the instructions and you have added the PATH and LIB directories to your environment variables you still need to point to the CUDA SDK includes and libraries directories.
In order to include that lib manually you must pass the paths to the compiler:
nvcc -I/CUDA_SDK_PATH/C/common/inc -L/CUDA_SDK_PATH/C/lib ...
Although I personally prefer not to use the CUDA SDK libraries, you probably will find easier start a project from a CUDA SDK example.

Compiled gcc4.4.6 on one machine, how to let another machine use it?

I built gcc 4.4.6 (to use CUDA) on a fast server, it takes about 10 min. However, on my own desktop, it takes kinda for ever to compile.
So both machines are 64 bit Linux, although 1 is Ubuntu while the other is Arch Linux. Arch Linux has new kernel version.
So on the server, I installed the built gcc-4.4.6 to /opt. And I just copy /opt/gcc-4.4.6 to my PC's /opt/gcc-4.4.6.
em, seems like it doesn't quite work, when I tried
./x86_64-unknown-linux-gnu-gcc ~/Development/c/hello/hello.c
it shows
x86_64-unknown-linux-gnu-gcc: error trying to exec 'cc1': execvp: No such file or directory
So what can I do now?
Thanks,
Alfred
If the systems are similar enough, you could compile GCC on the big machine (don't forget that GCC needs to be configured and built in a directory outside of its source tree), then run make -j3 all and then make install DESTDIR=/tmp/gccinst/ and copy that /tmp/gccinst directory to your small machine, and finally copy it into the root filesystem (on the small machine).
However, GCC 4.4.6 is quite old today, if you are compiling GCC try to compile GCC 4.6.2 (or 4.6.1 at least).
And (shameless plug for my work) if you compile a GCC 4.6, please enable plugins on it, then you might try the GCC MELT [meta-] plugin (MELT is a high level domain specific language to ease the development of GCC extensions).

How can I execute MIPS assembly programs on an x86 linux?

Are there any command line interpreters or any other set of programs around for x86 linux in order to run MIPS assembly programs?
I'd like to be able to write simple MIPS assembly programs and run them from the console on my local machine.
I know of SPIM but it requires X Windows and I'm curious if there are better options out there.
Edit: Turns out it doesn't require X Windows. I still have issues with SPIM. Not the best in my humble opinion. Qemu / Cross compiled toolchain is a little more work but I have less quirks.
Incidentally, Spim does not require X Windows. It has a console interface as well. Run either spim or xspim.
You will need either a cross compilation toolchain, or to build your own cross binutils.
For a prebuilt toolchain, you can visit code sourcery. If you just want to compile assembly, then all
you need is binutils. There are some guidelines on the Linux Mips wiki
For the emulation part, QEmu would be my choice.
MARS made my assembly programming for MIPS architecture so much easier. If you would like a GUI/IDE, I would recommend MARS for sure.
I was in the same situation yesterday. I also didn't like SPIM, so this is what I did:
installed gxemul and gxemul-doc (those are the package names on debian)
installed netbsd on an emulated MIPS machine following the detailed instructions in the documentation
since netbsd already includes the standard gcc toolchain and vi, you're good to go.
Setting up networking is pretty easy and well documented, too. This has the advantage of not needing to fiddle with cross compilation.
You could use gxemul, which emulates a MIPS machine (among others, including Dreamcast), and is able to run many Operating systems (included linux, netbsd and some more).
gxemul-wikipedia
gxemul-home page
QEmu has a good MIPS emulator. Combine that with a cross-compiled GCC/binutils (technically you only need binutils to get GAS, the GNU assembler) and you're good to go.
Assuming you wish to use GCC.
Steps for compiling for MIPS on an x86-64 system, and then running the executable using an emulator:
Use a cross-compilation toolchain to produce an executable.
If you are on Debian/Ubuntu, install a cross-compilation toolchain for MIPS. For example, either of these APT packages: gcc-mips-linux-gnu (MIPS big endian) or gcc-mipsel-linux-gnu (MIPS little endian).
Compile using mips-linux-gnu-gcc (mipsel-linux-gnu-gcc for little endian MIPS); assemble using mips-linux-gnu-as; link using mips-linux-gnu-ld.
Run the executable using an emulator.
Install an emulator that can launch Linux programs compiled for one architecture (e.g. MIPS) on another architecture (e.g. x86-64): sudo apt-get install qemu-user.
Run your executable compiled for MIPS using the emulator: qemu-mips ./a.out (or qemu-mipsel ./a.out for little endian MIPS). Simply running ./a.out might also work; the emulator might be used automagically if you (or your distro's qemu package) has set up binfmt-misc to transparently run qemu-user.
Maybe you can take a look at these emulators? I'm not an expert but the list seems good.

Resources