【问题标题】:How should I compile this C code?我应该如何编译这个 C 代码?
【发布时间】:2016-04-18 20:34:25
【问题描述】:

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

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

【问题讨论】:

    标签: c macos compilation compiler-errors


    【解决方案1】:

    问题不在于编译器,而在于您的代码不遵循语法这一事实。 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;
    }
    

    【讨论】:

      【解决方案2】:

      看起来代码不符合 C99 的隐式函数声明、默认返回 (int) 等。

      修复它似乎并不难。但是,如果您不想或不能,那么您可以尝试在 implicit int return 有效的 C89/C90 中编译。这应该可以解决您收到的大部分(如果不是全部)警告。

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

      【讨论】:

      • 感谢您的回答。我尝试了-std=c89-std=c90,但在这两种情况下都失败了。
      • 您仍然收到所有这些警告吗?另一种选择是-std=gnu89。如果没有,您可以为函数进行声明。这就是您真正需要正确修复它们的全部内容。
      • 我没有收到相同的警告,但我仍然收到相同的错误。与-std=gnu89 相同。所以我想,我只需要自己编辑代码的合成器,对吧?
      • 是的。您需要对函数进行声明。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-12
      • 2013-10-15
      • 1970-01-01
      • 2013-02-14
      • 2021-07-25
      • 2023-04-03
      相关资源
      最近更新 更多