テスト用に、以下の最も初歩的なプログラムを組みました。
#include <stdio.h>
main()
{
printf( "hello!" );
return 1;
}
これを、
cc -o temp temp.cpp
として、コンパイルすると、以下のメッセージがでます。
/var/tmp//ccwRLUq5.o(.eh_frame+0x11): undefined reference to
`__gxx_personality_v0'
試しに
cc -o temp temp.cpp -lc
としてコンパイルして見ても結果は変わりません。
printf以外にもscanf,sprintf,puts,fopenなどを単独でコンパイルしてみました
が、結果は変わりませんでした。
ただし、atoiやstrlenのような同じlibc.aを使う関数は何ともなくコンパイル完了
します。
FreeBSDのバグなのでしょうか?解決策等、或いは同じ経験をした方、「自分は5.2
使っているけどそんな症状はでないよ」と言う方がいらっしゃったらご助言・ご報告
いただければ幸いです。
ちなみにOSはFreeBSD5.2で、先日公式サイトからisoイメージを取得し、CD-Rに焼
いてインストールしました。
1台で上記の現象が発生したので、もう一台のPCにもインストールしてみて実行し
ましたが、結果は変わりませんでした。(この際同一のCDを使ってインストールして
います)
よろしくお願いいたします。
**************************************
*** 田淵義弘 in SPOONsoftware
*** E-Mail : tab...@spoonsoftware.com
*** HP : http://www.spoonsoftware.com
**************************************
>>>>> In <btiht4$flj$1...@pin3.tky.plala.or.jp>
>>>>> "Yoshihiro Tabuchi" <tab...@spoonsoftware.com> wrote:
> テスト用に、以下の最も初歩的なプログラムを組みました。
> これを、
> cc -o temp temp.cpp
^^^^
> として、コンパイルすると、以下のメッセージがでます。
> /var/tmp//ccwRLUq5.o(.eh_frame+0x11): undefined reference to
> `__gxx_personality_v0'
> 試しに
> cc -o temp temp.cpp -lc
^^^^
> としてコンパイルして見ても結果は変わりません。
ファイル名が .cpp で終わっているので、C++ のプログラムだとコンパイラが判断
するからです。
FreeBSD 附属の cc の実体は gcc ですが、その info によると、
Options Controlling the Kind of Output
======================================
Compilation can involve up to four stages: preprocessing, compilation
proper, assembly and linking, always in that order. The first three
stages apply to an individual source file, and end by producing an
object file; linking combines all the object files (those newly
compiled, and those specified as input) into an executable file.
For any given input file, the file name suffix determines what kind
of compilation is done:
...
`FILE.cc'
`FILE.cxx'
`FILE.cpp' ←これ
`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.
ですから、ファイル名を temp.c に変更するとうまくいくはずです。
--
NAKAJI Hiroyuki (中治 弘行)
>
> ファイル名が .cpp で終わっているので、C++ のプログラムだとコンパイラが判断
> するからです。
>
> FreeBSD 附属の cc の実体は gcc ですが、その info によると、
>
> Options Controlling the Kind of Output
> ======================================
>
> Compilation can involve up to four stages: preprocessing, compilation
> proper, assembly and linking, always in that order. The first three
> stages apply to an individual source file, and end by producing an
> object file; linking combines all the object files (those newly
> compiled, and those specified as input) into an executable file.
>
> For any given input file, the file name suffix determines what kind
> of compilation is done:
>
> ...
>
> `FILE.cc'
> `FILE.cxx'
> `FILE.cpp' ←これ
> `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.
>
> ですから、ファイル名を temp.c に変更するとうまくいくはずです。
> --
なるほど...確かに.cに変えれば動きますね...
ただ、実際に5.2に移植したいプログラムは、クラスを使った上に内部でsprintfを
使って文字列の設定をやっているプログラムなので、単純に拡張子(UNIXでこう呼ん
でいいのかな?)を変えるわけには行きません。
例えば、下のようなコードを正常に動かしたければどうすればよいでしょうか?
#include <stdio.h>
class Printer
{
public:
void out(){printf( "hello!" ); }
};
main()
{
Printer p;
p.out();
return 1;
}
FreeBSD4.7でやったときには普通にリンクまで出来ていたのですが...誠に申し
訳ありませんが、よろしくお願いいたします。
素直に
c++ -o temp temp.cpp
とやるか、
cc -o temp temp.cpp -lstd++
でしょうか。
//kawano
At Thu, 8 Jan 2004 18:58:32 +0900,
すみません。
> cc -o temp temp.cpp -lstd++
は
cc -o temp temp.cpp -lstdc++
^
のtypoです。
//河野
At Thu, 08 Jan 2004 20:27:53 +0900,
C++使うためには-lstdc++をやるんですか。たしかにちゃんとコンパイルできまし
た。
3年くらいUNIXでプログラミングやってて初めて知った...(お恥ずかしい)。
今まで、付けなくてもC++でコンパイルできてたから...
そう言えばUNIX用のC++の本って読んだことがなかったです。勉強し直します。
お恥ずかしい質問で、誠に申し訳ありませんでした。親切に教えてくださった河野
さんありがとうございました。
> そう言えばUNIX用のC++の本って読んだことがなかったです。勉強し直します。
所詮、コンパイラの問題なので、
info gcc
で gcc のマニュアルを読めばわかる…、はずですよ。