【问题标题】:Why isn't gcc recognizing my .c file even when I provide it as an argument?为什么即使我将其作为参数提供,gcc 也无法识别我的 .c 文件?
【发布时间】:2020-03-15 12:16:07
【问题描述】:

我有file1.c:

#include <stdio.h>

int main() {

    file2();

}

file2.c:

#include <stdio.h>

int file2() {
}

我正在尝试用gcc -o file1 file1.c file2.c 编译file1,但我收到了错误implicit declaration of function 'file2' is invalid

有人知道我在这里可能会错过什么吗? file1.cfile2.c 都在同一个文件夹中。

【问题讨论】:

  • gcc -o file1 file1.c -include file2.c。不要真的这样做......

标签: c gcc compiler-errors compilation


【解决方案1】:

有谁知道我在这里可能缺少什么?

每个源文件在编译时被单独处理。因此编译器在处理file.1.c 时不知道file2() 可能是什么并抱怨。

【讨论】:

    【解决方案2】:

    您需要在您的file1.c 或任何包含的文件中使用函数原型int file2(void);,因为每个文件都是单独编译的,您需要在编译器找到该函数之前再调用它。

    【讨论】:

      【解决方案3】:

      您还需要在file1.c 文件中包含函数声明。像这样

      #include <stdio.h>
      
      int file2();
      
      int main() {
          file2();
      }
      

      【讨论】:

      • 使它成为原型:int file2(void); — 这样编译器就可以阻止它被错误地调用。
      猜你喜欢
      • 2013-06-22
      • 2019-09-02
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多