我正在尝试使用gsl库进行一些随机数生成。我有以下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
和以下头文件
#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
运行makefile时遇到的问题是
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'
以及程序中每次调用gsl函数时出现的类似错误。根据我的阅读,未定义的引用错误通常是由链接到必要文件时的错误引起的。映射gsl(我在makefile中链接到的)与makefile在同一个映射中,所以这应该没问题呢?我不知道我犯了什么错误?提前感谢您的帮助。
编辑:我不知道这是否有帮助,但是替换了makefile中的LDFLAGS和INCLDIRS行
LDFLAGS = -L/usr/local/lib -lgsl -lgslcblas -lm
INCLDIRS = -I/usr/local/include
也会出现相同的错误,即使usr / local目录中也存在gsl库。
答案 0 :(得分:1)
你的问题很常见。这样:
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(INCLDIRS)
应该是这样的:
$(CC) $(CFLAGS) $(INCLDIRS) -o $@ $^ $(LDFLAGS)
链接库(-l
标志)应该在命令行中 源文件(在您的情况下为$^
)后。