gcc 와 g++ 를 사용함에 따라 결과가 다릅니다.
아래 4개의 file 이 있습니다.
--------------< a.c >----------------------------
#include "Print.h"
void main()
{
int a;
a = 1;
Print( "hahaha %d\n", a );
}
-------------< Print.h >-----------------------------
extern void myPrint( unsigned long level, const char * format, ... );
#define Print( x, ... ) myPrint( 1, x , ##__VA_ARGS__ )
--------------< myPrint.c>----------------------------
#include "myPrint.h"
#include
#include
#include
#include
void myPrint(unsigned long level, const char * format, ... )
{
va_list argptr;
va_start( argptr, format );
vprintf( format, argptr );
va_end( argptr);
}
-----------< myPrint.h >-------------------------------
void myPrint( unsigned long level, const char * format, ... );
------------------------------------------
아래와 같이 빌드 하면 gcc 로 하면 error 가 나고 g++ 로 하면 error 가 나지 않네요.
이유가 뭔가요? gcc 내부적으로 확장자를 보고 g++ 나 gcc 를 호출 된다고 하던 것 같은데..
$ gcc -c myPrint.cpp
$ gcc -c a.c
$ g++ a.o myPrint.o -o test --> 정상
$ gcc -c myPrint.cpp
$ gcc -c a.c
$ gcc a.o myPrint.o -o test ---> 에러
myPrint.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
google undefined reference to `__gxx_personality_v0' 결과
http://bytes.com/topic/c/answers/433942-undefined-reference-__gxx_personality_v0
"mlimber"란 분이 설명을 잘해놨네요.
1. cpp 확장자를 보고 c++ object myPrint.o로 compile (default로 exception handling과 같은 stdc++ runtime lib 호출 부분 추가)
2. c 확장자를 보고 c object a.o로 compile
2. gcc가 a.o myPrint.o 둘다 c object로 간주하고 기본 c runtime module link
3. 따라서 std c++ runtime library 참조 불가
4. 결론 gcc가 유추하는 것도 한계가 있으니, c++ library나 object를 link한다면 gcc가 아니라 g++을 쓸 것
ps)
test는 안해봤습니다만, 굳이 gcc를 사용해야 한다면 아래처럼 -lstdc++를 추가하면 된다고 들은 기억이 있군요.
된다하더라도, 별로 권장하고 싶지는 않습니다.
댓글 달기