我在安裝GCC 4.4.5的gentoo linux上。我可以使用gcc main.c -o main和命令./main正確地返回結果,從而編譯並鏈接此程序,而不會出現任何錯誤。鏈接多個C源文件
[main.c]
#include <math.h>
#include <stdio.h>
int main(void)
{
double c = ceil(2.5);
printf("The ceil of 2.5 is %f\n", c);
return 0;
}
但是當我把小區的invokation到另一個源文件時,會出現問題。
[calc.h]
#ifndef _CALC_H_
#define _CALC_H_
double myceil(double n);
#endif
[calc.c]
#include <math.h>
#include "calc.h"
double myceil(double n)
{
return ceil(n);
}
[main1.c]
#include <stdio.h>
#include "calc.h"
int main(void)
{
double c = myceil(2.5);
printf("The ceil of 2.5 is %f\n", c);
return 0;
}
使用命令GCC calc.c main1.c -o MAIN1發生,這樣的錯誤:
/tmp/cc6GhJvZ.o: In function `myceil':
calc.c:(.text+0x19): undefined reference to `ceil'
collect2: ld returned 1 exit status
那麼,爲什麼煩人的錯誤 「未定義的引用」 發生在後一種情況?我知道這個錯誤可以通過添加一個庫-lm來消除,但是,我只想知道爲什麼gcc會在後一種情況下拋出錯誤。
這個錯誤不會在osx或RHEL 6中顯示... –
@ Foo,最近我在Vmware Workstation 7.0.0上安裝了rhel-workstation-6.0-i386-dvd.iso,但錯誤依然存在。 – machinarium
哦我使用的是x64服務器版本 –