Uri Lukach
Uri Lukach

Reputation: 1121

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

Upvotes: 0

Views: 130

Answers (2)

Rahul Tripathi
Rahul Tripathi

Reputation: 172438

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

Upvotes: 2

ameyCU
ameyCU

Reputation: 16607

You need to include header <stdio.h> in your file method.c.

Upvotes: 1

Related Questions