我应该如何编译这个C代码?
我下载并希望在我的Mac(OSX 10.11.2)上用XCode版本7.2(7C68)编译它 我开始用编译文件我应该如何编译这个C代码?,c,macos,compilation,compiler-errors,C,Macos,Compilation,Compiler Errors,我下载并希望在我的Mac(OSX 10.11.2)上用XCode版本7.2(7C68)编译它 我开始用编译文件fdist.c gcc -o fdist2 -O fdist.c -lm 但它会返回一长串警告和错误(见下文)。看看这个文件,我发现它确实不像我习惯的那种代码。通常,函数返回的对象类型似乎未指定 自述文件2帮助不大。关于如何编译代码有一些指导原则,但第一行有一个输入错误 如何编译此代码? 下面是命令gcc-o fdist2-o fdist.c-lm返回的错误和警告 fdist2.c:
fdist.c
gcc -o fdist2 -O fdist.c -lm
但它会返回一长串警告和错误(见下文)。看看这个文件,我发现它确实不像我习惯的那种代码。通常,函数返回的对象类型似乎未指定
自述文件2
帮助不大。关于如何编译代码有一些指导原则,但第一行有一个输入错误
如何编译此代码?
下面是命令
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.
看起来代码不符合C99的隐式函数定义、默认返回(int)等 修复它似乎并不难。但是,如果您不想或不能,那么您可以尝试在C89/C90中编译,其中隐式int返回是有效的。这将修复您收到的大多数(如果不是全部)警告
gcc -std=c89 -o fdist2 -O fdist.c -lm
看起来代码不符合C99的隐式函数定义、默认返回(int)等 修复它似乎并不难。但是,如果您不想或不能,那么您可以尝试在C89/C90中编译,其中隐式int返回是有效的。这将修复您收到的大多数(如果不是全部)警告
gcc -std=c89 -o fdist2 -O fdist.c -lm
问题不在于编译器,而在于代码没有遵循语法。C99要求提前声明所有变量和函数。函数和类定义应放在.h头文件中,然后包含在引用它们的.c源文件中 i、 e mycode.h:
intmyfunction(inta,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;
}
问题不在于编译器,而在于代码没有遵循语法。C99要求提前声明所有变量和函数。函数和类定义应放在.h头文件中,然后包含在引用它们的.c源文件中
i、 e
mycode.h:
intmyfunction(inta,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;
}
谢谢你的回答。我尝试了-std=c89
和-std=c90
两种方法,但两种方法都失败了。您仍然收到所有这些警告吗?另一个选项是-std=gnu89
。如果没有,则可以对函数进行声明。这就是正确修复它们所需要的全部。我没有收到相同的警告,但仍然收到相同的错误。与-std=gnu89
相同。所以我想,我只需要自己编辑代码的synthax,对吗?是的。您需要函数的声明。谢谢您的回答。我尝试了-std=c89
和-std=c90
两种方法,但两种方法都失败了。您仍然收到所有这些警告吗?另一个选项是-std=gnu89
。如果没有,则可以对函数进行声明。这就是正确修复它们所需要的全部。我没有收到相同的警告,但仍然收到相同的错误。与-std=gnu89
相同。所以我想,我只需要自己编辑代码的synthax,对吗?是的。您需要函数的声明。