0

I have defined an enum in a header file, global.h:

Typedef enum
{
    ELEMENT1,
    ELEMENT2,
    ELEMENT3
}e_element;

I have a second file using the enum as a function parameter. file2.c

#include global.h
#include file2.h
Function(e_element x)
{
Body…
}

The prototype is in: file2.h

Function(e_element x);

The compiler doesn’t know e_element in file2.h. I have tried putting the #include for global.h in both file2.c and file2.h, but it still doesn’t see it. I would put the enum in file2.h, except that it is used by several other files, so if I move it the problem will just show up somewhere else. How can I get the file2.h prototype to see e_element?

3
  • It's typedef, not Typedef. When I fix that, and add quotes around the header files in #include, your code compiles. Commented Jul 11, 2016 at 22:11
  • Fix your basic syntax errors, then see if you still have the problem. Commented Jul 11, 2016 at 22:13
  • Your code has a lot of typographic errors. I assume this is not your real code? Please post your real code, not some kind of pseudo-code. Or, OTOH, if this is really your code, you should first read a good textbook about C. Commented Jul 11, 2016 at 22:14

1 Answer 1

6

This worked for me:

global.h

#ifndef GLOBAL_H
#define GLOBAL_H

typedef enum
{
    ELEMENT1,
    ELEMENT2,
    ELEMENT3
}e_element;

#endif

file2.h

#ifndef FILE_2_H
#define FILE_2_H

#include "global.h"
int test(e_element);

#endif

file2.c

#include "file2.h"

int test(e_element x)
{
    return x == ELEMENT1;
}

int main() {
    return 0;
}

edit: #ifndef is conventionally used as a "header guard". It prevents a header file from being included multiple times by the preprocessor, which prevents things from being defined multiple times. It works by checking if a unique symbol has been defined before. If it has not, then it immediately defines it and then continues with the header file until #endif. If the symbol was already defined then it skips the guarded code completely, preventing multiple definitions. An example of multiple definitions would be if the same header file was included in a source and a header that the source also includes.

See this link for more information.

Sign up to request clarification or add additional context in comments.

7 Comments

You should explain the purpose of all those #ifndef, maybe with also include a link to a tutorial on header guards.
Sure. I added some more info and a link.
Sorry. The code above was done in a hurry. The actual code says "typedef" with a small t (auto-correct), and the #include files are in quotes.
global.h #ifndef SOURCES_GLOBAL_H_ #define SOURCES_GLOBAL_H_ //controller enum typedef enum { ELEMENT1, ELEMENT2, ELEMENT3 }e_controllers; #endif /* SOURCES_GLOBAL_H_ */ file1.h // I get the error in this file void setSequenceStatus(e_controllers controller, bool value); file1.c #include "file1.h" #include "global.h" void setSequenceStatus(e_controllers controller, bool value) { if(controller == FF40_MMS_DSRC_CAS) { sequence_status_FF40 = value; } }
From what you wrote it doesn't look like file1.h is including global.h. It must because it is using an enum from globals.h.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.