【问题标题】:Issue with including external source files with GCC使用 GCC 包含外部源文件的问题
【发布时间】:2015-09-11 09:29:51
【问题描述】:

我对 C 比较陌生,所以如果我的问题太琐碎,请原谅我,尽管我在 Stackoverflow 中找不到任何可行的解决方案。

我正在尝试使用 GCC 编译源文件,同时提供另一个外部源文件(它具有我需要的实际方法)。

这是我正在尝试做的一个简单的例子:

这是我的主要源文件,hello.c

#include <stdio.h>

int main (void)
{
printTest();
return 0;
}

这是我的外部源文件method.c:

 void printTest(){
 printf ("Hello, world!\n");
}

这是我尝试使用的命令行:

 gcc -include method.c  hello.c -o hello -I./

我收到以下错误:

In file included from <command-line>:1:0:
./method.c: In function ‘printTest’:
./method.c:2:2: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
  printf ("Hello, world!\n");
  ^

更新: 让我感到困惑的是,如果我在 hello.c 中使用“#include”method.c”而不是使用 GCC -include 效果很好。我的简单思维方式让我假设 include 只是将代码放在 main 中文件,同时享受主源文件已经处理的所有内容(例如 stdio.h)。

感谢您的帮助 乌里

【问题讨论】:

  • method.c 中包含stdio.h
  • 您需要在method.c 中包含stdio.h 才能获得printf 的声明。
  • 谢谢!现在效果很好!再次抱歉这个琐碎的问题,我是 c 的新手

标签: c gcc


【解决方案1】:

您必须在方法method.c 中包含stdio.hstdio.h 包含方法printf

【讨论】:

    【解决方案2】:

    您需要在文件method.c 中包含标题&lt;stdio.h&gt;

    【讨论】:

      猜你喜欢
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多