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
Related
I am having issues linking a library (termbox) when compiling. I get the error:
make: *** No rule to make target `termbox.h', needed by `test.o'. Stop.
Makefile:
edit: test.o
gcc -Wall -o edit test.o
test.o: test.c termbox/src/termbox.h
gcc -Wall -c test.c -ltermbox/src
Include:
#include "termbox/src/termbox.h"
I have also tried using the compiled library but ran into similar issues. Do I have to use some sort of combination of specifying the header file and the location of the compiled library?
The directory of my termbox folder is in the same directory as test.c.
Thanks!
You have managed to compile and include the header file for the library, but you did not yet tell the compiler where the code (definitions) are - i.e. you did not tell it to link in the library yet.
You will need to do that next, this is done in a similar way to telling the linker what files to link, but with some extra syntax. It appears to be a static library (.a suffix) so you can link like this:
test.o: test.c termbox/src/termbox.h
gcc -Wall -c test.c -Itermbox/src -Lsrc -ltermbox
Where -L... specifies where libraries can be found and -l... specifies the library name to link to minus the lib prefix and the .a or .so suffix. Also note that order is important, so leave the library linkage at the end.
More on library linking order here
UPDATE
Sorry I added the linking to the wrong line! - here is the updated answer:
# The linker stage
edit: test.o
gcc -Wall -o edit test.o -Lsrc -ltermbox
# Compile stage
test.o: test.c termbox/src/termbox.h
gcc -Wall -c test.c -ltermbox/src
I'm trying to compile my C code but I need to tell the GCC compiler where two file are.
The two files are located here
/usr/local/ssl/include/
/usr/local/ssl/lib/
I want to add this to my gcc -o file file.c so that my program can work.
In gcc, the -I option is used for adding a directory to the set of directories to search for header files, and the -L option is used for adding a directory to the set of directories to search for libraries. Since you're not explicitly linking in any libraries, you shouldn't need the -L option in this case.
gcc -I/usr/local/ssl/include -o file file.c
If you were linking in libraries, something like the following format should work, assuming that file.c calls a function in libmyLib.a:
gcc -I/usr/local/ssl/include -o file file.c -L/path/to/my/library -lmyLib
See this question for more details regarding library linking order.
Sorry. I think this question would be very easy to you guys.
I have two c files and one h file, I put those two .c files stack.c and main.c and one .h file stack.h inside a folder named "test" at Desktop.
So they are in C:\Users\user\Desktop\test
However when i try to test this code by writing
gcc -c stack.c sq_main.c -l stack.h
It continuously shows "unkown type name ..."
I think the header file is not included into those two .c files.
Actually I wrote the code
#include "stack.h"
Inside stack.c and main.c
Can anyone tell me how to include header file properly?
You are using GCC wrongly. I guess you are on Linux (or on something emulating it like MinGW ...)
If you insist on giving several commands in a terminal, you'll need to run
gcc -Wall -Wextra -g -c stack.c
gcc -Wall -Wextra -g -c sq_main.c
these two commands are building object files stack.o & sq_main.o (from stack.c & the #include-d stack.h, and sq_main.c & the #include-d stack.h, respectively). The options -Wall -Wextra are asking for all warnings and some extra warnings. The -g option asks for debugging information. The -c option asks for compiling only. Assuming that they are enough for your program, you need to link these object files to make an executable:
gcc -g stack.o sq_main.o -o myprogram
You might need to add -Iinclude-directory options to the compiling commands (the first two), and you might need to add -Llibrary-directory and -llibrary-name to the linking command. Order of arguments to gcc matters a lot. You could also add -H to ask the compiler to show which files are included. And GCC has a lot of other options. Read the Invoking GCC chapter of its documentation.
The .o suffix might be .obj on most Windows systems. You might also need myprogram.exe instead of myprogram. I never used Windows so I cannot help more.
In practice, you should use GNU make and write some Makefile; this answer might inspire you.
So I'm having trouble getting even a simple Makefile to work. Heres what I have:
proj : driver.o
icc -g -O3 -openmp driver.o -o proj
driver.o : driver.c driver.h
icc -g -O3 -openmp driver.c
I feel like it's pretty straight forward. Proj only depends on driver.o which in turn depends on driver.c and driver.h. When run, the compiler fails with 'could not open source file "driver.h" ' at the include within the driver.c file. What am I missing?
You should give icc a -I. option to get it to look for include files in the current directory.
icc -I. -c -g -O3 -openmp driver.c
(I took the liberty of also adding the -c flag to prevent linking.)
Assuming you run make from the directory where all source files and headers reside, make sure you use quotes in your include directive:
#include "driver.h"
...rather than:
#include <driver.h>
The latter will search the system include path (and you will have to add the current directory to that path as larsmans suggested).
Is it in the correct folder? also the way you're compiling you might have to add -c to the command line to compile driver.o instead of trying to create a complete exectuable (only used to gcc so this might not be required).
Other than that (and the possible addition of the -I flag larsmans mentioned I can't see any other issue.
This is the header file and its C file:
cs50.h and
cs50.c
Now I use them in the following example http://www.paste.ubuntu.com/576370/ — which is no longer available.
I already put the header file in /usr/bin/include or something like that and when I try to compile my code using gcc -o xxx xxx.c, it doesn't work, so tried to fix this and the following way worked: http://www.paste.ubuntu.com/576371/ — which is no longer available.
Now I want to do something to make the 'make' command work as the gcc does.
What do I need to do?
The following was the old topic:
I was using gcc command to
compile C programs but after a period
of time I got a problem. I need
to compile a new header file and use
it as a library.
The header file called cs50.h.
so after doing it and it's ok I can
compile using the following
gcc -o xxx xxx.c -lcs50
It works but now I want to use 'make'
command and I can't get it to work.
It just don't compile the header file
and library as gcc was before I edit
it to accept the cs50 library.
So now I want to add to the 'make'
command the following: -lcs50
Can anyone help me in this please?
Near the top of your Makefile, add the line:
LDLIBS = -lcs50
If you are using Make's default (implicit) rules for the building, then that is all you need to do. If you are using explicit rules, you will need to add $(LDLIBS) to your explicit rules.
If there is no Makefile, then make is using default rules, and you can either just create a makefile with
echo LDLIBS = -lcs50 > Makefile
or tell make to link with certain libraries by specifying the required libraries in LDLIBS in the environment. For example, if you are using a sh-derived shell (anything other than csh or tcsh) you can do:
LDLIBS=-lcs50 make target
If you are using a csh variant, you can do:
env LDLIBS=-lcs50 make target
or just do (again, for non-csh shells)
export LDLIBS=-lcs50
before running make. (For csh variants, do setenv LDLIBS -lcs50)
You can use below “make” command to link library and include header directories,
make <.c or .cpp source filename_without_extension> LDLIBS="-l<lib1> -l<lib2>"
suppose you have server.cpp file to compile using make command,
make server LDLIBS="-lcpprest -lpthread -lssl -lcrypto" LDFLAGS="-L/usr/lib/" CXXFLAGS="-I/usr/include/"
Output will expand the compilation command as,
g++ -I/usr/include/ -L/usr/lib/ server.cpp -lcpprest -lpthread -lssl -lcrypto -o server
Did you forget that you have to tell gcc in what directory the CS50 library is located?
gcc … -L/directory/for/cs50_library -lcs50