我该如何编译这个C代码?

时间:2016-01-13 18:08:32

标签: c macos compilation compiler-errors

我已下载this source code (.zip),并希望使用XCode版本7.2(7C68)在我的Mac(OSX 10.11.2)上进行编译。

我开始使用

编译文件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.

2 个答案:

答案 0 :(得分:3)

看起来代码不符合具有隐式函数declrations的C99,默认返回(int)等。

似乎并不难解决它。但是如果你不想或不想那么你可以尝试在C89 / C90中编译,其中 implicit int 返回有效。这应该可以解决大多数(如果不是全部)警告。

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

答案 1 :(得分: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;
}