简体   繁体   中英

Issue with including external source files with GCC

I am rather new to C, so forgive me if my question is too trivial, although I could not find any working solution in Stackoverflow.

I am trying to compile a source file using GCC while providing another external source file (which has the actual methods I need).

Here is a trivial example of what I'm trying to do:

This is my main source file, hello.c

#include <stdio.h>

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

This is my external source file method.c:

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

This is the command line I'm trying to use:

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

I get the following error:

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");
  ^

Update: What confused me was that if I am using "#include "method.c" in hello.c instead of using GCC -include it works well. My trivial way of thinking got me to assume that the include simply drops the code inside the main file while enjoying all the inclusions the main source file already taken care of (such as stdio.h).

Appreciate your help Uri

You have to include stdio.h in your method method.c . stdio.h contains the method printf

您需要在文件method.c包含头文件<stdio.h>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM