How to include an archive file in your c code - c

I'm making an archive file, which I am then trying to include in my code, but when I try to #include 'libutils.h', I get an error:
src/indexer.h:8:10: fatal error: 'libutils.h' file not found
#include "libutils.h"
my make command is:
gcc -g -std=c11 -Wall -pedantic -o indexer src/indexer.c -L. -lutils.a
my file structure is:
Indexer/
libsutil.a obj/ src/ makefile
obj/
web.o list.o hashtable.o //These are the files in the archive file
src/
web.c web.h list.c list.h hashtable.c hashtable.h indexer.c indexer.h

You don't need to include anything, remove the
#include "libutils.h"
from the .c file, and your compilation command will be
gcc -g -std=c11 -Wall -pedantic -o indexer src/indexer.c -L. -lutils

Related

Compilation error on linux server (C project)

I need some help.
I worked on a C project locally and it ran perfect with no issues at all.
Then I moved my whole project files to a linux server (using Bitwise) and ran it using the following command:
gcc -g -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG main.c map.c map.h utilities.c
utilities.h election.c election.h extended_map.c extended_map.h test_utilities.h -o outmap
and again everything worked as expected.
Now, I want to replace my version of test_utilities.h with the version saved on that server, so I opened main.c (which is the only file to include test_utilities.h and replaced:
#include "test_utilities.h"
with
#include "~mtm/public/1920b/ex1/test_utilities.h"
But the terminal shows me the following error:
gcc: error: test_utilities.h: No such file or directory
-bash-4.2$
As suggested I changed it to
gcc -g -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG main.c map.c map.h utilities.c utilities.h election.c election.h extended_map.c extended_map.h test_utilities.h -o outmap -I ~mtm/public/1920b/ex1/
But still I get the following:
gcc: error: test_utilities.h: No such file or directory
update2: (I was requested to remove .h files so now I got)
gcc -g -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG main.c map.c utilities.c election.c extended_map.c -o outmap
Writing ~mtm to refer to the home directory of user mtm is a shortcut that your shell understands. It isn't something that the C preprocessor understands. So you'll have to spell it out as /home/mtm (or wherever mtm's home directory is located) instead of ~mtm.
That said, a better way would be to just leave it as "test_utilities.h" and instead adjust the include path of the compiler (specified via -I when invoking the compiler) to include ~mtm/public/1920b/ex1/.
You also shouldn't specfiy test_utilities.h as an argument to the compiler. In fact none of the header files should be passed as arguments to the compiler.

"curses.h: No such file or directory" even after installing into Cygwin

I'm working on a C clone of the 2048 game, using curses.h for the UI. When trying to compile it with Cygwin using the make commanad, I get following message:
PS D:\C\ps3> make all
gcc -std=c11 -Wall -Werror -g -c main.c -lm -lcurses -o main.o
main.c:4:20: fatal error: curses.h: No such file or directory
#include <curses.h>
^
compilation terminated.
make: *** [Makefile:13: main.o] Error 1
So I ran the setup again, looking for any package that has "curses" in it's name and installed it, added my /bin folder to the PATH variable but it didn't help.
I'm working on a 64-bit Win10 and trying to compile the program with Cygwin's terminal, using a Makefile. file. I've tried reinstalling the packages with curses in their name multiple times with no help.
Part of my Makefile:
CC=gcc
CFLAGS=-std=c11 -Wall -Werror -g
LDLIBS=-lm -lcurses
OUTPUT=game
# targets
all: $(OUTPUT)
$(OUTPUT): k.o hof.o main.o
$(CC) $(CFLAGS) k.o hof.o main.o $(LDLIBS) -o $(OUTPUT)
main.o: main.c
$(CC) $(CFLAGS) -c main.c $(LDLIBS) -o main.o
The line in main.c the error is pointing to:
#include "hof.h"
#include "k.h"
#include "ui.h"
#include <curses.h>
The header file would be in libncurses-devel (perhaps overlooked). Here's a screenshot showing the "curses" packages which I have in my local repository:

Error "no such file or directory" when compiling with Makefile

I am trying to use a Makefile to compile a couple of .c files and a couple of custom .h files to make a single executable file. however during the compilation im receiving the error that the error "No such file or directory".
Here is my Makefile, is there a possible flaw in the logic as to why the header files are being forgotten during compilation?
The error occurs on line 21 of the compilation during the $(BIN)main.o rule.
This is the compilation time error:
socs#socsvm:~/Desktop/Programs/CIS2520/root$ make
gcc -Wall -g -std=c99 -c -Iinclude -c ./src/main.c
./src/main.c:4:23: fatal error: structDefns: No such file or directory
#include "structDefns"
^
compilation terminated.
makefile:21: recipe for target 'bin/main.o' failed
make: *** [bin/main.o] Error 1
socs#socsvm:~/Desktop/Programs/CIS2520/root$
This is the makefile:
CC = gcc
CFLAGS = -Wall -g -std=c99 -Iinclude
BIN = ./bin/
SRC = ./src/
INC = ./include/
$(BIN)main: $(BIN)main.o $(BIN)book.o $(BIN)store.o $(BIN)boardGame.o
$(CC) -o $(BIN)main $(BIN)main.o $(BIN)book.o $(BIN)store.o
$(BIN)boardGame.o
$(BIN)book.o: $(SRC)book.c $(INC)structDefns.h $(INC)funcDefns.h
$(CC) $(CFLAGS) -c $(SRC)book.c
$(BIN)boardGame.o: $(SRC)boardGame.c $(INC)structDefns.h $(INC)funcDefns.h
$(CC) $(CFLAGS) -c $(SRC)boardGame.c
$(BIN)store.o: $(SRC)store.c $(INC)structDefns.h $(INC)funcDefns.h
$(CC) $(CFLAGS) -c $(SRC)store.c
$(BIN)main.o: $(SRC)main.c $(INC)structDefns.h $(INC)funcDefns.h
$(CC) $(CFLAGS) -c $(SRC)main.c
As for the file directory, the make file is outside of 3 folders, bin, src, and include. bin is where I want the object and executable to go, src is where the .c files live, and the .h files live in the include folder.
From the compilation command, the error and the source line given by the error:
gcc -Wall -g -std=c99 -c ./src/main.c
./src/main.c:4:23: fatal error: structDefns: No such file or directory
#include "structDefns"
it shows that gcc can't find the include file. It expects it in the same directory as main, but your Makefile shows it lives in ./include (or even ../include relative to main).
Use the -I flag for gcc together with the proper path. You can set this alongside the other CFLAGS:
CFLAGS = -Wall -g -std=c99 -Iinclude
should do it.
(I may be slightly mistaken with the path. If it still fails, try variations of -I./include or the relative path to main: -I../include. But it should be the path from the directory where you issue the command, not relative to main.c.)

C - programming a makefile and how they work

So given three files:
main.h
#include <stdio.h>
void printFunc(*char);
main.c
#include "main.h"
int main(){
printFunc("Hello World\n");
return 0;
}
printFunc.c
#include "main.h"
void printFunc(char *string){
printf("%s", string);
return;
}
You can compile using gcc on a linux machine as follows:
gcc -g -Wall -c file1.c //compile but do not link file
gcc -g -Wall -c file2.c //same
gcc file1.o file2.o -o main //executable "main"
or
gcc -g -Wall file1.c file2.c -o main
But I am concerned with how the header file gets included. I came across this when I was working on creating a "makefile" when I noticed that some tutorials will do something like this:
main : main.o printFunc.o
gcc -o main main.o printFunc.o
main.o : main.c
gcc -g -Wall -c -o main.o main.c
printFunc.o : printFunc.c
gcc -g -Wall -c -o printFunc.o printFunc.c
and others will include the header file as a dependent with:
main : main.o printFunc.o //main.h EDIT
//commands
main.o : printFunc.o main.h
//commands
Finally:
So, is it necessary to include the header file as a dependent to the executable? When does the include file get placed within the sources?
And also one could use this command:
executableName : dependencies.o //and a header file?
gcc -g -Wall -o executableSource.c
Which could be done with the line:
gcc -g -Wall -o executableName executableSource.c
Will the second command be run but the first is shorthand notation?
And finally, I thought the "-o" command was "send output to", if you will. If that way, it seems intuitive to run the command like:
gcc compileThisFile andSendOutputTo thisExecutableFile
gcc someSource.c -o executableFile
But with the notation listed above its more like:
gcc sendOutputTo thisExecutableFile fromThisSource
Is that correct?
main : main.o printFun.o main.h is definitely wrong. That's saying that the header is a prerequisite for linking. A header is a prerequisite for compilation.
Assuming what you really meant was to specify the header as a compilation dependency (e.g. printFunc.o : printFunc.c printFunc.h), this means that if the header changes, the object file will be automatically regenerated. If you don't, then it won't.
A dependency in Makefile is saying that whenever any of the listed files change, run the command again. It does not mean that the listed file will be included into the compilation or linking. You still need the regular #include "main.h" in your sources.
Thus, this works too:
printFunc.o : printFunc.c someReadmeFile.txt
gcc -g -Wall -c -o printFunc.o printFunc.c
Whenever printFunc.c or someReadmeFile.txt is updated, gcc -g -Wall .... will be executed again.
I hope it's clearer now.

trouble using make and #include

I have a project that has three files. The main file is called login.c.
I want to #include my other two files using make, but I'm having trouble doing so.
Thanks in advance for any advice!!
here is my makefile:
objects = login.o cipher.o linked.o
coptions = -Wall -g -ggdb
loginTest: ${objects}
gcc ${coptions} -o loginTest ${objects}
login.o: login.c cipher.h linked.h
gcc -c ${coptions} login.c
cipher.o: cipher.c cipher.h
gcc -c ${coptions} cipher.c
linked.o: linked.c linked.h
gcc -c ${coptions} linked.c
the error I get:
make: *** No rule to make target `cipher.h', needed by `login.o'. Stop.
the files in my current working directory:
cipher.c
linked.c
linked.o
login.c
makefile
make can not automatically create *.h.
You need to create it and #include that *.h file by yourself.

Resources