Why can't gcc find the include path using -I option? - c

here's my project tree
project
--bin
--source
----include
------foo.h
----main.c
----foo.c
but when I run gcc -I./source/include ./bin/main.exe ./source/*.c it gave me No such file or directory error.
How can I let gcc know where's the include directory?

You need to run gcc with the -o option to set the output location.
gcc -I./source/include -o ./bin/main.exe ./source/*.c

Related

clang to create output directory

I want clang to create a new directory for output files.
For example:
/usr/bin/clang -std=gnu17 -g /path/to/file/test2.c -o /path/to/file/build
Obviously, I keep getting errno=2 (no such file or directory):
ld: can't open output file for writing: /path/to/file/build, errno=2 for architecture x86_64
Is there any way to force a compiler to create a new directory for output files?
I know that you want to use clang itself for creating the directory
but there are other ways
you can use mkdir before you use clang
like that
mkdir path/yourfolder | /usr/bin/clang -std=gnu17 -g /path/to/file/test2.c -o /path/to/file/build

gcc prints no such header file when compile it from upper level folder

I have this command here:
gcc -MD -fno-builtin -nostdinc -fno-stack-protector -Os -g -m32 -I. -c -o boot1lib.o
boot1lib.c
It runs fine if I run this in the folder where boot1lib.o and boot1lib.c located. But when I tried to run it from the upper folder i.e. ./boot/boot1/boot1lib.c
It will shows:
./boot/boot1/boot1lib.c:1:10: fatal error: boot1lib.h: No such file or directory #include <boot1lib.h>
How do I modify the parameters to fix this issue? I am trying to build a makefile in the root folder so I don't have to copy and paste the command every time I tried to compile.
With GCC, #include <file> looks for files only in configured system directories, including those added with switches. #include "file" looks in the directory of the source file it is in.

How to change the following Makefile of LINUX ,so that it works on MACOS as well [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a Makefile on my Linux Pc.I want to build and run the same program on MacOS too . How to edit the following Makefile ?
I want to compile a C program that has a header file of a graphics library EGGXProCALL JAXA "eggx.h".
I already installed Xcode ,XQuartz.
I have installed EGGX file on the following directory .
[Home#/opt/eggx]$
I have a C file in the following directory.
[Home#~/Desktop/development]$
I have checked the followings .
① gcc works fine here [Home#~/Desktop/development]$
② even the sample program to display a digital clock works ,
when the current directory is [Home#/opt/eggx]$
③ PROBLEM
when I tried to build [make] the program from [Home#~/Desktop/development]$ by using the makefile that I had in Linux.
I always get the following error message.
[Home~/Desktop/development]$ sudo make
gcc -c main.c
main.c:7:10: fatal error: 'eggx.h' file not found
#include <eggx.h>
^~~~~~~~
1 error generated.
make: *** [main.o] Error 1
[Home#~/Desktop/development]$
Here is the Makefile
# Makefile
OBJS = main
$(OBJS): $(OBJS).o
# gcc -O2 -Wall $(OBJS).c -o $(OBJS) -I/usr/local/include -L/usr/local/lib64 -leggx -lX11 -lm
gcc $(OBJS).c -o $(OBJS) -I/usr/local/include -L/usr/local/lib64 -leggx -lX11 -lm
$(OBJS).o: $(OBJS).c
gcc -c $(OBJS).c
.PHONY: clean
clean:
rm -f $(OBJS) $(OBJS).o
You must do things in the correct order and not proceed to the next step until you have correctly completed the previous step.
1. Download, and extract the library.
The download is normally done with git clone or scp to copy the source files from somewhere.
The extract (unpack from archive) is normally done with:
tar -xvf eggx-0.93r5.tar
That will normally create a new directory (with the same name as the tar-file but without the .tar extension) like:
eggx-0.93r5
2. Build the library.
Normally you need to change directory into the newly created one and run make. I gave you the Makefile last time so you need to do:
cd eggx-0.93r5
cp MAKEFILEFROMMARK Makefile
make
There should be no errors. If there are errors, you must solve them and then run:
make clean # delete any rubbish from previous failed build
make
3. Install the library.
You normally do this with:
make install
What that actually does depends on the package you are installing, but as a general rule, it will copy the header files and the libraries you just made into a "known" location, like /usr/local or /opt/package. The idea is to make all the files your own code will need available to all users of the computer by "publishing" or installing them to known locations.
4. Work out how to compile a simple C program that uses the library.
You should do the following steps in a completely different directory from where you downloaded the library to - do not mix your code with the library's code.
If your program uses eggx.h like this:
#include "eggx.h"
then you need to find where eggx.h is like this:
find /usr /opt /Users -name eggx.h
If that results in:
/path/to/somewhere/include/eggx.h
that means you must add this to your gcc command to tell the compiler how to find it:
gcc -I/path/to/somewhere/include ...
If your library is called libeggx.a, you need to find that too:
find /usr /opt /Users -name "libegg*a"
If that results in:
/path/to/somewhere/lib/libeggx.a
that means you need to add this to your gcc command to tell the linker where it is and what it is called:
gcc ... -L/path/to/somewhere/lib -leggx
If your program uses X11, you must install XQuartz on a Mac, and add the flags/switches for X11 into your compilation:
gcc ... -I /opt/X11/include -L /opt/X11/lib -lx11 ...
So, putting all that together, if your program is called program.c, you will compile and link with:
gcc program.c -o program -I/path/to/somewhere/include -I /opt/X11/include -L /opt/X11/lib -lx11 -L/path/to/somewhere/lib -leggx
and then run with:
./program
5. Make a Makefile that enshrines what you learned at (4).
That might look something like this:
EGGINC = -I /path/to/somewhere/include
EGGLIB = -L /path/to/somewhere/lib -leggx
X11INC = -I /opt/X11/include
X11LIB = -L /opt/X11/lib -lx11
$(OBJS): $(OBJS).o
gcc $(OBJS).c -o $(OBJS) $(EGGLIB) $(X11LIB)
$(OBJS).o: $(OBJS).c
gcc -I/usr/local/include $(EGGINC) $(X11INC) -c $(OBJS).c
You build your program in two steps:
Build the source file into an object file
Link the object file with libraries to create the final executable program
The preprocessor (which handles #include directives) is part of the building of the object file. So all flags that are needed for creation of the object files (like the -I option) should be present there and only there.
So the two rules could be changed as follows:
$(OBJS): $(OBJS).o
gcc $(OBJS).c -o $(OBJS) -L/usr/local/lib64 -leggx -lX11 -lm
$(OBJS).o: $(OBJS).c
gcc -I/usr/local/include -c $(OBJS).c
Of course that assumes that the EGGX library was installed in /usr/local.

Using a static library in C

I found a useful library on github for my project, after building this later I tried to use some predefined function on it. I couldn't compile my project because there is some header file missing like this one :
In file included from main.c:2:0:
ptask.h:11:19: fatal error: ptime.h: No such file or directory
I compiled my project using this command :
gcc main.c -L. -lptask
This is all the files in project folder :
libptask.a main.c ptask.h
This is the library content:
$ ar -t libptask.a
pbarrier.c.o
pmutex.c.o
ptask.c.o
ptime.c.o
rtmode.c.o
tstat.c.o
libdl.c.o
dle_timer.c.o
calibrate.c.o
Do I need to add all the headers of this files or just link the lib when compiling ?
Your main.c #include-s ptask.h which in turn #include-s ptime.h. Having compiled static libs alone is not enough (that's the linker's job), you still need to have all used header files (which is the compiler's job), both the ones you use and their dependencies, recursively applicable.
Normally you need to be sure that the header files are in your "include path", something that a lot of compilers define with -I as a command-line option. You'll need to include the source directory of that library, or if it has a make install option, then the place where they got installed.
regarding:
gcc main.c -L. -lptask
this is performing the compile step and the link step in one command.
It is also not enabling the warnings, which should always be enabled during the compile step.
Suggest something similar to the following to compile
gcc -Wall -Wextra -Wconversion -pedantic -std=gnu11 -g -c main.c -o main.o -I.
and when you have fixed all the warnings, then use something similar to the following to link
gcc main.o -o main -L. -lptask

Fatal error: modbus.h: No such file or directory

I'm expecting a lot of difficulties to make my program working with the library libmodbus on Linux.
I've installed libmodbus with the command sudo make install and after make but the problem is when I want to link the library in my C program.
My Makefile for now is like:
all: test
test: main.o com.o
gcc main.o com.o -o test
main.o: main.c
gcc -c main.c -o main.o
com.o: com.c
gcc -c com.c -Wl,-rpath=/usr/local/lib -Wl,LIBDIR -o com.o
clean:
rm -rf *o test
In my file com.c I include the file modbus.h like this:
#include <modbus.h>
And I always get the error:
fatal error: modbus.h: No such file or directory.
If it can help when I did make install, the code return me this:
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
add LIBDIR to the `LD_LIBRARY_PATH' environment variable during execution
add LIBDIR to the `LD_RUN_PATH' environment variable during linking
use the `-Wl,-rpath -Wl,LIBDIR' linker flag
have your system administrator add LIBDIR to `/etc/ld.so.conf'
It seems like the modbus.h is not in the standard include directory.
You should to add the -I/<includes_path> flag to gcc options.
I suppose here:
gcc -I/<include_dir_path> -c com.c -Wl,-rpath=/usr/local/lib -Wl,LIBDIR -o com.o

Resources