我使用了新的 C++11“枚举类”类型,并在使用 g++ 时观察到了“未定义的引用”问题。clang++ 不会发生这个问题。我不知道我做错了什么还是 g++ 错误。
重现问题的代码是:(4 个文件:enum.hpp、enum.cpp、main.cpp 和 Makefile)
// file: enum.hpp
enum class MyEnum {
val_1,
val_2
};
template<typename T>
struct Foo
{
static const MyEnum value = MyEnum::val_1;
};
template<>
struct Foo<int>
{
static const MyEnum value = MyEnum::val_2;
};
template<typename T>
void foo(const T&);
和...
// file: enum.cpp
#include <iostream>
#include "enum.hpp"
template<typename T>
void foo(const T&)
{
switch(Foo<T>::value) {
case MyEnum::val_1:
std::cout << "\n enum is val_1"; break;
case MyEnum::val_2:
std::cout << "\n enum is val_2"; break;
default:
std::cout << "\n unknown enum"; break;
}
}
// Here we force instantation, thus everything should be OK!?!
//
template void foo<int>(const int&);
template void foo<double>(const double&);
和...
// file: main.cpp
#include "enum.hpp"
int
main()
{
foo(2.);
foo(2);
}
和Makefile...
COMPILER = g++ # does no work
#COMPILER = clang++ # Ok
all: main
main : main.cpp enum.cpp
$(COMPILER) -std=c++11 -c enum.cpp -o enum.o
$(COMPILER) -std=c++11 main.cpp enum.o -o main
当我使用 g++ 时,我得到:
make -k
g++ -std=c++11 -c enum.cpp -o enum.o
g++ -std=c++11 main.cpp enum.o -o main
enum.o: In function `void foo<int>(int const&)':
enum.cpp:(.text._Z3fooIiEvRKT_[_Z3fooIiEvRKT_]+0xe): undefined reference to `Foo<int>::value'
enum.o: In function `void foo<double>(double const&)':
enum.cpp:(.text._Z3fooIdEvRKT_[_Z3fooIdEvRKT_]+0xe): undefined reference to `Foo<double>::value'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
make: Target `all' not remade because of errors.
但是使用 clang++ 一切都很好(没有编译错误)。
欢迎任何解释,因为我在这里迷路了。
谢谢!:)
关于我的配置:
g++ --version
g++ (Debian 4.7.2-5) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
clang++ --version
Debian clang version 3.0-6 (tags/RELEASE_30/final) (based on LLVM 3.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
uname -a
Linux IS006139 3.2.0-4-amd64 #1 SMP Debian 3.2.35-2 x86_64 GNU/Linux