• 締切済み

C言語コンパイラーエラーについて教えてください。

C言語でコンパイルができなくて困っています。 コンパイルをすると、 ----------------------------------------- test.c.20:undefined reference to `getcury' test.c:20: undefined reference to `getcurx' collect2: ld returned 1 exit status ----------------------------------------- と言うエラーが出てしまい、コンパイルが完了しません。 ソースは、「test.c」と言うファイルネームで、内容は ----------------------------------------- #include <stdio.h> #include <stdlib.h> #include <curses.h> int main (void) { int y = 0, x = 0; initscr(); getyx(curscr, y, x); printw("x = %d, y = %d", x, y); refresh(); getchar(); endwin(); return 0; } ----------------------------------------- です。 OSは、Windows7 Professional SP1 コンパイラーは、MinGW Ver5.1.6. で、コマンドは gcc -o test.exe test.c -lcurses です。 確かに、curses へはパスも通っており、リンクできているようなんですが、 エラーが出てしまいます。 どうすれば良いのかわからず、大変困っています。 だれかご存知の方、教えてください。 どうかよろしくお願い致します。

みんなの回答

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

> おっしゃられる通り、MinGWをインストールしただけではcursesは入らなかったので、UNIX の curses.h を持って来たり、pdcurses-2.6-bin を入れたり、pdc34dllw を入れたりで、ヘッダとライブラリがめちゃくちゃになっている気がします。 素性がはっきりしていて、かつMinGWに適合するバイナリが入手できたのであればよいのですが、そうでなければうまくいかなくて当然です。 自分でcursesをコンパイルした方が、遠回りなようでも結局早かったりしませんか? 私はMinGWの環境でcursesを使ったことはないので、そもそもちゃんとコンパイルできて動作するのかどうかは知りませんが...

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

MinGWをインストールしただけではcursesは入らないはずです。 cursesはどんな素性のものを入手したのでしょうか? その辺りに問題の原因がありませんか?

ma-0710
質問者

お礼

ありがとうございます。 おっしゃられる通り、MinGWをインストールしただけではcursesは入らなかったので、UNIX の curses.h を持って来たり、pdcurses-2.6-bin を入れたり、pdc34dllw を入れたりで、ヘッダとライブラリがめちゃくちゃになっている気がします。 すべて消して一からやり直してみます。

  • Tacosan
  • ベストアンサー率23% (3656/15482)
回答No.5

とりあえず ・どの curses.h を #include するのか ・それに対応するライブラリ libcurses.* はどこにあるのか を確認しては? curses は Unix 由来だから, #include している curses.h のあるディレクトリ付近をさがせば対応する libcurses.* が見付かりませんかね.

ma-0710
質問者

お礼

ありがとうございます。 curses.h の整理とライブラリ探しをやってみます。

  • wormhole
  • ベストアンサー率28% (1622/5658)
回答No.4

>それと、めちゃくちゃにインストールしたためか、curses.h がいくつもありどれを使えば良いかわからない状態です。(念の為すべて試してみましたが…) > >何かヘッダファイル以外に、「.a」や「.dll」など必要なファイルがあるのでしょうか? >具体的に何をどうすればよいのやら、さっぱりわからず、立ち往生です。 >何か他に心当たりがあるようでしたら、教えていただけないでしょうか。 #2の方はその心当たりをすでに書かれてると思いますが。 要は、ヘッダファイルとリンクしているライブラリの組み合わせがかみ合ってないんだから、ヘッダファイルとリンクするライブラリの組み合わせを適切にしてください。って事です。 ヘッダファイルとリンクするライブラリの適切な組み合わせは、さすがにめちゃめちゃな状態といわれている以上アドバイスできる人はいないでしょうからご自分で調べるしかないと思います。 またヘッダファイルにあるのは基本的に関数の宣言であって、関数の本体はライブラリ(*.aや*.dll)にありますからヘッダファイルだけあってもリンクや実行時にエラーになります。 あと大抵のヘッダファイルは自分が必要とするヘッダファイルは自分でインクルードするように書かれてる事が多いので前もってインクルードしておかなければならないということはあまりないです。

ma-0710
質問者

お礼

ありがとうございます。大変参考になります。 getyxを使用するには、curses.hがあれば良いと思いますので、一先ずヘッダーは置いといて、もう一度 curses のライブラリを見直ししてみます。

回答No.3

> 「The inclusion of <curses.h> may make visible all symbols from the headers <stdio.h>, <term.h>, <termios.h> and <wchar.h>. 」って書いてある。 「curses.h を #include すると<stdio.h>などモロモロに定義されたシンボルも見えちゃうかも」と言うてるのだから、 他のヘッダが暗黙裡に #include されるんでしょう。だから > curses.hをインクルードするには「その前にインクルードしておく必要のあるヘッダ」がいっぱいある。 コレは違うんじゃないかしら?

ma-0710
質問者

お礼

ありがとうございます。 ヘッダファイルに関しては、よく吟味してからインクロードするようにします。

  • Tacosan
  • ベストアンサー率23% (3656/15482)
回答No.2

ヘッダやライブラリが複数あったりすると「どれを #include してどれをリンクするのか」で混乱する可能性が考えられるのですが, その辺は大丈夫でしょうか? つまり, 例えば ・#include しているヘッダでは getyx が getcurx や getcury を使う ・リンクしているライブラリは (対応するヘッダが使うことを想定していないので) getcurx や getcury を提供していない というようなことになってると, 明らかに破たんしますよね.

ma-0710
質問者

補足

ご回答ありがとうございます。 確かに curses.h では、 getyx が getcurx や getcury を使うようになっています。 それと、めちゃくちゃにインストールしたためか、curses.h がいくつもありどれを使えば良いかわからない状態です。(念の為すべて試してみましたが…) 何かヘッダファイル以外に、「.a」や「.dll」など必要なファイルがあるのでしょうか? 具体的に何をどうすればよいのやら、さっぱりわからず、立ち往生です。 何か他に心当たりがあるようでしたら、教えていただけないでしょうか。 よろしくお願い致します。

  • chie65535
  • ベストアンサー率43% (8524/19373)
回答No.1

http://pubs.opengroup.org/onlinepubs/7908799/xcurses/curses.h.html に「The inclusion of <curses.h> may make visible all symbols from the headers <stdio.h>, <term.h>, <termios.h> and <wchar.h>. 」って書いてある。 curses.hをインクルードするには「その前にインクルードしておく必要のあるヘッダ」がいっぱいある。 少なくとも、stdio.hとstdlib.hだけじゃ「何もしてないのと一緒」だから、絶対にコンパイルできない。 んで「前提となるヘッダ」もインクルードすると「そのヘッダをインクルードする為に、事前にインクルードしておくヘッダ」が雪ダルマ式に増えていって収集が付かなくなるから、覚悟しておくように。 経験から、コンパイルが通るようになるだけで2日は必要。さらに、リンクが通るようになるにはもう2日かかると思う。

ma-0710
質問者

お礼

早速のご回答ありがとうございます。 コツコツと頑張ってみます。

関連するQ&A

  • PostgreSQLをC言語でアクセスしようと

    PostgreSQL 8.4.8 文書にのっているサンプルプログラムの 例 30-1. libpq サンプルプログラム 1 #include <stdio.h> #include <stdlib.h> #include "libpq-fe.h" ・・・・・・・・・・・・・ をコピペしてコンパイルしたところ次のようなエラーがでました # gcc sample.c /tmp/cctZBolQ.o: In function `exit_nicely': sample.c:(.text+0xd): undefined reference to `PQfinish' /tmp/cctZBolQ.o: In function `main': sample.c:(.text+0x4a): undefined reference to `PQconnectdb' sample.c:(.text+0x5a): undefined reference to `PQstatus' sample.c:(.text+0x6a): undefined reference to `PQerrorMessage' sample.c:(.text+0xa5): undefined reference to `PQexec' sample.c:(.text+0xb5): undefined reference to `PQresultStatus' sample.c:(.text+0xc6): undefined reference to `PQerrorMessage' sample.c:(.text+0xed): undefined reference to `PQclear' sample.c:(.text+0x105): undefined reference to `PQclear' sample.c:(.text+0x119): undefined reference to `PQexec' sample.c:(.text+0x129): undefined reference to `PQresultStatus' sample.c:(.text+0x13a): undefined reference to `PQerrorMessage' sample.c:(.text+0x161): undefined reference to `PQclear' sample.c:(.text+0x179): undefined reference to `PQclear' sample.c:(.text+0x18d): undefined reference to `PQexec' sample.c:(.text+0x19d): undefined reference to `PQresultStatus' sample.c:(.text+0x1ae): undefined reference to `PQerrorMessage' sample.c:(.text+0x1d5): undefined reference to `PQclear' sample.c:(.text+0x1ed): undefined reference to `PQnfields' sample.c:(.text+0x20f): undefined reference to `PQfname' sample.c:(.text+0x26b): undefined reference to `PQgetvalue' sample.c:(.text+0x2a8): undefined reference to `PQntuples' sample.c:(.text+0x2ba): undefined reference to `PQclear' sample.c:(.text+0x2ce): undefined reference to `PQexec' sample.c:(.text+0x2de): undefined reference to `PQclear' sample.c:(.text+0x2f2): undefined reference to `PQexec' sample.c:(.text+0x302): undefined reference to `PQclear' sample.c:(.text+0x30e): undefined reference to `PQfinish' collect2: ld はステータス 1 で終了しました # # find /usr/include/ -name libpq*.h /usr/include/pgsql/internal/libpq-int.h /usr/include/pgsql/server/libpq/libpq-be.h /usr/include/pgsql/server/libpq/libpq.h /usr/include/pgsql/server/libpq/libpq-fs.h /usr/include/libpq/libpq-fs.h /usr/include/libpq-events.h /usr/include/libpq-fe.h # gcc -I/usr/include sample.c でもおなじです どうすればコンパイル出きるようになるでしょうか? よろしくお願いします

  • WinGWのC++コンパイラの設定について

    WinGWのC++コンパイラの設定について いつもお世話になっています。 C++ソースをコンパイルできなくて困っています。 原因は何なのでしょうか。 >g++ SampleC.cc Info: resolving __fmode by linking to __imp___fmode (auto-import) Info: resolving __fpreset by linking to __imp___fpreset (auto-import) C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: warning: a uto-importing has been activated without --enable-auto-import specified on the c ommand line. This should work unless it involves constant data structures referencing symbols from auto-imported DLLs. /mingw/lib/crt2.o:crt1.c:(.text+0x37): undefined reference to `__cpu_features_in it' /mingw/lib/crt2.o:crt1.c:(.text+0x89): undefined reference to `_pei386_runtime_r elocator' fu000001.o:(.idata$2+0xc): undefined reference to `libmsvcrt_a_iname' fu000002.o:(.idata$2+0xc): undefined reference to `libmsvcrt_a_iname' fu000004.o:(.idata$2+0xc): undefined reference to `libmsvcrt_a_iname' fu000005.o:(.idata$2+0xc): undefined reference to `libmsvcrt_a_iname' nmth000000.o:(.idata$4+0x0): undefined reference to `_nm___fmode' nmth000003.o:(.idata$4+0x0): undefined reference to `_nm___fpreset' collect2: ld returned 1 exit status ソース内容 /* hello world program */ #include <stdio.h> int main(void) { printf("hello world!\n"); return 0; }

  • C言語、度々申し訳ございません。コンパイルエラー。

    いつも大変お世話になり誠にありがとうございます。 下記コードがコンパイルエラーになります。 どうしてでしょうか? 度々申し訳ございません。 ご回答の程宜しくお願い申し上げます。 コード #include <stdio.h> int main(void) { int i; printf("1~10までの偶数を出力します。\n"); for(i=1; i<=10; i++){ if((i % 2) == 0) print("%d\n", i); } return 0; } コンパイルエラー C:\MinGW>gcc test21.c -o test21 test21.c: In function 'main': test21.c:10:14: warning: implicit declaration of function 'print' [-Wimplicit-function-declaration] print("%d\n", i); ^~~~~ C:\Users\jump7\AppData\Local\Temp\ccmLpsET.o:test21.c:(.text+0x3f): undefined reference to `print' collect2.exe: error: ld returned 1 exit status 以上です。 度々申し訳ございません。 ご回答の程宜しくお願い申し上げます。

  • C言語なうなんですが、コンパイルまでは行ったんですが、セグメントエラー

    C言語なうなんですが、コンパイルまでは行ったんですが、セグメントエラーを起こしてしまいます。初心者なのでどこがおかしいのかもわかりませんので、ご指摘いただけるとありがたいです。 以下がそれでゲソ。始点と終点を指定して線を描いてもらうプログラミングを目指す。 #include <stdio.h> #include "../Glib/Glib.h" int main(void) { int x1,y1,x2,y2; printf("直線の始点の座標(x1,y1)のx1,y1を、間にカンマ','を入れて入力してください。"); scanf("%d" ,"%d" ,&x1 ,&y1); printf("直線の終点の座標(x2,y2)のx1,y1を、間にカンマ','を入れて入力してください。"); scanf("%d" ,"%d" ,&x2 ,&y2); G_init(); G_open(800,600,"Draw Line"); G_show(); G_black(); G_fillrect(0, 0, 800, 600); G_flush(); printf("Return Key を押してください。\n"); getchar(); G_white(); G_fillrect(0, 0, 800, 600); G_rgb(0,0,8); void G_line(int x1, int y1, int x2, int y2); G_flush(); printf("Return key を何回か押すと終了します。\n"); getchar(); getchar(); G_close(); return (0); }

  • Eclipseだとエラーになります。

    いつも大変お世話になっております。 標記の件。 TeraPadでは成功したコードがEclipseでは通りません。 どうしてでしょうか? アドバイスのほどよろしくお願い申し上げます。 コード #include <stdio.h> void buy(int x, int y) { printf("%d万円と%d万円の車を買いました。\n",x ,y); } int main (void) { int num1, num2; printf("いくらの車を買いますか?\n"); scanf("%d", &num1); printf("いくらの車を買いますか?\n"); scanf("%d", &num2); buy(num1, num2); return 0; } コンパイルエラー C:\MinGW>gcc Sample1.c -o Sample1 c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16' collect2.exe: error: ld returned 1 exit status どうゆうことでしょうか? ご回答のほどよろしくお願い申し上げます。

  • MinGWのg++で分割コンパイルエラー

    MinGW環境でC++の勉強を始めました。 簡単なサンプルのコンパイルをしてみたのですが、ソースファイルを一括してコンパイルすると問題ないのですが、個別にコンパイルしようとするとエラーがでてしまいまいます。 原因や対処法をご存じの方がいらっしゃいましたらご教示下さい。 一括でコンパイルすると問題なし bash-3.1$ g++ -Wl,--enable-auto-import main.cpp point.cpp -lstdc++ 個別にコンパイルしようとした場合 bash-3.1$ g++ -Wl,--enable-auto-import -o main.o main.cpp C:\DOCUME~1\user\LOCALS~1\Temp\ccZrVmLp.o:main.cpp:(.text+0x16): undefined reference to `Point::Point()' C:\DOCUME~1\user\LOCALS~1\Temp\ccZrVmLp.o:main.cpp:(.text+0x32): undefined reference to `Point::Point(int, int)' C:\DOCUME~1\user\LOCALS~1\Temp\ccZrVmLp.o:main.cpp:(.text+0x3e): undefined reference to `Point::println()' C:\DOCUME~1\user\LOCALS~1\Temp\ccZrVmLp.o:main.cpp:(.text+0x4a): undefined reference to `Point::println()' collect2: ld returned 1 exit status ※ -Wl,--enable-auto-importは、他の警告を消すために入れました、無くても質問の問題に変化はありませんでした。 サンプルソース ---main.cpp--- #include<iostream> #include "point.h" using namespace std; int main(){ Point p1,p2(4,5); p1.println(); p2.println(); return 0; } ----------- ---point.h--- class Point { private: int x, y; static int count; public: Point(); Point( int, int ); void set( int, int ); void println(); }; ------------- ---point.cpp--- #include<iostream> #include"point.h" using namespace std; int Point::count; Point::Point(){ this->x = this->y = 0; ++count; } Point::Point(int ax, int ay){ this->x = ax; this->y = ay; ++count; } void Point::set( int ax, int ay ){ this->x = ax; this->y = ay; } void Point::println(){ cout << "Point(" << x << "," << y << ")" << endl; } ----------- bash-3.1$ g++ -v Using built-in specs. COLLECT_GCC=D:\MinGW\bin\g++.exe COLLECT_LTO_WRAPPER=d:/mingw/bin/../libexec/gcc/mingw32/4.5.0/lto-wrapper.exe Target: mingw32 Configured with: ../gcc-4.5.0/configure --enable-languages=c,c++,ada,fortran,obj c,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgo mp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-r untime-libs --disable-werror --build=mingw32 --pref

  • C言語、度々申し訳ございません。エラーです。

    いつも大変お世話になり誠にありがとうございます。 標記の件。 コードとコンパイルエラーを書きます。 度々すいません。 どうしたらいいでしょうか? ご回答の程宜しくお願い申し上げます。            高橋耕造 1.コード #include <studio.h> int main(void) { printf("%d\n",123); printf("\\100もらった\n"); printf("またあした\n"); return 0; } 2.コンパイルエラー C:\MinGW>gcc test2.c -o test2 test2.c:1:20: fatal error: studio.h: No such file or directory #include <studio.h> ^ compilation terminated. 検索しましたが解りません。 ご回答の程宜しくお願い申し上げます。

  • C言語、度々すいません。コンパイルエラーです。

    いつも大変お世話になり誠にありがとうございます。 下記コードがコンパイルエラーになり色々と 変えてみたのですが、どうしてもコンパイルエラー となります。 度々すいません。 ご回答の程宜しくお願い申し上げます。      記 1.コード #include <stdio.h> int main(void) { char ch; printf("アルファベットの最初の文字は何ですか?\n"); ch = getchar(); printf("アルファベットの最初の文字は%cです。\n" ch); return 0; } 2.コンパイルエラー C:\MinGW>gcc test8.c -o test8 test8.c: In function 'main': test8.c:9:51: error: expected ')' before 'ch' printf("アルファベットの最初の文字は%cです。\n" ch); 上記の通りエラーメッセージが出ます。 どうしたらいいでしょうか? ご回答の程宜しくお願い申し上げます。

  • 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言語 コンパイルエラー

    以下webページに載っているC言語のプログラムをコンパイルした時に ”/tmp/cchzThVk.o: In function 'c_to_sh': 5.c:(.text+0x5b3): undefined reference to 'sqrt' 5.c:(.text+0x64e): undefined reference to 'atan2' collect2: Id は ステータス1で終了しました。” とメッセージが出るのですが、このメッセージが出たときはプログラムにミスがあるということなのでしょうか。教えてくださいお願いします。 http://plaza.rakuten.co.jp/dragonkkk

専門家に質問してみよう