헤더에 제공하려는 정의로 인해 중복 심볼 오류가 발생했습니다. Minimal, Complete, and Verifiable example의 오류는 다음과 같습니다. 헤더 파일과 소스 파일은 아래와 같습니다.헤더의 전문화로 인해 중복 된 기호를 피할 수 있습니까?
$ clang++ main.cpp x.cpp y.cpp -o main.exe 2>&1 | c++filt
duplicate symbol Id<S>::id in:
/tmp/main-3f2415.o
/tmp/long long-d62c28.o
duplicate symbol Id<T>::id in:
/tmp/main-3f2415.o
/tmp/long long-d62c28.o
duplicate symbol Id<S>::id in:
/tmp/main-3f2415.o
/tmp/unsigned long long-bfa6de.o
duplicate symbol Id<T>::id in:
/tmp/main-3f2415.o
/tmp/unsigned long long-bfa6de.o
ld: 4 duplicate symbols for architecture x86_64
여기에 비슷한 질문이 있지만 특수화가 필요하지 않습니다 : Static member initialization in a class template. 이 질문은 전문화되어 있지만 MSVC가 아니라 Clang : How to initialize a static member of a parametrized-template class입니다. 그리고이 질문 상태는 소스 (* .CPP) 파일을 넣어하지만 우리는 방지하기 위해 헤더 파일을 연타 3.8 및 'Id<S>::id' required here, but no definition is available
경고를 목표로하고 있습니다 : Where should the definition of an explicit specialization of a class template be placed in C++?이
GCC, ICC, MSVC, SunCC과 XLC은 OK입니다 초기화. Clang과 LLVM이 나에게 문제를주고있다. Clang과 LLVM은 또한 전문화 된 구체화와 extern
의 인스턴스화에 문제가있어 특별한 종류의 지옥을 소유하고 있습니다.
우리는 C++ 03을 지원하지만 C++ 17이므로이 솔루션을 조심해야합니다. 순진하게도, 번역 유니트를 이스케이프 처리하지 못하도록 이름없는 네임 스페이스에 특수화 초기화를 시도했지만 컴파일 오류가 발생했습니다.
헤더의 템플릿 클래스를 초기화하고 특수화 할 때 중복 심볼 정의가 발생하지 않도록 어떻게해야합니까?
아래는 cat main.cpp a.h s.h s.cpp t.h t.cpp x.cpp y.cpp
인 MCVE입니다. 문제는 전문화 및 초기화를 제공하는 a.h
인 것으로 보입니다. 소스 파일 x.cpp
및 y.cpp
(a.h
포함).
MAIN.CPP
#include "a.h"
#include "s.h"
#include "t.h"
int main(int argc, char* argv[])
{
uint8_t s = Id<S>::id;
uint8_t t = Id<T>::id;
return 0;
}
a.h
#ifndef A_INCLUDED
#define A_INCLUDED
#include <stdint.h>
template <class T> class Id
{
public:
static const uint8_t id;
};
class S;
class T;
template<> const uint8_t Id<S>::id = 0x01;
template<> const uint8_t Id<T>::id = 0x02;
#endif
s.h
,451,515,#ifndef S_INCLUDED
#define S_INCLUDED
class S {
public:
S();
};
#endif
s.cpp
#include "s.h"
S::S() {}
t.h
#ifndef T_INCLUDED
#define T_INCLUDED
class T {
public:
T();
};
#endif
t.cpp
,#include "t.h"
T::T() {}
x.CPP
#include "a.h"
y.cpp
#include "a.h"
'인라인'도움이되지 않습니까? – user0042
@ user0042 - 아니요, 인라인 컴파일에 실패했습니다. 수업과 전문 분야 모두에서 실패합니다. – jww