How do I compile this code?

I have downloaded this source code (.zip) and would like to compile it on my Mac (OSX 10.11.2) with XCode 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 can see that it really doesn't look like the code I'm used to. Typically, it seems that the object type returned by the function is not specified.

README_fdist2

did not help. There are guidelines for compiling the code, but there is a typo in the first line.

How do I compile this code?


Following are the errors and warnings returned by the command 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.

      

0


source to share


2 answers


The problem is not with the compiler, but that your code doesn't match the syntax. C99 requires all variables and functions to be declared in advance. Function and class definitions must be placed in a .h header file and then included in the .c source file in which they are specified.

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

      

+3


source


It looks like the code is not C99 compliant with implicit function declarations, default return (int), etc.

It seems that this is not difficult to fix. But if you don't want to or cannot, you can try to compile in C89 / C90 where the implicit int return is valid. This should fix most, if not all, of the warnings you receive.



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

      

+3


source







All Articles