Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C header file is causing warning "ISO C requires a translation unit to contain at least one declaration"

Using Qt Creator I made these plain C files just to test my understanding:

main.c

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

int main()
{
    printf("Hello World!\n");
    printf("%d", linked());
    return 0;
}

linked.h

#ifndef LINKED_H_
#define LINKED_H_

int linked(void);

#endif // LINKED_H

linked.c

int linked()
{
    return 5;
}

The IDE shows a warning on the line of linked.h in-between #define LINKED_H_ and int linked(void); which reads

ISO C requires a translation unit to contain at least one declaration

My best guess about what this means is that any header or other C file, if it is in a project, should get used in the main file at least once somewhere. I've tried searching the warning but if this has been answered elsewhere, I'm not able to understand the answer. It seems to me I've used the linked function and so it shouldn't give me this warning. Can anyone explain what's going on?

The program compiles and runs exactly as expected.

like image 467
Addem Avatar asked Jun 02 '19 22:06

Addem


1 Answers

I think the issue is that you don't #include "linked.h" from linked.c. The current linked.c file doesn't have any declarations; it only has one function definition.

To fix this, add this line to linked.c:

#include "linked.h"

I don't know why it says this is an issue with linked.h, but it seems to be quite a coincidence that the line number you pointed out just happens to be the line number of the end of linked.c.

Of course, that may be all this is; a coincidence. So, if that doesn't work, try putting some sort of external declaration in this file. The easiest way to do that is to include a standard header, such as stdio.h. I would still advise you to #include "linked.h" from inside linked.c, though.

like image 87
S.S. Anne Avatar answered Oct 13 '22 22:10

S.S. Anne