Reputation: 129
I am trying to use the gsl library for some random number generation. I have the following makefile
CC = g++
#CFLAGS = -Wall -O2
CFLAGS = -g -Wall
LDFLAGS = -Lgsl/lib -lgsl -lgslcblas -lm
INCLDIRS = -Igsl/include
RAND_TARGET = rand
HM_TARGET = hitormiss
NU_TARGET = nonuniform
MC1D_TARGET = mc1d
RAND_SRCS = rand.cc generators.cc EX_RNGs.cc LC_RNGs.cc util.cc RNG_factory.cc
HM_SRCS = hitormiss.cc generators.cc EX_RNGs.cc LC_RNGs.cc discrep.cc util.cc sphere.cc
NU_SRCS = nonuniform.cc generators.cc LC_RNGs.cc EX_RNGs.cc util.cc
MC1D_SRCS = mc1d.cc generators.cc LC_RNGs.cc
RAND_OBJS = ${RAND_SRCS:.cc=.o}
HM_OBJS = ${HM_SRCS:.cc=.o}
NU_OBJS = ${NU_SRCS:.cc=.o}
MC1D_OBJS = ${MC1D_SRCS:.cc=.o}
CLEANFILES = $(RAND_OBJS) $(RAND_TARGET) \
$(HM_OBJS) $(HM_TARGET) \
$(NU_OBJS) $(NU_TARGET) \
$(MC1D_OBJS) $(MC1D_TARGET) \
core
VERYCLEANFILES = randdata.txt \
randdata.bin \
randdata2d.txt \
accepts.txt \
rejects.txt \
nonuniformdata.txt \
${wildcard hom_*_*d_*.txt} \
${wildcard points_*_*d_*.txt}
all: $(RAND_TARGET) $(HM_TARGET) $(NU_TARGET) $(MC1D_TARGET)
release:
(make CFLAGS="-Wall -O2" all;)
$(RAND_TARGET): $(RAND_OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(INCLDIRS)
$(HM_TARGET): $(HM_OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(INCLDIRS)
$(NU_TARGET): $(NU_OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(INCLDIRS)
$(MC1D_TARGET): $(MC1D_OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(INCLDIRS)
%.o: %.cc
$(CC) $(CFLAGS) $(INCLDIRS) -c $<
clean:
rm -f $(CLEANFILES)
veryclean: clean
rm -f $(VERYCLEANFILES)
LC_RNGs.o: generators.h LC_RNGs.h
EX_RNGs.o: generators.h EX_RNGs.h
discrep.o: generators.h discrep.h
RNG_factory.o: generators.h LC_RNGs.h EX_RNGs.h
util.o: util.h
sphere.o: sphere.h
rand.o: generators.h LC_RNGs.h EX_RNGs.h util.h RNG_factory.h
hitormiss.o: generators.h LC_RNGs.h EX_RNGs.h discrep.h util.h sphere.h
mc1d.o: generators.h LC_RNGs.h
and the following header file
#ifndef _H_EX_RNGS
#define _H_EX_RNGS
#include <cstdlib>
#include <cmath>
#include <iostream>
#include "generators.h"
#include "gsl/include/gsl/gsl_rng.h"
namespace generators {
class GSL_RAND: public RNG {
gsl_rng *r;
public:
GSL_RAND();
virtual unsigned int nextInt();
/** Base deconstructor. */
virtual ~GSL_RAND() {}
virtual void setSeed( unsigned int seed );
virtual unsigned int getMaximum();
};
};
#endif
The problems I get when running the makefile are
EX_RNGs.o: In function `generators::GSL_RAND::GSL_RAND()':
/home/school/rngmc/EX_RNGs.cc:34: undefined reference to `gsl_rng_rand'
/home/school/rngmc/EX_RNGs.cc:34: undefined reference to `gsl_rng_alloc'
and similar errors for every call to a gsl function in the program. From what I have read, undefined reference errors are usually caused by mistakes in linking to the necessary files. The map gsl (which I link to in the makefile) is in the same map as the makefile, so this should be fine right? I do not see where I made a mistake? Thanks in advance for any help.
EDIT: I don't know if this helps, but the replacing the LDFLAGS and INCLDIRS lines in the makefile by
LDFLAGS = -L/usr/local/lib -lgsl -lgslcblas -lm
INCLDIRS = -I/usr/local/include
also gives the same error, even though the gsl library also exist in the usr/local directory.
Upvotes: 0
Views: 1214
Reputation: 10486
Your problem is quite common. This:
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(INCLDIRS)
Should be this:
$(CC) $(CFLAGS) $(INCLDIRS) -o $@ $^ $(LDFLAGS)
Linked libraries (-l
flags) should go after source files (in your case $^
) in command line.
Upvotes: 2