C ++链接器问题:未定义的引用

时间:2014-07-12 01:30:54

标签: c++ linker

所以我一直在努力让我的代码在过去几个小时内编译,我无法理解我哪里出错了。

这是我的make文件:

这是输出:

g++ -c LPfilter.cpp -o LPfilter.o -g 
g++ -c EKSmoother.cpp -o EKSmoother.o -g 
g++ -c ECGFitter.cpp -o ECGFitter.o -g 
g++ -c KalmanFiltering.cpp -o KalmanFiltering.o -g 
g++ -c helper.cpp -o helper.o -g 
g++ -c math.cpp -o math.o -g 
g++ -o kalmanFiltering LPfilter.o EKSmoother.o ECGFitter.o KalmanFiltering.o helper.o math.o -lm -larmadillo -L. -lmpfit

EKSmoother.o: In function `EKSmoother':
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/EKSmoother.cpp:30: undefined reference to `double Math::calculateMean<double>(std::vector<double, std::allocator<double> >)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/EKSmoother.cpp:31: undefined reference to `double Math::sd2<double>(std::vector<double, std::allocator<double> >)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/EKSmoother.cpp:39: undefined reference to `double Math::findAbsMax<double>(std::vector<double, std::allocator<double> >)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/EKSmoother.cpp:51: undefined reference to `double Math::calculateMean<double>(std::vector<double, std::allocator<double> >)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/EKSmoother.cpp:58: undefined reference to `double Math::calculateMean<double>(std::vector<double, std::allocator<double> >)'
ECGFitter.o: In function `ECGFitter':
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:19: undefined reference to `double Math::calculateMeanSqrt<double>(std::vector<double, std::allocator<double> >)'
ECGFitter.o: In function `SignalFitter::ECGFitter::peakDetection(double)':
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:60: undefined reference to `double Math::getMaxStd<double>(std::vector<double, std::allocator<double> >)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:60: undefined reference to `double Math::getMinStd<double>(std::vector<double, std::allocator<double> >)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:83: undefined reference to `bool Math::compare<double>(double, double)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:106: undefined reference to `bool Math::compare<double>(double, double)'
ECGFitter.o: In function `SignalFitter::ECGFitter::phaseCalculation()':
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:140: undefined reference to `int Math::sumStd<int>(std::vector<int, std::allocator<int> >)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:173: undefined reference to `bool Math::GT<double>(double, double)'
ECGFitter.o: In function `SignalFitter::ECGFitter::meanECGExtraction(bool)':
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:195: undefined reference to `bool Math::GTE<double>(double, double)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:195: undefined reference to `bool Math::LT<double>(double, double)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:204: undefined reference to `double Math::calculateMean<double>(std::vector<double, std::allocator<double> >)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:205: undefined reference to `double Math::sd<double>(std::vector<double, std::allocator<double> >)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:215: undefined reference to `bool Math::GTE<double>(double, double)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:215: undefined reference to `bool Math::LT<double>(double, double)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:226: undefined reference to `double Math::calculateMean<double>(std::vector<double, std::allocator<double> >)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:227: undefined reference to `double Math::calculateMean<double>(std::vector<double, std::allocator<double> >)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:228: undefined reference to `double Math::sd<double>(std::vector<double, std::allocator<double> >)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:264: undefined reference to `double Math::calculateMean<double>(std::vector<double, std::allocator<double> >)'
ECGFitter.o: In function `SignalFitter::ECGFitter::findIndicies(std::vector<double, std::allocator<double> >)':
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/ECGFitter.cpp:355: undefined reference to `bool Math::compare<double>(double, double)'
KalmanFiltering.o: In function `main':
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/KalmanFiltering.cpp:37: undefined reference to `Helper::readDataFile(char const*)'
/home/ryan/Documents/Research_Work/SimpleHealth/KalmanFilter/KalmanFilter_C++/test/cmpfit-1.2/KalmanFiltering.cpp:40: undefined reference to `Helper::readDataFile(char const*)'
collect2: ld returned 1 exit status
make: *** [kalmanFiltering] Error 1

我的make文件是:

CC      = g++
LDFLAGS = -lfltk

CFLAGS = -g $(OPT)
OPT = 

LIBS = -lm -larmadillo -L. -lmpfit
SRCS = LPfilter.cpp EKSmoother.cpp ECGFitter.cpp KalmanFiltering.cpp helper.cpp math.cpp
OBJS =$(SRCS:.cpp=.o)
DEPS = 

MAIN = kalmanFiltering
all: $(MAIN)
    @echo ------ $(MAIN) has been compiled ------

.cpp.o:
    $(CC) -c $< -o $@ $(CFLAGS)

$(MAIN): $(OBJS)
    $(CC) -o $@ $^ $(LIBS)

.PHONY: clean cleanest

clean:
    rm *.o

cleanest: clean
    rm octhecdec

我的math.cpphelper.cpp文件基本上包含ECGFitter.cppECGSmoother.cppKalmanFiltering.cpp中使用的一些函数。我知道您只需要为汇编程序包含.hpp文件来生成.o文件,然后在运行链接器之前需要链接相应源代码的.o文件。我以为我这样做很好,但显然我不是。

感谢您的帮助!

PS:

math.hpp

#ifndef MATH
#define MATH

#include <cmath>
#include <vector>

#define EPSILON 0.0000001
#define templateT template<typename T>
const double PI = 3.141592653589793;

namespace Math {
  templateT T calculateMeanSqrt(const std::vector<T>);
  templateT T calculateMean(const std::vector<T>);
  templateT T sd(std::vector<T>);
  templateT T sd2(std::vector<T>);
  templateT T getMaxStd(std::vector<T>);
  templateT T getMinStd(std::vector<T>);
  templateT T sumStd(std::vector<T>);
  templateT bool compare(T, T);
  templateT bool GTE(T, T);
  templateT bool LTE(T, T);
  templateT bool GT(T, T);
  templateT bool LT(T, T);
  templateT T findAbsMax(std::vector<T>);
}

#endif

helper.hpp:

#ifndef HELPER
#define HELPER

#include <iostream>
#include <vector>
#include <cmath>

#define __DEBUGG
#ifdef __DEBUGG
#include <algorithm> // only used to display vector                                                      
#endif

#define EPSILON 0.0000001

namespace Helper {
  template<typename T> void displayVector(const std::vector<T>);
  std::vector<double> readDataFile(const char*);
}

#endif

0 个答案:

没有答案