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

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.

Related

Header.h: No such file or directory

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"?

Weird behavior with defines for usleep() and inet_aton()

I have created two modules: files.h and connection.h.
files.h is included in connection.h.
files.h uses usleep() function and connection.h uses inet_aton() function at some point of the respective .c files. Those functions need the following defines:
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 1
#define _GNU_SOURCE
So, as files.h is included in connection.h, I thought I could just write those defines in files.h but when I compile I get the following error:
connection.c:23:6: error: implicit declaration of function ‘inet_aton’
So I decided to try to write those defines in connection.h instead of files.h just to compile and get the following error:
files.c:298:3: error: implicit declaration of function ‘usleep’
At this point, my next option was writing the defines in the respective .c files to solve this. But instead, I got this error while compiling:
files.c:302:3: error: implicit declaration of function ‘usleep’
connection.c:23:6: error: implicit declaration of function ‘inet_aton’
I don't understand what's the issue. How can I use both functions?
files.h
#ifndef _FILES_H_
#define _FILES_H_
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
// ...
#endif
connection.h
#ifndef _CONNECTION_H_
#define _CONNECTION_H_
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 1
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "files.h"
#include <ctype.h>
#include <pthread.h>
// ...
#endif
This seems to be an ordering issue..
When you include in this way:
#include <unistd.h> // other includes as well
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 1
#define _GNU_SOURCE
The header files are brought in without the override defines.
However, the purpose of the defines is to change what functions/signatures are imported from the headers!
And since C is very linear, the order matters..
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 1
#define _GNU_SOURCE
#include <unistd.h> // other includes as well
Basically, define your requests first, before you include any standard header file.
These must be included before the FIRST TIME the header is seen -
So if 'connection.c' includes something before connection.h, then the defines may not be present for the first include of <unistd.h>
I finally solved this issue writing the defines above the includes thanks to your suggestions BUT in the .c files. Still not working if I write the defines anywhere in the .h files.
files.c
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 1
#define _GNU_SOURCE
#include "files.h"
connection.c
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 1
#define _GNU_SOURCE
#include "connection.h"

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

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.

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