gpt4 book ai didi

c - GCC 包含外部源文件的问题

转载 作者:太空宇宙 更新时间:2023-11-04 07:06:37 24 4
gpt4 key购买 nike

我是 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.hstdio.h 包含方法 printf

关于c - GCC 包含外部源文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32520135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com