Cのコンパイルについて
2010/11/17
math.hを使うときのコンパイルオプション.
久しぶりにmath.hの関数を使うことになりコンパイルしたらエラーになりました.実行したのは下記.
#include<stdio.h>
#include<math.h>
int main(int argc, char *argv[])
{
double tei=2;
double kata=10;
double atai;
atai = pow(tei,kata);
printf("%f\n",atai);
return 0;
}
コンパイルをした際はノーオプションでgccを使いました.ushi@BigOwl:~$ gcc power-test.c /tmp/ccMaP96f.o: In function `main': power-test.c:(.text+0x31): undefined reference to `pow' collect2: ld returned 1 exit status ushi@BigOwl:~$それが間違いで,math.hを使うときは-lmオプションをつけるべきだったのでした.mathのライブラリを読めというような意味と思われます.
ushi@BigOwl:~$ gcc power-test.c -lm ushi@BigOwl:~$今度はエラーが出ることもなく.盲点かも知れないので,記事にしておきます.
どうやら,bccなどでは多分このオプションは不要です.