2016-01-13 44 views
0

我下載了this source code (.zip),並希望在我的Mac(OSX 10.11.2)上用XCode V7.2(7C68)進行編譯。我應該如何編譯這個C代碼?

我開始編制該文件fdist.c

gcc -o fdist2 -O fdist.c -lm 

但它返回一個長系列的警告和錯誤(見下文)。看着這個文件,我發現它確實不像我習慣的那種代碼。通常情況下,函數返回的對象類型似乎未指定。

README_fdist2沒有什麼幫助。有關於如何編譯代碼的指導原則,但第一行存在拼寫錯誤。

我該如何編譯這段代碼?


下面是錯誤和警告gcc -o fdist2 -O fdist.c -lm返回

fdist2.c:42:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int] 
main() 
^ 
fdist2.c:117:3: warning: implicit declaration of function 'sim1' is invalid in C99 
     [-Wimplicit-function-declaration] 
       sim1(init,initot,rm,mu,freq_arr,val_arr,&noall); 
       ^
fdist2.c:119:4: warning: implicit declaration of function 'my_thetacal' is invalid in C99 
     [-Wimplicit-function-declaration] 
         my_thetacal(freq_arr,noall,init,Subs,&h0,&h1,&fst); 
         ^
fdist2.c:136:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int] 
sim1(init,initot,rm,mu,freq_arr,val_arr,noall) 
^ 
fdist2.c:222:3: warning: implicit declaration of function 'dfill' is invalid in C99 
     [-Wimplicit-function-declaration] 
       dfill(); 
       ^
fdist2.c:234:4: warning: implicit declaration of function 'cnode' is invalid in C99 
     [-Wimplicit-function-declaration] 
         cnode(k); 
         ^
fdist2.c:237:8: warning: implicit declaration of function 'mnode' is invalid in C99 
     [-Wimplicit-function-declaration] 
       else mnode(k); 
        ^
fdist2.c:252:1: warning: control may reach end of non-void function [-Wreturn-type] 
} 
^ 
fdist2.c:254:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int] 
thetacal(gen,noall,sample_size,no_of_samples,het0,het1,fst) 
^ 
fdist2.c:293:1: warning: control reaches end of non-void function [-Wreturn-type] 
} 
^ 
fdist2.c:301:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int] 
dfill() 
^ 
fdist2.c:312:1: warning: control reaches end of non-void function [-Wreturn-type] 
} 
^ 
fdist2.c:315:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int] 
cnode(sp) 
^ 
fdist2.c:349:2: error: non-void function 'cnode' should return a value [-Wreturn-type] 
     return; 
     ^
fdist2.c:353:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int] 
mnode(sp) 
^ 
fdist2.c:464:2: error: non-void function 'mnode' should return a value [-Wreturn-type] 
     return; 
     ^
fdist2.c:489:10: warning: implicit declaration of function 'poidev' is invalid in C99 
     [-Wimplicit-function-declaration] 
     mutno = poidev(time*mu); 
       ^
fdist2.c:491:12: warning: implicit declaration of function 'addmut' is invalid in C99 
     [-Wimplicit-function-declaration] 
       p->dna = addmut(p->dna); 
         ^
fdist2.c:676:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int] 
mom(x,n,x1,x2,x3,x4,min,max) 
^ 
fdist2.c:707:2: error: non-void function 'mom' should return a value [-Wreturn-type] 
     return; 
     ^
fdist2.c:761:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int] 
my_thetacal(gen,noall,sample_size,no_of_samples,het0,het1,fst) 
^ 
18 warnings and 3 errors generated. 

回答

3

問題不在於編譯器,而在於您的代碼不遵循語法。 C99要求提前聲明所有變量和函數。函數和類定義應放置在.h頭文件中,然後包含在引用它們的.c源文件中。

mycode.h:

int myFunction (int a, char * s);

mycode.c中

#include "mycode.h" 

int main(int argc, char **argv) { 
    int x = 2; 
    char * str = "Hello"; 
    int r = myFunction(x,str); 
    return r; 
} 
3

貌似代碼不符合C99與隱函數declrations,默認返回(INT)等

命令

這似乎並不難解決它。但是如果你不想或者不能,那麼你可以嘗試在C89/C90中編譯,其中隱式int返回有效。這應該修復大部分即使不是全部的警告。

gcc -std=c89 -o fdist2 -O fdist.c -lm 
+0

謝謝您的回答。我試過'-std = c89'和'-std = c90',但在兩種情況下仍然失敗。 –

+0

你仍然得到所有這些警告?另一個選項是'-std = gnu89'。如果沒有,你可以爲函數做聲明。這就是你需要真正解決它們的問題。 –

+0

我沒有得到相同的警告,但我仍然得到相同的錯誤。與'-std = gnu89'一樣。所以我想,我只需要自己編輯代碼的合成代碼,對吧? –