Can't make OpenMP work with CodeBlocks and GFortran - gcc

For some reason, I cant make OpenMP work with CodeBlocks and GFortran. I tried another IDE (Geany) and it works properly, but with CodeBlocks it does not. I must say that I included the -fopenmp option in CodeBlocks.
Here is a simple code that I tested.
program test
implicit none
integer i
!$OMP PARALLEL DO DEFAULT(PRIVATE)
Do i=1,10
Write(*,*) i
end do
!$OMP END PARALLEL DO
end program test
And here is the build log:
-------------- Build: Debug in TEST (compiler: GNU Fortran Compiler)---------------
gfortran.exe -Jobj\Debug\ -Wall -g -fopenmp -c "C:\Users\tilter\Dropbox\Public\Code\Nova pasta\TEST\main.f95" -o obj\Debug\main.o
gfortran.exe -o bin\Debug\TEST.exe obj\Debug\main.o
obj\Debug\main.o: In function `test':
C:/Users/tilter/Dropbox/Public/Code/Nova pasta/TEST/main.f95:5: undefined reference to `GOMP_parallel_start'
C:/Users/tilter/Dropbox/Public/Code/Nova pasta/TEST/main.f95:5: undefined reference to `GOMP_parallel_end'
obj\Debug\main.o:C:/Users/tilter/Dropbox/Public/Code/Nova pasta/TEST/main.f95:6: undefined reference to `GOMP_parallel_start'
obj\Debug\main.o:C:/Users/tilter/Dropbox/Public/Code/Nova pasta/TEST/main.f95:6: undefined reference to `GOMP_parallel_end'
obj\Debug\main.o:C:/Users/tilter/Dropbox/Public/Code/Nova pasta/TEST/main.f95:8: undefined reference to `omp_get_num_threads'
obj\Debug\main.o:C:/Users/tilter/Dropbox/Public/Code/Nova pasta/TEST/main.f95:8: undefined reference to `omp_get_thread_num'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
6 errors, 0 warnings (0 minutes, 0 seconds)
I don't know what I'm missing here...

I just restarted Windows 7 and it fixed the problem.

Related

Compiling and linking static library with gfortran [duplicate]

This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Compiling Fortran netCDF programs on Ubuntu
(2 answers)
Closed 4 years ago.
I want to compile some of my often-used Fortran code into a static library (assuming I would have access to both *.mod and *.a files).
This worked.
However, I have encountered the problem with linking.
Here is an example. Let's say I have mylib.f90 file:
MODULE MYLIB
IMPLICIT NONE
CONTAINS
INTEGER FUNCTION FOO(N)
INTEGER, INTENT(IN) :: N
FOO = N + 2
END FUNCTION FOO
END MODULE MYLIB
Which is compiled as
gfortran -c mylib.f90
ar rcs libmylib.a mylib.o
Now, I have a program that uses FOO function:
PROGRAM MYPROG
USE MYLIB
IMPLICIT NONE
INTEGER M
M = FOO(3)
WRITE (*,*) M
END PROGRAM MYPROG
For simplicity I put it in the same directory as mylib.mod and libmylib.a. Compilation:
gfortran -c myprog.f90
no issues. Linking, however results in error:
gfortran -L./ -lmylib myprog.o
myprog.o: In function `MAIN__': myprog.f90:(.text+0x11): undefined
reference to `__mylib_MOD_foo' collect2: error: ld returned 1 exit
status
However, symbol __mylib_MOD_foo is in libmylib.a:
nm libmylib.a
mylib.o:
0000000000000000 T __mylib_MOD_foo
and it compiles without a problem if I give it mylib.o. What am I doing wrong?
PS. I've seen somewhere, but can't find where now the way to link as follows:
gfortran -Wl,--whole-archive libmylib.a -Wl,--no-whole-archive myprog.o
It works. But it should solve the problem with weak symbols, whereas the output of nm didn't mark __mylib_MOD_foo as weak.

Makefile deleted my fortran program

Hi I've never written a makefile before, but I tried my hand at it with my fortran90 final project and the makefile seems to have delete my main program. here's my makefile
# Sample makefile for several modules
#
FC = gfortran
final.x: subs.o func.o maina.o
gfortran -o final.x subs.o func.o maina.o
subs.o: subs.f90
gfortran -c subs.f90
func.o: func.f90
gfortran -c func.f90
maina.o: maina.f90
gfortran -c maina.f90
after running this, my maina.f90 was deleted and the I did not have a copy. this was what it showed when it was running. (The first output is when I ran it and found an error in subs, and after fixing these errors, I got the second output)
$ make
gfortran -o final.x subs.o func.o maina.o
subs.o: In function `__subs_MOD_gauss':
subs.f90:(.text+0x350): undefined reference to `f_'
subs.f90:(.text+0x366): undefined reference to `f_'
subs.o: In function `__subs_MOD_simp':
subs.f90:(.text+0x434): undefined reference to `f_'
subs.f90:(.text+0x4a2): undefined reference to `f_'
subs.f90:(.text+0x51b): undefined reference to `f_'
subs.o:subs.f90:(.text+0x571): more undefined references to `f_' follow
collect2: ld returned 1 exit status
make: *** [final.x] Error 1
$ make
gfortran -c subs.f90
gfortran -o final.x subs.o func.o maina.o
does anyone know why this file deleted my maina.f90, or (though it's probably unlikely) how to get my work back?
EDIT- I should add that I do not have admin or sudo privileges on this computer

Undefined reference error from a makefile for a Fortran code with HDF5 [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I am trying to compile a Fortran 90 code that uses HDF5. For that purpose, I am using the following Makefile:
# Location of HDF5 binaries (with include/ and lib/ underneath)
HDF5 = /fs/posgrado16/other0/guido/libraries/hdf5/serial
# Compiler
FC = gfortran
# ------ No machine-specific paths/variables after this -----
FORTRANLIB=-I$(HDF5)/include $(HDF5)/lib/libhdf5_fortran.a
FSOURCE = h5_crtgrpar.f90
OBJECTS = $(FSOURCE:.f90=.o)
EXECUTABLE = $(FSOURCE:.f90=.exe)
LIBSHDF = $(FORTRANLIB) $(HDF5)/lib/libhdf5.a
all:$(EXECUTABLE)
$(EXECUTABLE):$(OBJECTS)
$(FC) -o $# $^ $(LIBSHDF)
$(OBJECTS):$(FSOURCE)
$(FC) -c $# $< $(LIBSHDF)
.PHONY : clean
clean:
rm -f $(FSOURCE) $(OBJECTS) *.h5
But, I get the following error:
$ make -f Makefilef
gfortran -o h5_crtgrpar.exe h5_crtgrpar.o -I/fs/posgrado16/other0/guido /libraries/hdf5/serial/include /fs/posgrado16/other0/guido/libraries/hdf5/serial/lib/libhdf5_fortran.a /fs/posgrado16/other0/guido/libraries /hdf5/serial/lib/libhdf5.a
/fs/posgrado16/other0/guido/libraries/hdf5/serial/lib/libhdf5.a(H5PL.o): In function `H5PL_term_interface':
H5PL.c:(.text+0x205): undefined reference to `dlclose'
/fs/posgrado16/other0/guido/libraries/hdf5/serial/lib/libhdf5.a(H5PL.o): In function `H5PL_load':
H5PL.c:(.text+0x477): undefined reference to `dlsym'
H5PL.c:(.text+0x5be): undefined reference to `dlopen'
H5PL.c:(.text+0x5d7): undefined reference to `dlsym'
H5PL.c:(.text+0x704): undefined reference to `dlclose'
H5PL.c:(.text+0x789): undefined reference to `dlerror'
H5PL.c:(.text+0x960): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
make: *** [h5_crtgrpar.exe] Error 1
I have no idea what the error is. Probably, there is something wrong with my Makefile.
To compile Fortran code with HDF5 enabled, you can replace
FC = gfortran
by
FC = h5fc
and skip all the hdf5 flags, as the h5fc wrapper will take care of those.
If you have some specific reason of calling the compiler by its name, you can learn about what flags are needed by calling
h5fc -show
that will show you what flags are added to the compiler.
On my computer (linux with gfortran), the result is:
gfortran -g -O2 -fstack-protector-strong -I/usr/include/hdf5/serial -L/usr/lib/x86_64-linux-gnu/hdf5/serial /usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5hl_fortran.a /usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5_hl.a /usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5_fortran.a /usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5.a -Wl,-z,relro -lpthread -lz -ldl -lm -Wl,-rpath -Wl,/usr/lib/x86_64-linux-gnu/hdf5/serial
Often you can get by with less flags than that, which you can find with some experimentation.
Given the error message you report, you lack the -ldl flags that enables linking of the dynamic linking library, see for isntance this other SO question.

Compiling with gfortran: undefined reference to iargc_

I'm using gfortran [GNU Fortran (GCC) 4.8.3 20140911 (Red Hat 4.8.3-7)] on a Fedora 20 x86_64 to compile a bunch of Fortran 77 code which refers to 'iargc' function in the following manner:
bin2D2nc.f:31: integer iargc,strlen1
bin2D2nc.f:32: external iargc,strlen1
bin2D2nc.f:44: i=iargc()
When the make script reaches the compilation comand bellow,
gfortran -O3 -ffixed-line-length-132 -fall-intrinsics -I/home/santiago/Install/netcdf_sam/include -o bin2D2nc -I./SRC ./SRC/bin2D2nc.f ./SRC/hbuf_lib.f ./SRC/cape.f ./SRC/cin.f -L/home/santiago/Install/netcdf_sam/lib -lnetcdf -L/usr/lib64 -lpthread
I receive these messages:
bin2D2nc.f:(.text+0x14): undefined reference to `iargc_'
collect2: error: ld returned 1 exit status
make: ** [bin2D2nc] Erro 1
I'm not the author of this code. As far as I know, I set up correctly the library paths in the makefile.
I have found that 'iargc' is a routine for backward compability with GNU Fotran 77, but I don't understand it deeply.
Could someone give some advise to surpass this problem?
The problem is very similar to Fixing FORTRAN IV warning: "The number of arguments is incompatible with intrinsinc procedure, assume 'external' " but the difference is that in the other question there was an external function present and the similarity with an intrinsic was inadvertent, but you are calling the intrinsic on purpose.
The statement
EXTERNAL IARGC
meant that IARGC is an external or an intrinsic function in FORTRAN 66, but in "modern Fortran" 77 and later it means that it is an external function only.
But you need to call the intrinsic function https://gcc.gnu.org/onlinedocs/gfortran/IARGC.html .
You should use
INTRINSIC IARGC
or even just delete IARGC from the EXTERNAL statement without adding anything else. The compiler will then stop searching for a non-existent external function and will use the intrinsic.
A final note, IARGC itself is not standard Fortran, ut it shouldn't matter here.

Customise a generic makefile for OS X ( using homebrew/gfortran/open-mpi)

I'm trying to compile a piece of scientific code called DLPOLY (with multicore processing support). Instructions online for how to do this seem to be out of date, and the makefile only includes examples for big computing clusters and not a home computer.
Instructions are as follows:
Generic target template
unknown_platform:
$(MAKE) LD="path to FORTRAN90 Linker-loaDer" \
LDFLAGS="appropriate flags for LD (MPI libraries)" \
FC="path to FORTRAN90 compiler" \
FCFLAGS="appropriate flags for FC (MPI include)" \
EX=$(EX) BINROOT=$(BINROOT) $(TYPE)
I have gfortran and open-mpi installed via homebrew, along with the Xcode CLTs. I don't mind using, e.g., macports if it's easier.
edit: e.g. I tried this:
LD="ld"
LDFLAGS="-L/usr/local/lib"
FC="gfortran"
FCFLAGS="-I/usr/local/include/"
but then get this error:
'Use mpi_module' must change to 'Use mpi' in 'comms_module.f90'
gfortran -I/usr/local/include/ kinds_f90.f90
Undefined symbols for architecture x86_64:
"_MAIN__", referenced from:
_main in libgfortranbegin.a(fmain.o)
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make[1]: *** [kinds_f90.o] Error 1
so I have no idea what I need to do. After further googling, I then tried:
LD="mpif90"
LDFLAGS="-m64"
FC="mpif90"
FCFLAGS="-m64"
but I got a similar error message...
Progress has been made. It starts compiling with these settings, though I don't properly understand them:
$(MAKE) FC="mpif90" LD="mpif90 -o" \
LDFLAGS="-O2 -ffast-math" \
FFLAGS="-c -O2 -ffast-math"\
EX=$(EX) BINROOT=$(BINROOT) $(TYPE)
But eventually errors-out with:
mpif90 -c -O2 -ffast-math set_bounds.f90
set_bounds.f90:36.23:
zero_plus = Nearest( 0.0_wp , 1.0_wp)
1
Error: Result of NEAREST underflows its kind at (1)

Resources