clock関数のコンパイルエラーで

このQ&Aのポイント
  • RedHat9上でコンパイル時にclock関数のコンパイルエラーが発生します。
  • エラーメッセージには、deprecatedなヘッダーが含まれていることが記載されています。
  • また、undefined referenceエラーも発生しています。
回答を見る
  • ベストアンサー

clock関数のコンパイルエラーで

こんにちは。 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 で終了しました となってしまいます。これはどうすればコンパイルできるようになるのでしょうか?

質問者が選んだベストアンサー

  • ベストアンサー
  • jacta
  • ベストアンサー率26% (845/3158)
回答No.1

大きな間違いが2つあります。 1つは、<iostream.h>を使用している点です。前半の警告メッセージはこれが原因です。 <iostream.h>ではなく、<iostream>を使用しましょう。その場合、std名前空間内でcerr等の識別子は定義されるので、その点についても配慮してください。 もう1つは、コンパイルにg++ではなく、gccを使用している点です。 g++を使用しなければ、C++用のライブラリがリンクされません。これが後半部分のcollect2によるエラーメッセージの原因です。

hozumi_
質問者

お礼

大変有難うございます。 一挙に解決しちゃいました。m(_ _)m

関連するQ&A

  • c++のビルドエラーについて

    昨日からc++を学習し始めました。 pythonは2年ほどになりますが、c++は初めてだったので、コンパイルでエラーが起こってしまいわからなくなってしまったので質問させていただきました。 hello worldを出力しようと思いやってみたのですが、どこでエラーが起こっているのかがわかりません。 下記が私の書いたソースコードです。 #include <iostream> using namespace std; int main(){ cout << "HelloWorld." << endl; } エラーメッセージ C:\Users\ユーザー名\AppData\Local\Temp\ccKwbLoc.o: In function `main': C:/Users/ユーザー名/Documents/cpp/hello.cpp:4: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' C:/Users/ユーザー名/Documents/cpp/hello.cpp:4: undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))' C:\Users\ユーザー名\AppData\Local\Temp\ccKwbLoc.o: In function `__tcf_0': C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:74: undefined reference to `std::ios_base::Init::~Init()' C:\Users\ユーザー名\AppData\Local\Temp\ccKwbLoc.o: In function `__static_initialization_and_destruction_0': t::Init()' e to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' C:\Users\ユーザー名\AppData\Local\Temp\ccKwbLoc.o:hello.cpp:(.rdata$.refptr._ZSt4cout[.refptr._ZSt4cout]+0x0): undefined reference to `std::cout' collect2.exe: error: ld returned 1 exit status os:windows11 home 64ビット コンパイラ gcc (x86_64-posix-sjlj-rev0, Built by MinGW-W64 project) 8.1.0 エディタ:vscode よろしくお願いいたします。

  • MingwでC++のソースがコンパイルできない。

    質問があるのですがよろしくお願いしします。 プラットフォームは Windows HOME です。 MinGWでC++のhelloworld.cppという名前のプログラム ------ここから----------- #include <iostream> using namespace std; int main(){ cout <<"Hello World!!\n"; return 0; } -------ここまで----------- を、gcc helloworld.cpp とWindowsのコマンドプロンプトから打ってコンパイルしようとしたのですが、 undefined reference to std string::size() const undefined reference to std string::operator[](using int) const undefinrd reference to std::cout undefinrd reference to std::basic_ostream<char, std::char traits<char> >&, char const*) undefinrd reference to std::ios_base::Init::() undefinrd reference to std::ios_base::~Init::() などのエラーが出て、コンパイルできません・・ hello.cという名前のC言語プログラム --------ここから------ #include<stdio.h> int main(void){ printf("Hello GCC World on Windows!!\n"); } ------ここまで------- は、gcc hello.c で正常にコンパイルでき、生成したexeファイルも正常に動きます。 また、eclipse CDT上からc++プログラムをMinGWでコンパイルすることもできるのですが・・ なぜかWindowsのコマンドプロンプトからc++プログラムをコンパイルしようとするとエラーが出てしまいます。(C言語のプログラムでもエラーが出ることが時々あります。) どなたか詳しい方いらっしゃいましたらご教授頂けると幸いです。 よろしくお願いします。

  • C++でコンパイルするとwarningが出ます

    C++初心者です。 C++でコンパイルすると以下のようなwarningメッセージが出るのですが、どういう意味なんでしょうか?解決策があれば教えてください。 プログラム(sample.cpp) #include <iostream.h> main(){ cout << " sample \n"; } warningメッセージ In file included from /usr/include/c++/3.3.1/backward/iostream.h:31, from sample.cpp:1: /usr/include/c++/3.3.1/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider usin g 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 <s stream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated

  • リンクエラーについて

    visual NET2003を使ってプログラムのコンパイルをしているんですが、 以下のようなエラーが出てしまって詰まっています。 すいませんが助言よろしくお願いします。 twcore.lib(twcore.obj) : error LNK2019: 未解決の外部シンボル "__declspec(dllimport) class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,char const *)" (__imp_??6std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z) が関数 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class TWOBJECT &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVTWOBJECT@@@Z) で参照されました。 twstereorcv.exe : fatal error LNK1120: 外部参照 1 が未解決です。

  • C++がコンパイルできない...

    非常に初歩的な問題で恐縮なのですが、以下のようなシンプルなプログラムでコンパイルが出来ず困っております。 何がいけないのか、もしくは何をチェックすればよいかを教えて下さい *_*; program #include <iostream.h> int main(void){ cout << "matumoto"; } ---- 最初 void main(void)とした所、怒られたため、int ...としましたが、更に以下のようなエラーが出力されてしまいました 未定義の 最初に参照している シンボル ファイル std::ios_base::Init::~Init [in-charge]()/var/tmp//ccxj7cux.o std::ios_base::Init::Init[in-charge]()/var/tmp//ccxj7cux.o std::cout /var/tmp//ccxj7cux.o std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)/var/tmp//ccxj7cux.o __gxx_personality_v0 /var/tmp//ccxj7cux.o ld: 重大なエラー: シンボル参照エラー。a.out に書き込まれる出力はありません。 collect2: ld returned 1 exit status [環境] Solaris 8 gcc %>>gcc -ver Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/3.3/specs Configured with: ../configure --disable-nls --with-as=/usr/ccs/bin/as --with-ld=/usr/ccs/bin/ld Thread model: posix gcc version 3.3 すみませんが、宜しくお願い致します。

  • Visual C++ Toolkitでcppをコンパイルすると・・

    最近質問が多くてすみません。 一応調べてみましたが、同じような質問が既存ならば警告お願いします。 #include <iostream> int main(){ return 0; } これだけのファイルを、例えばtest.cppとして保存します。そして cl test.cpp とすると、ぶわーっと警告(warning)がでて、しかしtest.exeができあがります。 警告内容がかなり長いので、そのうちの1つを載せます。 C:\Program Files\Microsoft Visual C++ Toolkit 2003\include\stdexcept(39) : see reference to class template instantiat ion 'std::basic_string<_Elem,_Traits,_Ax>' being compiled with [ _Elem=char, _Traits=std::char_traits<char>, _Ax=std::allocator<char> ] 意味がわかりません!! ちなみに、bccでは警告なしにコンパイルできます。

  • lunuxでコンパイル

    普段はリヌックスの環境を使っているのですが、ウィンドウズでしか動かないプログラムがあり、プログラムを実行するたびに、マシンを切り替えていて不便を感じています。 http://prefixspan-rel.sourceforge.jp/のC++のファイルをリヌックスでコンパイルしたいのですが、g++ prefixspan.cpp では In file included from /usr/include/c++/4.4/backward/strstream:47, from prefixspan.cpp:30: /usr/include/c++/4.4/backward/backward_warning.h:28:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. prefixspan.cpp: In function ‘int main(int, char**)’: prefixspan.cpp:492: error: ‘atoi’ was not declared in this scope となります。C++に関しては詳しくないのですが、これは直せるものなのでしょうか?

  • STLで、vectorのファイルへの書き出し、読み込み。

    STLで、vectorのファイルへの書き出し、読み込み。 C++ の STL で vector をファイルに書き出し、読み込もうとしておりまして、まずは書き出しで躓いております。 vector<int> testvector; for (i = 0 ; i < 10 ; i++) testvector.push_back( i ); ofstream out("test.dat", ios::out | ios::binary); if (!out) return 1; out.write((vector<int>) testvector, sizeof(testvector)); out.close(); こんな感じのコードですと、 error: no matching function for call to ‘std::basic_ofstream<char, std::char_traits<char> >::write(std::vector<int, std::allocator<int> >, long unsigned int)’ /usr/include/c++/4.2.1/bits/ostream.tcc:173: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::write(const _CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>] とのことです orz. どのようにすれば vector を書き出し、そして読み込むことができるのでしょうか。情報がありましたら是非お寄せください。

  • エラーの意味がわからない

    すみません。 以前に似たような質問をしたのですが解決されません。 誰かおしえてください。 ---------------- vtktorus.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall vtkObject::PrintTrailer(class std::basic_ostream<char,struct std::char_traits<char> > &,class vtkIndent)" (?PrintTrailer@vtkObject@@UAEXAAV?$basic_ostream@DU?$ char_traits@D@std@@@std@@VvtkIndent@@@Z) ---------------- なんですけど、とりあえず考慮してやってみたことは (1)libファイルを足してみる。 >>>vtkを使っているのでvtkdll.libだけ足しました。 (2)本体が見つからないらしいので本体を探してみる。 >>>ちゃんと記入しました。 (3)非常にたくさんのソースを併用しているので変数とかがかぶっていないか見てみる >>>かぶってないみたいです。

  • コンパイルエラーについて

    関数 y=x と y=x*x/8 のグラフをテキストで描くプログラムなのですが、cygwinでコンパイルすると次のエラーが出ます。何を直せばいいのか教えてください。 /tmp/cceveo7s.o:week2.c:(.text+0x62): undefined reference to `_f' /tmp/cceveo7s.o:week2.c:(.text+0xb1): undefined reference to `_f' /tmp/cceveo7s.o:week2.c:(.text+0xbd): undefined reference to `_g' collect2: ld はステータス 1 で終了しました ソースプログラムはこちらです。 #include <stdio.h> #define XSIZE 20 #define YSIZE 20 char graph[XSIZE][YSIZE]; /* 座標(x, y)に文字cをプロットする */ void f(int x, int y, char c); /* 作成したグラフを表示する */ void g(void); int main(void){ int x, y; for (x=0;x<XSIZE;x++){ y = x; f(x, y, '+'); } for (x=0 ; x < XSIZE ; x++){ y=x*x/8 ; f(x, y, '*'); if(y>=YSIZE) break; g(); return 0; } void f(int x, int y, char c) { graph[x][y] = c; } void g(void) { int a, b; for (b=YSIZE ; b>0 ; b--){ for (a = 0 ; a < XSIZE ; a++){ if (graph[a][b]==0) printf(" "); else printf("%c ", graph[a][b]); } printf("\n"); } } return 0; }

専門家に質問してみよう