C++ 소스를 gcc와 g++로 컴파일 할 때의 차이

송지석의 이미지

C++ 파일을 리눅스에서는 cc 확장자로 사용하잖아요? 그 확장자로 하면 gcc에서 자동으로 c++ 파일로 알고 처리하는 것으로 알고 있는데요.

그런데 아주 간단한 헬로월드 테스트 프로그램이 gcc로는 컴파일이 안되는군요..

[root@sjs root]# cat c++test.cc
#include <string>
#include <iostream>

using namespace std;

int main()
{
        string str("hello world");
        cout << str << endl;
        return 0;
}

이런 코드입니다.

Quote:
[root@sjs root]# g++ c++test.cc

이렇게하면 잘 컴파일 되는데,

Quote:
[root@sjs root]# gcc c++test.cc
/tmp/ccmjz3Ou.o: In function `main':
/tmp/ccmjz3Ou.o(.text+0x1c): undefined reference to `std::allocator<char>::allocator[in-charge]()'
/tmp/ccmjz3Ou.o(.text+0x31): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string[in-charge](char const*, std::allocator<char> const&)'
/tmp/ccmjz3Ou.o(.text+0x48): undefined reference to `std::allocator<char>::~allocator [in-charge]()'
/tmp/ccmjz3Ou.o(.text+0x65): undefined reference to `std::allocator<char>::~allocator [in-charge]()'
/tmp/ccmjz3Ou.o(.text+0x70): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/tmp/ccmjz3Ou.o(.text+0x7c): undefined reference to `std::cout'
/tmp/ccmjz3Ou.o(.text+0x81): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/ccmjz3Ou.o(.text+0x8a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccmjz3Ou.o(.text+0x99): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string [in-charge]()'
/tmp/ccmjz3Ou.o(.text+0xb0): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string [in-charge]()'
/tmp/ccmjz3Ou.o: In function `__static_initialization_and_destruction_0(int, int)':
/tmp/ccmjz3Ou.o(.text+0xee): undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccmjz3Ou.o: In function `__tcf_0':
/tmp/ccmjz3Ou.o(.text+0x11d): undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccmjz3Ou.o(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

이렇게 gcc로 하면 링커 에러가 난답니다. 음..

그렇다면 gcc로 컴파일 할 때는 링커 옵션으로 뭔가를 줘야만 하는 것인가요? 아님 g++로 컴파일 해야 할까요?

mario의 이미지

gcc - cc1
g++ - cc1plus

의 관계일껍니다.

vigor96의 이미지

송지석의 이미지

아 그렇군요. stdc++ 기억하고 있었는데 왜 전혀 생각이 안난것인지..

답변 감사합니다.

익명 사용자의 이미지

이기야

익명 사용자의 이미지

14년 전 글에 답글 달지 말아라 이기야

Anti-Lock의 이미지

컴파일러와 링커의 차이점을 알고 계십니까?

$ cat > c++test.cc
#include <string>
#include <iostream>
 
using namespace std;
 
int main()
{
        string str("hello world");
        cout << str << endl;
        return 0;
}
 
$ gcc -c c++test.cc
$ g++ c++test.o
$ ./a.exe
hello world

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.