c – 程序以g编译,但在gcc中以链接器错误退出
发布时间:2020-12-16 10:50:08 所属栏目:百科 来源:网络整理
导读:我正在尝试 solution到 question about specialized template classes. 这个代码编译精细g,但在使用gcc编译时会抛出链接器错误.这些错误的原因是什么? $g++ traits2.cpp$gcc traits2.cpp/tmp/ccI7CNCY.o: In function `__static_initialization_and_destruc
我正在尝试
solution到
question about specialized template classes.
这个代码编译精细g,但在使用gcc编译时会抛出链接器错误.这些错误的原因是什么? $g++ traits2.cpp $gcc traits2.cpp /tmp/ccI7CNCY.o: In function `__static_initialization_and_destruction_0(int,int)': traits2.cpp:(.text+0x36): undefined reference to `std::ios_base::Init::Init()' traits2.cpp:(.text+0x3b): undefined reference to `std::ios_base::Init::~Init()' /tmp/ccI7CNCY.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status traits2.ccp文件包含前面提到的带有emtpy main()函数的solution: #include <iostream> using namespace std; // A default Traits class has no information template<class T> struct Traits { }; // A convenient way to get the Traits of the type of a given value without // having to explicitly write out the type template<typename T> Traits<T> GetTraits(const T&) { return Traits<T>(); } template <int major,int minor> struct A { void f() { cout << major << endl; } }; // Specialisation of the traits for any A<int,int> template<int N1,int N2> struct Traits<A<N1,N2> > { enum { major = N1,minor = N2 }; }; template <> struct A<4,0> { void f() { cout << "Specialized:" << GetTraits(*this).major << endl; } }; int main(int argc,char * argv[] ) { /* A<4,0> p; A<1,2> p2; p.f(); p2.f(); */ return 1; } 解决方法
使用gcc编译时,默认情况下不会链接C库.始终用g构建C代码.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |