0

I have a file header.h which has include guards:

#ifndef HEADER_H
#define HEADER_H

typedef enum {
    ENUM_1,
    ENUM_2,
} enumerator;

typedef struct {
    uint8_t struct_field_1;
    bool struct_field_2;
} structName

void functionName(uint8_t arg1, uint32_t arg2);

#endif 

header.h is included in file1.h and file2.h (#include <header.h>)

Then foo.c includes file1.h and file2.h as a relative path

#include "../../../file1.h"
#include "../../../file2.h"

I'm getting compile errors such as "previous declaration of 'structName' was here" and "conflicting types for 'enumerator'". Why is this happening even with include guards and how do I fix it?

header.h has not been included in any C files. header.h also does not contain function definitions (only declarations, and some enums)

How to avoid "multiple definition" error for global constants? mentions separating definitions and declarations but I have already done that

Jan Wang
  • 1
  • 1
  • What is the exact command you're using to compile? – dbush Apr 03 '23 at 21:31
  • Does `file1.h` or `file2.h` define `structName` too? – Ted Lyngmo Apr 03 '23 at 21:54
  • 2
    Note that you're missing a ; after the typedef line, which could actually cause errors and sometimes trigger an incorrect error for the real source of the problem. – Wes Hardaker Apr 03 '23 at 22:00
  • It's a Makefile with elf-srcs += foo.c elf-srcs += file1.c elf-srcs += file2.c elf-deps += directory to source file of header.h And structName are not defined in file1.h or file2.h Thanks, the ; after structName was a typo in the question, it is present in the actual file – Jan Wang Apr 03 '23 at 22:09
  • 3
    I would reopen this, if you provide a [mre] with all your relevant files and double check the ";" problem mentioned above. It seems unlikely that you get the mentioned error with that missing. – Yunnosch Apr 03 '23 at 22:09
  • 1
    I do not trust paths in includes, but it seems unlikely that they can cause your problem. Still, could you do your MRE without them? I.e. build with suitably set up include search path? Just to eliminate that uncertainty from the analysis. – Yunnosch Apr 03 '23 at 22:10
  • Don't describe the makefile, **show it**. – dbush Apr 03 '23 at 22:25
  • Note the discussion in [What are the benefits of a relative path such as `"../include/header.h"` for a header?](https://stackoverflow.com/q/597318/15168) – Jonathan Leffler Apr 04 '23 at 04:49

0 Answers0