好吧,我从昨天开始就一直在为这个奇怪的错误而苦苦挣扎,所以我想是时候问问社区了……
我目前正在使用 Objective-C++,并且我在头文件中有一个纯 C++ 类声明,如下所示:
#ifndef __MATRIX_H__
#define __MATRIX_H__
#define USE_NEON_UPSAMPLING2X true
#define USE_NEON_THRESHOLD true
typedef float OCRfloat;
template<class T = OCRfloat>
class Matrix {
public:
...
Matrix threshold(T thresholdValue) const;
...
Matrix upsample2x() const;
...
};
#ifdef TARGET_OS_IPHONE
#if USE_NEON_UPSAMPLING2X
template<> Matrix<float> Matrix<float>::upsample2x() const;
#endif
#if USE_NEON_THRESHOLD
template<> Matrix<float> Matrix<float>::threshold(float thresholdValue) const;
#endif
#endif
#include "Matrix.cpp"
#endif
它是模板类,具有基本的矩阵运算,但是,我想通过T=float上的模板专业化来优化一些瓶颈。我有包含的 Matrix.cpp 文件,如下所示:
#include <iostream>
#include <cmath>
#if defined TARGET_OS_IPHONE
#include <Accelerate/Accelerate.h>
#endif
...
template<class T> Matrix<T> Matrix<T>::threshold(T thresholdValue) const {
... // general naive algorithm
}
template<class T> Matrix<T> Matrix<T>::upsample2x() const{
... // general naive algorithm
}
#ifdef TARGET_OS_IPHONE
#if USE_NEON_UPSAMPLING2X
template<> Matrix<float> Matrix<float>::upsample2x() const{
... // specialized for ARM NEON float32_t
}
#endif
#if USE_NEON_THRESHOLD
template<> Matrix<float> Matrix<float>::threshold(float thresholdValue)const{
... // specialized for ARM NEON float32_t
}
#endif
问题如下:如果我定义 USE_NEON_UPSAMPLING2X=false 和 USE_NEON_THRESHOLD=true,一切正常 - 应用程序已构建,并且像魅力一样工作。但是,如果我设置 USE_NEON_UPSAMPLING2X=true,链接器会中断以下内容:
duplicate symbol __ZNK6MatrixIfE10upsample2xEv in:
/.../OCRDemo-eggnlcolcwqycjagwvwddpfwgzlb/Build/Intermediates/OCRDemo.build/Release-iphoneos/OCRDemo.build/Objects-normal/armv7/A.o
/.../OCRDemo-eggnlcolcwqycjagwvwddpfwgzlb/Build/Intermediates/OCRDemo.build/Release-iphoneos/OCRDemo.build/Objects-normal/armv7/B.o
duplicate symbol __ZNK6MatrixIfE10upsample2xEv in:
/.../OCRDemo-eggnlcolcwqycjagwvwddpfwgzlb/Build/Intermediates/OCRDemo.build/Release-iphoneos/OCRDemo.build/Objects-normal/armv7/A.o
/.../OCRDemo-eggnlcolcwqycjagwvwddpfwgzlb/Build/Intermediates/OCRDemo.build/Release-iphoneos/OCRDemo.build/Objects-normal/armv7/C.o
duplicate symbol __ZNK6MatrixIfE10upsample2xEv in:
/.../OCRDemo-eggnlcolcwqycjagwvwddpfwgzlb/Build/Intermediates/OCRDemo.build/Release-iphoneos/OCRDemo.build/Objects-normal/armv7/A.o
/.../OCRDemo-eggnlcolcwqycjagwvwddpfwgzlb/Build/Intermediates/OCRDemo.build/Release-iphoneos/OCRDemo.build/Objects-normal/armv7/D.o
有趣的是,这两种方法都在这些文件中使用,但是链接器只抱怨 upsample2x ......从句法的角度来看,两者之间的唯一区别是参数的数量:阈值的参数为 T,而 upsample2x 不需要 - 除此之外,两者都定义为 const,都返回矩阵等。
所以我的问题是:是什么导致了这个神秘的错误,我该如何解决?