我应该如何编译此C代码? [英] How should I compile this C code?

查看:103
本文介绍了我应该如何编译此C代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载了此源代码(.zip)并希望在我的Mac(OSX 10.11.2)上使用XCode版本7.2(7C68)进行编译.

I downloaded this source code (.zip) and would like to compile it on my Mac (OSX 10.11.2) with an XCode Version 7.2 (7C68).

我开始使用编译文件fdist.c

gcc -o fdist2 -O fdist.c -lm

,但它会返回一系列警告和错误(请参见下文).查看文件,我发现它确实不像我惯用的那种代码.通常,似乎未指定返回函数的对象类型.

but it returns a long series of warnings and errors (see below). Looking at the file I see that indeed it does not quite look as the kind of code I am used to. Typically, it appears that the type of object the functions returned are unspecified.

README_fdist2并没有太大帮助.有有关如何编译代码的准则,但第一行有错字.

The README_fdist2 did not help much. There are guidelines on how to compile the code but there is a typo in the first line.

我应如何编译此代码?

以下是命令gcc -o fdist2 -O fdist.c -lm返回的错误和警告

Below are the errors and warnings the command gcc -o fdist2 -O fdist.c -lm returned

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要求提前声明所有变量和函数.函数和类的定义应放在.h头文件中,然后包含在引用它们的.c源文件中.

The problem is not the compiler but the fact that your code does not follow the syntax. C99 requires that all variables and functions be declared ahead of time. Function and class definitions should be placed in a .h header file and then included in the .c source file in which they are referenced.

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;
}

这篇关于我应该如何编译此C代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆