0

I'm trying to use a header to declare some Macros for the pre-processor and use them in the code file.

That's my header : error.h

#ifndef PROJET_MODULE_H 
#define PROJET_MODULE_H

#define TESTMACRO 5

#endif

and the code file : error.c

#include <error.h>
#include <stdio.h>

int main(){
   printf("%d\n", TESTMACRO);
   return 0;
}

And I get this error :

‘TESTMACRO’ undeclared (first use in this function)

I've tried to compile doing :

gcc error.c -o error

and

gcc error.h error.c -o error

Both gave me the error.. Any help appreciated, thanks

Rémy.W
  • 225
  • 2
  • 9
  • 5
    Try `#include "error.h"` – suspectus Sep 26 '19 at 08:12
  • That solved it, thank you – Rémy.W Sep 26 '19 at 08:13
  • 1
    It's quite likely `error.h` is the name of system header that is found elsewhere. Not your header – StoryTeller - Unslander Monica Sep 26 '19 at 08:13
  • 1
    Additional remarks: Your command `gcc error.h error.c -o error` is wrong. You don't have to specify a header file on the command line. (Except if you want to do something very unusual that does not fit the conventions.) If you want to see what gets included you can run `gcc -E -C error.c -o error.i` and check the resulting file `error.i`. `#include "file.h"` is for your project's include files, `#include ` is for system includes, normally from `/usr/include` or some compiler specific location. – Bodo Sep 26 '19 at 08:20
  • Possible duplicate of [What is the difference between #include and #include "filename"?](https://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename) – klutt Sep 26 '19 at 10:08

2 Answers2

1

To sum up everything said in comment :
To include non-system headers, you have to use " and not <>
The resquest was solved changing #include <error.h> to #include "error.h"

cocool97
  • 1,201
  • 1
  • 10
  • 22
1

To include System header file you can use <> or ""

To include Custom header file you should use "error.h" or "absolute path of error.h"

If you still want to include you custom header file using <> you should compile using following command.

gcc error.c -I <path of folder in which error.h resides> -o error

e.g if error.h is in /user/testuser/include/error.h then

gcc error.c -I /user/testuser/include/ -o error

Mohit Jain
  • 270
  • 2
  • 6