gcc 와 g++ 를 사용함에 따라 결과가 다릅니다.

jmbae940의 이미지

아래 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

bootmeta의 이미지

http://bytes.com/topic/c/answers/433942-undefined-reference-__gxx_personality_v0

"mlimber"란 분이 설명을 잘해놨네요.

$ gcc -c myPrint.cpp
$ gcc -c a.c

1. cpp 확장자를 보고 c++ object myPrint.o로 compile (default로 exception handling과 같은 stdc++ runtime lib 호출 부분 추가)
2. c 확장자를 보고 c object a.o로 compile

$ gcc a.o myPrint.o -o test 

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++를 추가하면 된다고 들은 기억이 있군요.
된다하더라도, 별로 권장하고 싶지는 않습니다.

$ gcc a.o myPrint.o -o test -lstdc++ 

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.