2013-03-30 20 views
9

私はUbuntuでコンパイルするために手を使っていました。 だから私はこのくらいの小さなプログラム型付け:C++プログラムをコンパイルできません

#include <iostream> 
using namespace std; 
int main(){ 
int cases; 
cin>>cases; 
return 0; 
} 

やエラーの多くを与えるこの事を:

[email protected]:~/cpp$ gcc -Wall -W -Werror 2.cpp -o 1 
/tmp/ccU4nAIg.o: In function `main': 
2.cpp:(.text+0x10): undefined reference to `std::cin' 
2.cpp:(.text+0x15): undefined reference to `std::istream::operator>>(int&)' 
/tmp/ccU4nAIg.o: In function `__static_initialization_and_destruction_0(int, int)': 
2.cpp:(.text+0x4d): undefined reference to `std::ios_base::Init::Init()' 
2.cpp:(.text+0x5c): undefined reference to `std::ios_base::Init::~Init()' 
/tmp/ccU4nAIg.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0' 
collect2: error: ld returned 1 exit status 

私は「C」で簡単に行うことができます。しかし、私の間違いは "C++"ですか?

+4

C++コードをコンパイルするには、C++コンパイラを使用する必要があります..... – talonmies

答えて

21

gccの代わりにg++を使用して、C++プログラムをビルドします。

gccながらそれはあなたのプログラムが必要とされているC++ライブラリに反しないリンクを行い、デフォルトでは、 C++をコンパイルする方法を知っています。マニュアルから

:C++、代わりにgccのコールg++をコンパイルする

 
Compiling C++ Programs 

     C++ source files conventionally use one of the suffixes .C, .cc, .cpp, .CPP, 
     .c++, .cp, or .cxx; C++ header files often use .hh or .H; and preprocessed C++ 
     files use the suffix .ii. GCC recognizes files with these names and compiles 
     them as C++ programs even if you call the compiler the same way as for 
     compiling C programs (usually with the name gcc). 

     However, the use of gcc does not add the C++ library. g++ is a program that 
     calls GCC and treats .c, .h and .i files as C++ source files instead of C 
     source files unless -x is used, and automatically specifies linking against the 
     C++ library. This program is also useful when precompiling a C header file 
     with a .h extension for use in C++ compilations. On many systems, g++ is also 
     installed with the name c++. 
関連する問題