2010-03-04 152 views
2

我知道C++代碼應該由g ++編譯和鏈接,而不是gcc。 但是爲什麼gcc仍然可以編譯C++源代碼,儘管源代碼中有很多C++關鍵字。爲什麼gcc可以編譯C++代碼但不能鏈接?

順便說一下,我發現我甚至可以通過gcc與所有C++代碼構建一個共享庫。爲什麼?

回答

9

g ++是gcc,它只是自動鏈接到標準的C++庫。

如果你的G ++代碼依賴於標準庫(東西在std命名空間),你可以

  1. 使用g ++命令,它的所有自動
  2. 使用GCC命令,並指定C++標準(-lstdc++
2

您可以鏈接-lstdC++。

+0

不起作用。當我鏈接 – solotim 2010-03-04 07:07:00

+0

@ solotim時,錯誤會發生,它確實有效。也許你錯了什麼?或者,也許你應該編輯你的問題,並指定錯誤。 – 2010-03-04 07:12:26

4

從GCC手冊頁:

For any given input file, the file name suffix determines what kind of 
    compilation is done: 

    file.c 
     C source code which must be preprocessed. 

    . 
    . 
    . 

    file.h 
     C, C++, Objective-C or Objective-C++ header file to be turned into 
     a precompiled header. 

    file.cc 
    file.cp 
    file.cxx 
    file.cpp 
    file.CPP 
    file.c++ 
    file.C 
     C++ source code which must be preprocessed. Note that in .cxx, the 
     last two letters must both be literally x. Likewise, .C refers to 
     a literal capital C. 

什麼也沒有做自動鏈接到C++標準庫。在那時使用g++是最簡單的。

+1

如果您要降級,請發表評論。 – Xorlev 2010-03-04 07:11:13

相關問題