Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linking Fortran 90 code with 'gfortran' compiler on linux ubuntu 11.10

Status
Not open for further replies.

dibas

Programmer
Nov 23, 2011
24
ZA
Hi. Please pardon me if this is not a very relevant topic here. I just thought someone might help out.

I have a slight problem linking my f90 code now on my linux ubuntu 11.10 system. Compiling procedes well. However, when linking, an error message is
generated. This started after I installed some updates on my system.

Example: I have the two source files
subroutine : Jacobian.f90
program(main) : test_Jacobian.f90

compiling command/sequence : gfortran -c Jacobian.f90
gfortran -c test_Jacobian.f90

linking : gfortran -o jacobian test_Jacobian.o Jacobian.o

But I find that when combining both compilation and linking in
gfortran -o jacobian test_Jacobian.f90 Jacobian.f90
produces the executable ("jacobian" in his case here). However,
this is not my preferred option of coming up with the executable
because I use a makefile which carries out the compiling and
linking explicitly. So this might limit me somehow.
 
Sorry but without the exact error message I cannot help you very much. My crystal ball seems on strike...

In addition, could you provide your makefile please ?

François Jacq
 
Thanks for your attention. This looks weird because the makefile works fine
with the same compiling and linking sequences as I had on the command-line.
I have attached the makefile though.

Makefile:
Code:
# Simple makefile for creating executble from 'jacobian' 
# from f90 test_Jacobian.f90 and Jacobian.f90

#######################################
### Declarations/compiler & linker ####
#######################################


# The compiler (gfortran or f95)
F90C = gfortran 

# Debugging flags

#F90FLAGS = -O3
F90FLAGS = -g -O3

#LNK = gfortran 
LNK= gfortran 
# Library flags (leave blank)
LDFLAGS = 
LIBS =

#####################################
####### Define some macros ##########
#####################################

OBJ  = test_Jacobian.o Jacobian.o
EXE  = jacobian

#####################################
###### Define dependencies ##########
#####################################

test_Jacobian.o: test_Jacobian.f90
	$(F90C) $(F90FLAGS) -c test_Jacobian.f90

Jacobian.o: Jacobian.f90
	$(F90C) $(F90FLAGS) -c Jacobian.f90

##############################################
## Describing executable target(s): Linking ##
##############################################

${EXE} : $(OBJ)
	$(LNK) $(F90FLAGS) -o $(EXE) $(LDFLAGS)$(OBJ)$(LIBS)

############################################
### Clean up object and executable files ###
############################################

# Utility targets
.PHONY: clean veryclean


clean:
	rm -f *.o 

veryclean: clean
	rm -f *~ $(EXE)

run: $(EXE)
	./$(EXE)
 
Sorry again but could you please give us the correct error message ?

We are not magician ! Without clear diagnostics, we cannot provide a good advice. You just said in your first message that something wrong occurred at the link stage. This is not enough !

About the makefile, I don't see any obvious mistake. Just two remarks :

- You should insert spaces in $(LDFLAGS)$(OBJ)$(LIBS) (to write $(LDFLAGS) $(OBJ) $(LIBS)) even if this has no consequence here because LDFLAGS and LIBS are empty variables.

- you should add a dependency :

test_Jacobian.o: test_Jacobian.f90 Jacobian.o
$(F90C) $(F90FLAGS) -c test_Jacobian.f90

Here agin, it does not matter because the procedure Jacobian is external. But if you include it in a module (the advised way), then Jacobian.f90 must be compiled BEFORE test_Jacobian.f90.


François Jacq
 
This was the error message:
Code:
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

I'm sorry for not including it in my initial post. I only wrote it down somewhere. I believe it was just a system glitch because after restarting my pc several times, the error message could not be reproduced any more. Up to now! Thanks for the suggested improvements for my makefile.
 
you probably don't have main program in your code...
 
That's the point. But I had one. Eventually, I have somehow managed to reproduce the error message. I noticed that if at some point you mess up slightly in the linking sequence e.g. using the " -o " option without supplying the name of the output executable, the main program object file gets deleted. So, on the next correct try of linking this will be missing, and hence the error ( ..." undefined reference to `main'... "

Many thanks for your time and strong will to help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top