trouble using make and #include - c

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.

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:

How do I create Makefile for this

Here is my header file
#include <stdio.h>
#include <stdlib.h>
#include "gd.h"
#include "gdfontmb.h"
#include "gdfontl.h"
#include "gdfontg.h"
When I run this program I usually type 'gcc -o test test.o -lm -lpng -lgd'
It works fine for only one .c file, but this is just for testing. I want to link this with others c file in my project (Actually I'm really new to use gd.h)
Here is my Makefile (but It isn't work!!)
ifeq ($(OSTYPE),WINDOWS)
EXECEXT =.exe
COMP =__MINGCC__
PLATFORM =mingw
else
EXECEXT =
COMP =__GCC__
PLATFORM =linux
endif
EXECUTABLES= test$(EXECEXT)
all : $(EXECUTABLES)
test.o : test.c
gcc -c test.c
test$(EXECEXT) : test.o
gcc -o test$(EXECEXT) test.o -lm -lpng -gd
clean :
-rm *.o
-rm $(EXECUTABLES)
Using this Makefile, I got all error about undefined reference to whatever that are in the gd library.
What did I do wrong and How can I fix this?
Your own cc command already gives the answer. You need -lgd, not -gd.
E.g. set in the start:
LIBS=-lm -lpng -lgd
CC=gcc
(the latter can be the full OS-dependent path as well, and then the CC should be part of the OS specific part, and be specified as a full path).
and change the gcc line later to
$(CC) -o test$(EXECEXT) test.o $(LIBS)
And the rule for test.o is (usually) not really needed, as it is a default way to make a .o file from a .c file.

How to include an archive file in your c code

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

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.

Resources