1

So my situation is something like this, I have three files:

main.c:

#include <stdio.h>
#include "hello.h"

int main() {
    hello();
}

hello.h:

void hello();

hello.c:

void hello() {
    printf("Hello");
}

My Cmake file looks something like this:

cmake_minimum_required(VERSION 3.3)
project(test)

set(SOURCE_FILES main.c hello.c)
add_executable(test ${SOURCE_FILES})

The code runs fine. However CLion doesn't recognise the printf() function in hello.c, and wants me to add it as a header file. Is there a way to make it see the #include <stdio.h> in the main.c file, and stop giving me a hard time?


So I fixed this to my own satisfaction by making the functions defined in my .c files return values rather than calling printf inside those functions. Then printing the values returned in main.c

Alex DB
  • 111
  • 5

1 Answers1

2

hello.c and main.c are independent compilation units and as such needs to have #include <stdio.h> in both. Actually in your example having #include <stdio.h> in main.c accomplishes nothing as nothing forward declared there is being used in hello.h nor main.c.

You should actually be seeing warnings when compiling hello.c on its own.


When the compiler finds a function it does not know (has not been declared yet), it assumes it has the signature int function_X(void). So for your case it will be wrong for printf which has int printf(char const*, ...). But you are lucky, due to the way that arguments are passed in your platform, everything works out.

So you basically need to forward declare functions to ensure that when compiling the compiler knows where to place the arguments so that the called function can find it.

There is more to it but this short explanation should be enough for a beginner and if you read one of the books in the link I provided in the comments you should be able to understand it better.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
RedX
  • 14,749
  • 1
  • 53
  • 76
  • Yeah, but the code still works. In my actual use case, I'm not planning on compiling the non-main.c files separately, and it would be a pain to add the stdio header in every one of them. You say having including the stdio header in main.c accomplishes nothing, but if I remove it the code doesn't work. I guess I'm just treating the collection of files as one executable, whereas the compiler sees the different C files seperately – Alex DB Jan 06 '16 at 11:25
  • Actually, I just removed the stdio header and the code still worked... Maybe it's adding the header automatically when if finds the printf function – Alex DB Jan 06 '16 at 11:27
  • 1
    @AlexDB Do you know how function declarations work in C - in particular, implicit declarations? Also, do you know how header files work? – user253751 Jan 06 '16 at 11:30
  • I'm very new to C (couple of days), and I wasn't really sure what the point of declaring a function first, then defining it later was, I just read that it's the proper thing to do. And I think I know how to use header files, but not exactly what the difference between using a header file, and just including the code in the header file, where it's called in main. – Alex DB Jan 06 '16 at 11:37
  • @AlexDB Then probably you should pick one of [these book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and go through it. – RedX Jan 06 '16 at 12:08