Makefile deleted my fortran program - makefile

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

Related

Implementing /usr/bin/g++ -DEVAL -std=gnu++11 -O2 -pipe -static -s -o A A.cpp into Visual Studio for task upload

I need to upload a .cpp file for a course application. The code works, i tried their test inputs, but when i upload my file it throws this error
</usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crt1.o: In function _start': (.text+0x20): undefined reference to main'
collect2: error: ld returned 1 exit status>
On the website it has a list of compilation commands that I assume have to somehow put into my code so the website can process my code?
Maybe that isnt even it, this is my first time dealing with this sort of thing, so any help is appreciated.
Tried just putting it into the code outside of main, didnt work.

Unable to link ".so" shared library to main ".c" file while compiling

I am trying to make a shared library for a particular problem I was working on. It has "point_sense.c" as the main file which uses functions defined in "createPolygon.c." The functions are declared in a header file "createPolygon.h."
To compile them, I used a makefile which looks like the following
all:point_sense
createPolygon.o:createPolygon.c
g++ -c -fpic createPolygon.c
libcreatePolygon.so:createPolygon.o
g++ -shared -o libcreatePolygon.so createPolygon.o
point_sense:point_sense.c libcreatePolygon.so
g++ -o point_sense -L~Desktop/Summer_2020_linux/tutorials/cpp_practise point_sense.c -lcreatePolygon
clean:
rm point_sense createPolygon.o libcreatePolygon.so
but when I make the file, it gives an output as
g++ -c -fpic createPolygon.c
g++ -shared -o libcreatePolygon.so createPolygon.o
g++ -o point_sense -L~Desktop/Summer_2020_linux/tutorials/cpp_practise point_sense.c -lcreatePolygon
/usr/bin/ld: cannot find -lcreatePolygon
collect2: error: ld returned 1 exit status
make: *** [makefile:10: point_sense] Error 1
Initially I thought this was some silly mistake, and to check I used
ld -L~/Desktop/Summer_2020_linux/tutorials/cpp_practise -lcreatePolygon -verbose
and after a long output I got (a few unimportant lines in the code are skipped in between)
ld: mode elf_x86_64
attempt to open ~/Desktop/Summer_2020_linux/tutorials/cpp_practise/libcreatePolygon.so failed
attempt to open ~/Desktop/Summer_2020_linux/tutorials/cpp_practise/libcreatePolygon.a failed
attempt to open /usr/local/lib/x86_64-linux-gnu/libcreatePolygon.so failed
attempt to open /usr/local/lib/x86_64-linux-gnu/libcreatePolygon.a failed
.
.
.
ld: cannot find -lcreatePolygon
But when I try to open 'libcreatePolygon.so' directly, I am able to open it.
$ nano ~/Desktop/Summer_2020_linux/tutorials/cpp_practise/libcreatePolygon.so
There are several threads which explain the process of doing this, but I don't see what it is that I am doing wrong. Any help is appreciated.
I am using Ubuntu 20.04.1 LTS and g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0 .
I tried to reproduce the problem here, and this error message goes away if you put a space between the -L flag and the tilde character.
The reason is: if there is no space between -L and ~, the tilde character cannot be expanded to the home directory.

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.

How to statically link a Chicken Scheme program that uses extensions?

I have need to compile and statically link a Chicken program. I expect to use many extensions, most notably http-client.
I can compile the source with the following command:
csc -compile-syntax -static linux-setup.scm
or
csc -R http-client -compile-syntax -static linux-setup.scm
But when I run it, I get the following error:
Error: (require) cannot load extension: http-client
Call history:
##sys#require <--
I have also tried (declare (uses http-client)) in the source, with no success:
linux-setup.o: In function `f_369':
/mnt/data/Documents/Programming/chicken-scheme/linux-setup/linux-setup.c:219:
undefined reference to `C_http_2dclient_toplevel'
collect2: error: ld returned 1 exit status
Error: shell command terminated with non-zero exit status 256: 'gcc' 'linux-setup.o'
-o 'linux-setup' -L"/usr/lib" -Wl,-R"/usr/lib" -static '/usr/lib/libchicken.a' -lm -ldl
Static linking is something I need. This is not an XY problem. I need my executables to run on a freshly-installed Linux system with no dependancies. This is the primary reason I switched from Common Lisp to Scheme in the first place.
What am I doing wrong, please?
Assuming your program is in a-program.scm file:
csc -deploy a-program.scm
cd a-program/
chicken-install -deploy -p $PWD http-client
...et voilĂ !
edit: turns out that the proper answer to the problem posted is solved in this document: http://www.foldling.org/scheme.html#compiling-statically-linked-chicken-scheme-programs-with-extensions

GCC how to add before the default linker search path by default? LIBRARY_PATH not working

I'm trying to figure out how to set some environment variable which would make g++ to link to correct versions of the libraries.
I have some old boost libraries in /usr/lib64 (linking against these will fail) and new libraries in /v/users/regel/lib. So the linker should link against the new libraries.
Command:
$ g++ test.cpp -lboost_system -L/v/users/regel/lib
links the program correctly. However, I wish to set this as the number 1 search directory for the linker so that I don't have to specify '-L' every time I link.
The following environment variables do not seem to do the trick:
$ LIBRARY_PATH=/v/users/regel/lib g++ test.cpp -lboost_system
/tmp/regel/cc4SmBtI.o: In function `main':
test.cpp:(.text+0x5): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status
and
$ LD_LIBRARY_PATH=/v/users/regel/lib:$LD_LIBRARY_PATH g++ test.cpp -lboost_system
/tmp/regel/ccUreBZy.o: In function `main':
test.cpp:(.text+0x5): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status
Despite reading numerous articles and posts on similar subjects, I have not found a solution yet.
As the GCC manual says, LIBRARY_PATH is the correct environment variable to add directories to the library search path.
If you add -v to the g++ command you should see the LIBRARY_PATH that it uses, and you should see it includes the directory you have specified, and that it gets added to the collect2 command as -L, but you will see it gets added after the standard directories such as -L/usr/lib etc.
I don't know any way to make the directories in LIBRARY_PATH come first, I think you have to use -L for that.
Try specifying the library path in a .conf file in /etc/ld.so.conf.d/
The linker looks at paths specified in files in /etc/ld.so.conf.d/ while linking.
Make sure you run 'ldconfig' once you create the file, that will force it to update its cache.

Resources