dポイントプレゼントキャンペーン実施中!

こんにちは。

C++プログラムの実行時間計測リスト

#include<iostream.h>
#include<time.h>
int main(void){
clock_t start_time=clock();
int i;

//ココから
//計測したい処理を書く
//ココまで

cerr<<clock()-start_time<<"マイクロ秒"<<endl;
return 0;
}


をRedHat9上でコンパイルしようとすると

$gcc -o clock clock.cc
/usr/include/c++/3.2.2/backward/iostream.h:31 から include されたファイル中,
clock.cc:1 から:
/usr/include/c++/3.2.2/backward/backward_warning.h:32:2: 警告: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
/tmp/ccoZe32a.o(.text+0x1c): In function `main':
: 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/ccoZe32a.o(.text+0x3b): In function `main':
: undefined reference to `std::cerr'
/tmp/ccoZe32a.o(.text+0x40): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(long)'

collect2: ld はステータス 1 で終了しました

となってしまいます。これはどうすればコンパイルできるようになるのでしょうか?

A 回答 (1件)

大きな間違いが2つあります。



1つは、<iostream.h>を使用している点です。前半の警告メッセージはこれが原因です。
<iostream.h>ではなく、<iostream>を使用しましょう。その場合、std名前空間内でcerr等の識別子は定義されるので、その点についても配慮してください。

もう1つは、コンパイルにg++ではなく、gccを使用している点です。
g++を使用しなければ、C++用のライブラリがリンクされません。これが後半部分のcollect2によるエラーメッセージの原因です。
    • good
    • 0
この回答へのお礼

大変有難うございます。

一挙に解決しちゃいました。m(_ _)m

お礼日時:2005/03/16 15:43

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!