gcc doesn't include c header file json-c - c

On Macbook, I am making something with json-c(https://github.com/json-c/json-c)
gcc a.c
a.c:1:10: fatal error: 'json.h' file not found
#include "json.h"
^
1 error generated.`>
when I try to compile it,
it prints out error, but I have json.h file in include file.
> cd /usr/local/include/json-c/
> ls
arraylist.h json_config.h json_tokener.h
bits.h json_inttypes.h json_util.h
debug.h json_object.h linkhash.h
json.h json_object_iterator.h printbuf.h
json_c_version.h json_object_private.h random_seed.h
/usr/include/json-c/json.h is definitely exists

It's likely that the compiler doesn't have the json-c subdirectory in its include path.
With luck, you can just add that to your inclusion:
#include "json-c/json.h"
This only works if that header is stand-alone, i.e. it doesn't reference any further headers in json-c/.
If that fails you have to tell the compiler when you invoke it:
$ gcc -I/usr/local/include/json-c/ a.c

Related

Compiling C file which includes other header files

I am trying to compile a file Mv.c like - g++ microtime.c Mv.c
It gives an error - v.c:2:10: fatal error: microtime.h: No such file or directory 2 | #include <microtime.h>
My current directory has both microtime.h and microtime.c and Mv.c includes microtime.h
I am not sure how to go about compiling it.
Since my main program Mv.c is using microtime.h do I need to compile microtime.c first and pass it as an argument to g++?
I got it compiled by using the command g++ -I. Mv.c microtime.o
where microtime.o I generated using g++ -c microtime.c
I am not sure why this command works and why we need to specify the extra -I. option when I have already created the compiled object file microtime.o
If you write #include <microtime.h>, the compiler uses its list of system directories to look for the header file. By the option -I you add a directory to this list, and the compiler is happy.
To include project-local header files, use #include "microtime.h".
Read the chapter "Directory Options" in GCC's documentation to learn more.

gcc include source files in subdirectory relative to each other in c

I'm having issues compiling some c code.
I have a code structure like the following
main.c
src
line
line.h
line.c
Available here: https://github.com/Vafilor/c_includes
main.c includes #include "src/line/line.h"
src/line/line.c includes #include "src/line/line.h"
When I try to compile this with gcc main.c src/line/line.c on ubuntu 20.4
I get this error
src/line/line.c:1:10: fatal error: src/line/line.h: No such file or directory
1 | #include "src/line/line.h"
| ^~~~~~~~~~~~~~~~~
compilation terminated.
How can I get this to compile?
Additional info:
I've been looking at the bluez code at https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/
and they have something similar with
client/main.c - includes shell.h
src/shared/shell.c - includes shell.h
src/shared/shell.h
so I'm trying to figure out how it works for their code base.

makefile fatal error: no such file or directory, but the file is in current directory

I'm trying to make a very simple makefile for some .c and .h files, all of which are in the current directory. I will admit I don't fully understand makefile; here's what I have so far:
prog3 : prog3.c prog3.h lib.o
gcc -c prog3.c
lib.o : lib.c lib.h
gcc -c lib.c
When I use the command make I get this message:
prog3.c:5:17: fatal error: lib.c: No such file or directory
compilation terminated.
makefile:2: recipe for target 'prog3' failed
make: *** [prog3] Error 1
The file lib.c, however, is in the same directory as all the others (prog3.c, prog3.h, lib.h).
I have found a lot of questions about this particular error, but none of them are about a file that is in the PWD. What am I doing wrong?
Your makefile looks fine. The basic structure of a makefile contains this pattern:
<build object> : <list of dependencies>
command to execute to build the object if dependencies have changed
...
command to execute to build the object if dependencies have changed
Your makefile is trying to compile prog3.c, which is what it is supposed to do. You have a compile error in prog3.c and it looks like you are trying to #include "lib.c", but my guess is that your are doing this on line 15:
#include <lib.c>
If this is what you are doing, this is wrong. C implementation files should never include other .c files (there are rare exceptions). You should only include .h header files. The linker will bring in the required functions from lib.c based on compiling against lib.h.
Also note that include files with brackets <> are for system headers only. User defined header files should be included using quotes:
#include "lib.h"
If this does not fix your compile error, edit your question to provide the prog3.c and lib.c files if you want those checked.

header.h: No such file or directory even though source and header are in same directory

I have made a header and a source but I don't know how to link them up. I looked it up on the web but the commands provided didn't work (or I wouldn't be here :) ).
To compile it (if you use GCC):
Header:
$ gcc -c whatever.h -o whatever.o
Source:
$ gcc -c sample.c -o sample.o
To link the files to create an executable file:
$ gcc sample.o whatever.o -o sample
What did I do wrong. I am using geany for writing (compile error is here) but the commands are executed on a terminal in the same directory. can anybody give me the build commands for geany so whenever I want to include a header I can just compile and run?
Good and the right way would be to
sample.c
#include "header.h"
and compile
gcc sample.c -o ob
Thumb Rule:
header files [.h] are for #includeing
source files [.c] are for compiling and linking together to create the executable.
Once you've #included your header file in a .c file, there's no need to compile the header file and produce an object file.
FYI, you can check the effect of #include-ing the header file by running
gcc -E sample.c
and hope you'll understand why you need not compile and link the header file separately.
EDIT:
if you have a sample.c and whatever.h, to produce and run the binary, simply do
#include "whatever.h" in the top of sample.c
gcc -o sample sample.c
./sample
if you include header file by:
#include <header.h>
it will give this error.
Instead you can write as given below:
#include "header.h"

GCC unable to find header file in a included library

I am trying to include a library file named libmathematica.a in gcc so it gets linked in the executable example.
I attempt to do this with gcc main.c libmathematica.a -o example
Note: I have to do this with gcc, as ld won't link it properly with the correct system libraries
But I get: fatal error: mathematica.h: No such file or directory ,which is odd because mathematica.h is in the library.
Can you help?
A header file cannot be in the library. It has to be present at a certain location and you have to specify that location with the -I compiler flag:
gcc -I/path/to/mathematica/include main.c libmathematica.a -o example
If the header file is in the directory where the main.c is or in a subdirectory, then be sure that you use quotes and not angle brackets in the #include directive.
The issue would be in your source file. If the mathematica.h is in the system includes directory then you would use #include <mathematica.h> and if it was in some local directory then you would use something like #include "libs/mathematica.h".
Try adding to the gcc call - an option like
-I/Full/Path/To/The/Directory/Where/the/desired/header/resides
For example:
gcc -I/usr/include/mathematica -lmathematica -o example main.c

Resources