How do I create Makefile for this - c

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.

Related

How do you link two files in make, that both include a different library?

I want to test my code in test.c which includes main.h with all the function declarations from main.c. I want to compile my test.c file with criterion, which on its own works fine like this: cc -c test.c -o testing -I include -L lib -l criterion.3.1.0 and I compile my main.c in the same way and it works fine: cc main.c -o output -I include -L lib -l SDL2-2.0.0. But when I want to include a function from main.c in test.c it gives me a linking error.
The project architecture is like this:
main.c
#include "main.h"
int function1(){
[...]
}
test.c
#include "main.h"
#include <criterion/criterion.h>
Test(sample,test_function1){
cr_assert(function1() == 1);
}
main.h
#include <SDL2/SDL.h>
extern int function1();
How do I have to compile/link these files in order to have my test-file use get access to the functions from main.c?
What you are currently doing is compiling and linking the binary in a single command for each source file in your project. The linker, with how you are doing this, is not aware of your other sources. Instead, you should first build the object files for each source file, and then you can link those together to form your binary.
Building object files:
cc -c main.c -I include -o main.o
cc -c test.c -I include -o test.o
Linking:
cc -o testing main.o test.o -Llib -lcriterion.3.1.0 -lSDL2-2.0.0

Where to change LD_LIBRARY_PATH

I want to compile these files into executable.
//main.c
#include <stdio.h>
#include <mylib.h>
int main(void){
call_hello_world();
return 0;
}
//mylib.h
void call_hello_world(void);
//mylib.c
#include <mylib.h>
#include <stdio.h>
void call_hello_world( void ) {
printf( ”Hello world!” );
}
I tried
gcc -c -I. -fPIC -o mylib.o mylib.c
gcc -shared -o libmylib.so mylib.o
gcc -c -o main.o main.c
gcc -o hello main.o -L. -lmylib
but at the third step, I got stucked because it couldn't find my 'mylib.h'. My professor said I needed to change 'LD_LIBRARY_PATH' so I tried to add this export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/dev/shm my .zshrc but it still didn't work for me. Any suggestions what I should do?
There are several issues with your approach.
First, there is a difference between including a header file like this #include <mylib.h> and including it like that #include "mylib.h".
The first option is usually used to include standard library header files, that should be located in the standard set of directories according to the FHS on Linux.
The latter is the option you might want to use as it is usually used to include user-defined headers and tells the preprocessor to search in the directory of the file containing the directive. (See #quest49 answer's https://stackoverflow.com/a/21594/3852949)
The LD_LIBRARY_PATH environment variable is used to indicate where libraries should be searched for first before looking into the standard set of directories.
So what you would want to do to make your main.c file compile, and after changing #include <mylib.h> directive to #include "mylib.h", is to either :
Add the include file into the directory where your main.c file is located
Indicate where the include file path is with -I option to gcc
These are the commands needed :
gcc -c -I. -fPIC -o mylib.o mylib.c
gcc -shared -o libmylib.so mylib.o
gcc -c -I. -o main.o main.c
gcc -o hello main.o libmylib.so
Then in your shell:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/full/path/of/directory/containing/libmylib-so

"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:

makefile (link 2 source files non is main)

I have a project that contains 4 source files :
RTP.c, RTCP.c RTSP.c main.c
and 3 header files :
RTP.h RTCP.h RTSP.h
I have to include all the header files in the main and the RTCP.h in the RTP.c after I included the header files in the source files I linked them in a make file please help me understand the problem.
the RTP.c
#include "RTP.h"
#include "RTCP.h"
the RTCP.c
#include "RTCP.h"
The RTSP.c
#include "RTSP.h"
The main.c
#include "RTP.h"
#include "RTSP.h"
The make file:
OBJS = main.o RTPfunctions.o RTCPfunctions.o RTSPfunctions.o
CC = gcc
CCFLAGS = -g
Client : $(OBJS)
$(CC) $(OBJS) -o -pthread client
RTCPfunctions.o : RTCPfunctions.c RTCPfunctions.h
$(CC) -c -g -pthread RTCPfunctions.c
RTSPfunctions.o : RTSPfunctions.c RTSPfunctions.h
$(CC) -c -g -pthread RTSPfunctions.c
RTPfunctions.o : RTPfunctions.c RTPfunctions.h RTCPfunctions.h
$(CC) -c -g -o -pthread RTPfunctions.c RTCPfunctions.o
main.o : main.c RTPfunctions.h RTSPfunctions.h
$(CC) -c -g -o -pthread main.c RTPfunctions.o RTSPfunctions.o
clean:
\rm *.o *~ client
Your question is not very verbose, however, from a quick glance at your makefile, we can say, as per the online gcc manual,
-o file
Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.
Basically, this says, the immidiate next argument to -o should be the output file name.
Also, to follow the order of linking, the pthread library should be placed at the end, like
$(CC) $(OBJS) -o client -pthread

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