프로그램은 G ++로 컴파일되지만 GCC에서 링커 오류로 종료됩니다.

StackOverflow https://stackoverflow.com/questions/1221902

  •  10-07-2019
  •  | 
  •  

문제

나는 노력하고있다 해결책 a 전문 템플릿 클래스에 대한 질문.

이 코드는 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

Trains2.CCP 파일에는 위에서 언급 한 내용이 포함되어 있습니다 해결책 emtpy main () 함수 :

#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 ++ 코드를 빌드하십시오.

다른 팁

자신의 차이를보고 싶다면 -v 플래그를 사용하여 두 버전을 모두 시도하십시오.

$ g++ -v traits2.cpp
$ gcc -v traits2.cpp

여기에는 추가 된 라이브러리를 포함하여 코드 상단 실행 파일의 각 단계가 표시됩니다.

코드는 C ++이므로 GCC, C 컴파일러가 아닌 G ++, C ++ 컴파일러로 컴파일해야합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top