Add c-language function in the ROMS compiling process

Discussion on computers, ROMS installation and compiling

Moderators: arango, robertson

Post Reply
Message
Author
s.celestino

Add c-language function in the ROMS compiling process

#1 Unread post by s.celestino »

Hello,

I want to add a C function (to compute the resident set size memory used by ROMS) in the compiling process. I need to compile the object C file together the object fortran files.

This part in the makefile (line 185)?

Code: Select all

#--------------------------------------------------------------------------
#  Set Pattern rules.
#--------------------------------------------------------------------------

%.o: %.F

%.o: %.f90
    cd $(SCRATCH_DIR); $(FC) -c $(FFLAGS) $(notdir $<)

%.f90: %.F
    $(CPP) $(CPPFLAGS) $(MY_CPP_FLAGS) $< > $*.f90
    $(CLEAN) $*.f90

User avatar
kate
Posts: 4091
Joined: Wed Jul 02, 2003 5:29 pm
Location: CFOS/UAF, USA

Re: Add c-language function in the ROMS compiling process

#2 Unread post by kate »

I needed to add a C function for the waves-in-a-circle problem. I believe the chunk of makefile you show does nothing at all. It's all in the functions. I added these to https://github.com/kshedstrom/roms/blob ... n/makefile:

Code: Select all

  c_sources  :=

Code: Select all

# $(call source-to-object, source-file-list)
c-source-to-object = $(call source-dir-to-binary-dir,       \
                     $(subst .c,.o,$(filter %.c,$1))        \
                     $(subst .cc,.o,$(filter %.cc,$1)))

# $(call make-c-library, library-name, source-file-list)
define make-c-library
   libraries += $(SCRATCH_DIR)/$1
   c_sources += $2

   $(SCRATCH_DIR)/$1: $(call source-dir-to-binary-dir,    \
                      $(subst .c,.o,$(filter %.c,$2))     \
                      $(subst .cc,.o,$(filter %.cc,$2)))
        $(AR) $(ARFLAGS) $$@ $$^
        $(RANLIB) $$@
endef

# $(call one-c-compile-rule, binary-file, source-file)
define one-c-compile-rule
  $1: $2
        cd $$(SCRATCH_DIR); $$(CXX) -c $$(CXXFLAGS) $$<

endef
Then in your compilers file you also need:

Code: Select all

               CC := gcc
              CXX := g++
           CFLAGS :=
         CXXFLAGS :=

Code: Select all

ifdef USE_DEBUG
           FFLAGS += -g -fbounds-check
           CFLAGS += -g
         CXXFLAGS += -g
else
           FFLAGS += -O3 -ffast-math
           CFLAGS += -O3
         CXXFLAGS += -O3
endif
Finally, you need to invoke the compiling of your C code. I forget how we did that before. It was for the Hong Kong workshop, files which are no longer being served by ARSC with the demise of ARSC. I can poke around later for them.

User avatar
kate
Posts: 4091
Joined: Wed Jul 02, 2003 5:29 pm
Location: CFOS/UAF, USA

Re: Add c-language function in the ROMS compiling process

#3 Unread post by kate »

Hah, I didn't do so much work back in 2011. I just needed one new C routine, so I did this in Master/Module.mk:

Code: Select all

diff --git a/Master/Module.mk b/Master/Module.mk
index 4ff5e1b..a9c943d 100644
--- a/Master/Module.mk
+++ b/Master/Module.mk
@@ -18,8 +18,8 @@ ifdef LD_WINDOWS
 $(BIN):        $(libraries) $(local_objs)
        $(LD) $(FFLAGS) $(local_objs) -o $@ $(libraries) $(LIBS_WIN32) $(LDFLAGS)
 else
-$(BIN):        $(libraries) $(local_objs)
-       $(LD) $(FFLAGS) $(LDFLAGS) $(local_objs) -o $@ $(libraries) $(LIBS)
+$(BIN):        $(libraries) $(local_objs) $(SCRATCH_DIR)/bessi.o
+       $(LD) $(FFLAGS) $(LDFLAGS) $(local_objs) $(SCRATCH_DIR)/bessi.o -o $@ $(libraries) $(LIBS)
 endif
 
 $(eval $(compile-rules))
In other words, I added $(SCRATCH_DIR)/bessi.o to the dependencies and to the link list. Then in the makefile, I added these lines below the "all:" target, directions for creating $(SCRATCH_DIR)/bessi.o:

Code: Select all

$(SCRATCH_DIR)/bessi.o: $(MY_ANALYTICAL_DIR)/bessi.c
[TAB]gcc -c $< -o $@
Note that you *must* have a tab before "gcc".

s.celestino

Re: Add c-language function in the ROMS compiling process

#4 Unread post by s.celestino »

Wow thanks a lot! I will try it now :D

s.celestino

Re: Add c-language function in the ROMS compiling process

#5 Unread post by s.celestino »

Thanks kate,

I updated all the code you cited.

and the makefile:

Code: Select all

#--------------------------------------------------------------------------
#  Build target directories.
#--------------------------------------------------------------------------

.PHONY: all

all: $(SCRATCH_DIR) $(SCRATCH_DIR)/MakeDepend $(BIN) rm_macros

$(SCRATCH_DIR)/memuse.o: $(MY_ANALYTICAL_DIR)/memuse.c
    gcc -c $< -o $@
but after the compiling process I get this:

Code: Select all

ar: creating /home/ocean/WC13/I4DVAR/Build/libUTIL.a
master.f90:122:25:

    print*, 'Mem Used:: ', memuse(), 'KB'
                         1
Error: Function ‘memuse’ at (1) has no IMPLICIT type
make: *** [/home/ldamore/ocean/WC13/I4DVAR/Build/master.o] Error 1
make: *** Waiting for unfinished jobs....
memuse() is defined in the memuse.c. So I did not compile the C object file together with the other files in fortran. Where am I wrong?

User avatar
kate
Posts: 4091
Joined: Wed Jul 02, 2003 5:29 pm
Location: CFOS/UAF, USA

Re: Add c-language function in the ROMS compiling process

#6 Unread post by kate »

Actually, that's not what it's complaining about. It's complaining that you didn't declare the type of memuse in the Fortran code. Does memuse return an integer or a real? You have to tell the compiler since we have turned on IMPLICIT NONE (we don't want it making assumptions for us). At the top of your code, just add something like:

Code: Select all

real(r8) :: memuse
or

Code: Select all

integer :: memuse

s.celestino

Re: Add c-language function in the ROMS compiling process

#7 Unread post by s.celestino »

kate wrote:Actually, that's not what it's complaining about. It's complaining that you didn't declare the type of memuse in the Fortran code. Does memuse return an integer or a real? You have to tell the compiler since we have turned on IMPLICIT NONE (we don't want it making assumptions for us). At the top of your code, just add something like:

Code: Select all

real(r8) :: memuse
or

Code: Select all

integer :: memuse
Oh yes, you are right. I'm a noob on Fortran :D

Thanks.

Post Reply