Skip to content

Commit

Permalink
Compile SOILWAT2 library with c++ when used for linking to unit tests
Browse files Browse the repository at this point in the history
- close #82

- this required addressing following "error: cannot increment
expression of enum type 'Months'"
- use defined values for each month instead of `typdef enum` which
doesn't allow mathematical operations (easily); then define variables
as 'TimeInt' instead of 'Months'
  • Loading branch information
dschlaep committed Nov 22, 2017
1 parent 6d4a9e0 commit 83a6429
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion SW_VegProd.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void SW_VPD_read(void) {
/* =================================================== */
SW_VEGPROD *v = &SW_VegProd;
FILE *f;
Months mon = Jan;
TimeInt mon = Jan;
int x, lineno = 0;
const int line_help = 29;
RealF help_grass, help_shrub, help_tree, help_forb, help_bareGround, litt, biom, pctl, laic;
Expand Down
2 changes: 1 addition & 1 deletion Times.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ TimeInt Time_lastDOY(void) {
return cum_monthdays[Dec];
}

TimeInt Time_days_in_month(Months month) {
TimeInt Time_days_in_month(TimeInt month) {
/* =================================================== */

return days_in_month[month];
Expand Down
21 changes: 17 additions & 4 deletions Times.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,22 @@
#define MAX_WEEKS 53
#define MAX_DAYS 366

typedef enum {
Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, NoMonth
} Months; /* note base0 */
/* constants for each month; this was previously a typedef enum.
* Note: this has to be base0 and continuous. */
#define Jan 0
#define Feb 1
#define Mar 2
#define Apr 3
#define May 4
#define Jun 5
#define Jul 6
#define Aug 7
#define Sep 8
#define Oct 9
#define Nov 10
#define Dec 11
#define NoMonth 12


typedef unsigned int TimeInt;

Expand All @@ -74,7 +87,7 @@ void Time_set_mday(const TimeInt day);
void Time_set_month(const TimeInt mon);
time_t Time_timestamp(void);
time_t Time_timestamp_now(void);
TimeInt Time_days_in_month(Months month);
TimeInt Time_days_in_month(TimeInt month);
TimeInt Time_lastDOY(void);

char *Time_printtime(void);
Expand Down
16 changes: 11 additions & 5 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ CFLAGS = -O3 -Wall -Wextra -pedantic -std=c11
CXXFLAGS = -Wall -Wextra -std=gnu++11 # gnu++11 required for googletest on Windows/cygwin
LDFLAGS = -L.
LDLIBS = -l$(target) -lm # order of libraries is important for GNU gcc (libSOILWAT2 depends on libm)
gtest_ldlibs = $(LDLIBS) -l$(gtest)

sources = SW_Main_lib.c SW_VegEstab.c SW_Control.c generic.c \
rands.c Times.c mymemory.c filefuncs.c \
Expand All @@ -45,6 +44,7 @@ Rpkg_objects = $(objects) SW_R_lib.o SW_R_init.o
target = SOILWAT2
bin_test = sw_test
lib_target = lib$(target).a
lib_target++ = lib$(target)++.a
SHLIB = r$(target)$(SHLIB_EXT)

gtest = gtest
Expand All @@ -53,7 +53,7 @@ GTEST_DIR = googletest/googletest
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
$(GTEST_DIR)/include/gtest/internal/*.h

gtest_LDLIBS = -l$(gtest) -l$(target)++ -lm


all: $(SHLIB)
Expand All @@ -69,6 +69,12 @@ $(lib_target) :
ar -rcsu $(lib_target) $(objects)
@rm -f $(objects)

$(lib_target++) :
$(CXX) $(CXXFLAGS) -c $(sources)
ar -rcsu $(lib_target++) $(objects)
@rm -f $(objects)


bin : $(target)

$(target) : $(lib_target)
Expand All @@ -94,9 +100,9 @@ $(lib_gtest) :
-pthread -c ${GTEST_DIR}/src/gtest-all.cc
ar -r $(lib_gtest) gtest-all.o

test : $(lib_gtest) $(lib_target)
test : $(lib_gtest) $(lib_target++)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -isystem ${GTEST_DIR}/include -pthread \
test/*.cc -o $(bin_test) $(gtest_ldlibs)
test/*.cc -o $(bin_test) $(gtest_LDLIBS)

test_run :
./$(bin_test)
Expand All @@ -108,7 +114,7 @@ clean :

.PHONY : clean2
clean2 :
@rm -f $(target) $(SHLIB) $(lib_target)
@rm -f $(target) $(SHLIB) $(lib_target) $(lib_target++)
@rm -f testing/$(target)
@rm -f testing/Output/*

Expand Down
4 changes: 0 additions & 4 deletions test/test_rands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@
#include <time.h>
#include <unistd.h>
#include "../generic.h"
#include "../generic.c"
#include "../myMemory.h"
#include "../mymemory.c"
#include "../filefuncs.h"
#include "../filefuncs.c"
#include "../rands.h"
#include "../rands.c"


namespace {
Expand Down

0 comments on commit 83a6429

Please sign in to comment.