How should I compile this C code?

2019-09-06 21:38发布

问题:

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).

I started to compile the file fdist.c with

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.

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.

How should I compile this code?


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.

回答1:

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.

i.e.

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


回答2:

Looks like the code doesn't conform to C99 with implicit function declrations, default return (int) etc.

It doesn't seem to be hard to fix it. But if you don't want to or can't then you can attempt to compile in C89/C90 in which implicit int return is valid. This should fix most, if not all, of the warnings you get.

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