Header.h: No such file or directory - c

I'm trying to link my c files and header files in compilation, but get the following error:
fatal error: header1.h: No such file or directory #include <header1.h>
The problem is, I have included the header file using #include <header1.h> in each c file, and compiled using the command gcc header1.h file1.c file2.c main.c -Wall -std=c99 But still gives me the error in every c file. I've included the top of my code from each file below.
header1.h:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct microtreat{
int id;
char user[51];
char text[141];
struct microtreat*next;
}treat;
main.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <header1.h>
file 1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <header1.h>
file 2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <header1.h>
How do I fix this error? Thanks

try
#include "header1.h"
when you use the <> include. the pre processor search's for the header in certain paths but if you want to include file in the directory of your c files you should use include ""
if you want to include header file in other directory you can compile it with the directory which the header is in like so:
gcc some_compilation_flags name_of_c_file_to_compile.c -iquote /path/to/the/header/directory
the flag -iquote say to the compiler to include this directory to find the include file in it

There are two ways of including headers in C/C++.
#include <header.h> looks for the header in the system path
#include "header.h" starts looking from the local folder of the file including the header
Here there is a much more detailed explanation
What is the difference between #include <filename> and #include "filename"?

Related

C macro name must be an identifier

I've created a c project and this is the beginning of the main.c file:
#include <curl/curl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "include/httpdef.h"
//...some code
The httpdef.h beginning is this:
#ifndef httpdef
#define httpdef
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
//definitions
#endif
At the very first line of both files I get the error from the gcc compiler:
macro name must be an identifier
What could be the problem?
EDIT: I realized now that actually the compiler doesn't give any error, it's my vim plugin (YouCOmpleteMe) that generates this error. If I compile everything works and the error doesn't appear

Compiling C with non standard header

I have my main C file:
#if defined(WIN32)
#include <windows.h>
#include <stddef.h>
#endif
#if defined(LINUX)
#include <curses.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(WIN32)
#include <conio.h>
#endif
#include <ctype.h>
#include <a429usbnt.h>
#if defined(WIN32)
#include "genlib.h"
#endif
void main()
{
_open_xpc(1);
}
When I try to compile using this command
gcc -I. -L. test.c -o test
I get the following error: undefined reference to '_open_xpc'.
However if I change the call to the _open_xpc function and instead just
printf("%d", XPC_ERROR_ACTIONCODE);
the program compiles fine and the correct value assigned to the definition of XPC_ERROR_ACTIONCODE is printed out, so the compiler is linking a429usbnt.h but will only recognize defined variables and not the functions.
If you are trying to link against a .lib file with gcc, it seems you need to define a directory with -L and an actual file with -l

Unable to Open an Include file "Protocols.h" using C Compiler

I have a c Program and it was running Perfectly, but now i want to run it in Windows. So i am trying to compile the code in the Windows 7 Operating System, using Turboc3.
When i am compiling, i am getting an error "Unable to open include file Protocols.h"
But the Protocols.h file exists in the Directory. And all the directories are set perfectly in the C compiler.
From .c file it will include one .h file, and from that .h file another .h file is included.
But still i am facing the same issue, Can anyone help me out.
The header i have is like below:
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
//#include <wait.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/types.h>
//#include <ipc.h>
//#include <shm.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define socklen_t int
/* limit values */
#define MAX_TAGS 500
#define MAX_OBJECTS 500
#include "Protocols.h"
If you really set the correct path in you compiler, you could try to hardcode the path like #include "/path/name.h".
If this works, you have some issue with your compiler and maybe not set the path (the right way).
If this does not work it seems, that the file is not existing or broken in some way. In this case: Get a new version of the headerfile and make sure, that it is in the right place.

Issue with organizing a large project with .c and .h files

So I have quite a lot of .c and .h files in my project:
Here are just the headers of the files that are having an issue:
#ifndef OUTPUTUTILITIES_H
#define OUTPUTUTILITIES_H
#include <string.h>
#include <stdio.h>
#include "parser.h" // <== IF I REMOVE THIS include everything works. But I need to include this here
#endif
#ifndef PARSER_H
#define PARSER_H
#include <stdbool.h>
#include <stdio.h>
#include "utilities.h"
#endif
#ifndef UTILITIES_H
#define UTILITIES_H
#include <string.h>
#include <stdio.h>
#include "variableVector.h"
#endif
#ifndef VARIABLEVECTOR_H
#define VARIABLEVECTOR_H
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#endif
Can someone please explain why I cannot #include "parser.h" file in my outputUtilites.h file? And also what are the general principles behind organizing your import .h files?
I also wanted to let you know that in the corresponding .c file I only include the corresponding .h file and nothing else. For example in my parser.c file I ONLY include parser.h and in my ouputUtilities.c file I ONLY include outputUtilities.h file.

Header file with FNM_EXTMATCH

Everything goes well with this statement:
fnmatch(pattern, href, FNM_EXTMATCH);
when I oder the header files as below:
#include <fnmatch.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "link.h"
But the gcc says that FNM_EXTMATCH not defined when I order the head files instead as below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fnmatch.h>
#include "link.h"
So where is the conflict?
FNM_EXTMATCH is a GNU extension. If you wish to use it, put
#define _GNU_SOURCE
at the top of your file (before any #include statements). Note, however, that it will not be portable to non-GNU systems -- those without gcc and glibc.

Resources