0

The problem is that my webserver works fine when I compile my code in terminal using gcc directly:

gcc webserver.c ../include/webserverinit.c ../include/webserver_request_response.c -o webserver

but after using Makefile the code doesn't work fine. I don't get any error but one of the functions doesn't work correctly

I don't understand what is happening here. I know my code is ok and to make sure the Makefile is working fine, I used some Makefile generator as well but it is still the same.

I know my code is ok and to make sure the Makefile is working fine I used some Makefile generator as well but it is still the same

Makefile that I'm using:

CC=gcc
WEBSERVER_OBJECT=./objects/webserver.o
WEBSERVER_SOURCE=./src/webserver.c
WEBSERVERINIT_OBJECT=./objects/webserverinit.o #Library to start the webserver and listening on the port
WEBSERVERINIT_SOURCE=./include/webserverinit.c
WEBSERVERINIT_HEADER=./include/webserverinit.h
WEBSERVER_REQUEST_RESPONSE_OBJECT=./objects/webserver_request_response.o #Library for receiving requests and sending respons
WEBSERVER_REQUEST_RESPONSE_SOURCE=./include/webserver_request_response.c
WEBSERVER_REQUEST_RESPONSE_HEADER=./include/webserver_request_response.h
OBJECTS=./objects/webserver.o ./objects/webserverinit.o ./objects/webserver_request_response.o #objects folder
HEADERS=./include/webserverinit.h ./include/webserver_request_response.h #headers
OBJECTDIR=./objects

webserver: ${OBJECTS}
    ${CC} ${OBJECTS} -o webserver

${WEBSERVER_OBJECT}: ${WEBSERVER_SOURCE} ${HEADERS}
    ${CC} -c  ${WEBSERVER_SOURCE} -o ${WEBSERVER_OBJECT}

${WEBSERVERINIT_OBJECT}: ${WEBSERVERINIT_SOURCE} ${WEBSERVERINIT_HEADER}
    ${CC} -c ${WEBSERVERINIT_SOURCE} -o ${WEBSERVERINIT_OBJECT}   

${WEBSERVER_REQUEST_RESPONSE_OBJECT}: ${WEBSERVER_REQUEST_RESPONSE_SOURCE} ${WEBSERVER_REQUEST_RESPONSE_HEADER}
    ${CC} -c ${WEBSERVER_REQUEST_RESPONSE_SOURCE} -o ${WEBSERVER_REQUEST_RESPONSE_OBJECT} 
clean:
    rm -rf ${OBJECTDIR}/*.o webserver

I want my code works with Makefile as well but it is not working with Makefile. What should I do?

  • Compare the compiler commands make emits with the ones you are using manually. What is the difference? – mkrieger1 Jul 03 '19 at 19:40
  • @mkrieger1 thanks for your advice....I compared my Makefile with manual command but I couldn't find any difference... I know the problem should be with my Makefile but I can't figure it out – amir eghbalian Jul 03 '19 at 19:43

1 Answers1

1

How can I create a Makefile to compile my C code correctly?

You need a source code editor (e.g. emacs) which knows the peculiarities of Makefiles in particular the weird role of tab characters in them.

CFLAGS=-c -o

That is probably wrong. You might want CFLAGS= -g -Wall instead. Hint: run make -p to understand the builtin rules of make and take advantage of them.

WEBSERVERINIT_SOURCE=./include/webserverinit.c

And that looks strange (I guess that you should replace include with src).


First, you need to read documentation. In particular, about invoking GCC on the command line, and later read the GNU make documentation. Spend several hours to read each of them.

Then, you might want to use the -v and -H flags to gcc. Both are helpful.

At last, you should use remake : it is an improved variant of make (designed to ease debugging of your Makefile). You'll use it with the -x flag.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • @amireghbalian: from my perspective, what's odd about `./include/webserverinit.c` is that you don't normally put source files such as `webserverinit.c` in a directory `include` which sounds like it will contain headers. I can't be sure that's what Basile was thinking of. – Jonathan Leffler Jul 03 '19 at 19:59
  • @JonathanLeffler yes...you are right.... source files should be in source directory instead of include directory but I don't think it solves the problem – amir eghbalian Jul 03 '19 at 20:01
  • 1
    @amireghbalian: When `webserver.c` includes `webserverinit.h`, what do you write as the `#include` line? `#include "webserverinit.h"`? Something else? You might need `-I./include` on the compiler command lines — possibly as part of `CFLAGS`. Your `CFLAGS` should include options such as `-O3 -std=c11 -Wall -Wextra -Werror`. The `-c` and `-o` options would normally be written verbatim in the command line; they don't vary between compilers. I use separate names such as `WFLAG1 = -Wall` — `WFLAG2 = -Wextra` — `WFLAG3 = -Werror` — `WFLAGS = ${WFLAG1} ${WFLAG2} ${WFLAG3}` _[…continued…]_ – Jonathan Leffler Jul 03 '19 at 20:06
  • 1
    _[…continuation…]_ and then `CFLAGS = … ${WFLAGS} …`. That way, I can turn any of the warnings on or off from the command line: `make WFLAG3=` disables `-Werror`, for example. I don't often use that unless I'm working on someone else's code that doesn't meet my coding standards. – Jonathan Leffler Jul 03 '19 at 20:08
  • @JonathanLeffler webserver.c is the main file and first, it uses webserverinit header (#include "../include/webserverinit.h"). it has 3 functions and all of them works fine. another header is webserver_request_response which is responsible for getting a request from clients and sending a response to client. the function for getting request is working fine but the function for sending a response to the client doesn't work ok. – amir eghbalian Jul 03 '19 at 20:12
  • 1
    @amireghbalian: the working or not of the function in `webserver_request_response.c` is not related to the `makefile`; it is related to the code in the source file, and/or the headers it uses. At most, the differences in the build process mean a bug (with 'undefined behaviour') manifests itself differently. If you create the executable, it probably isn't the `makefile` that's at fault. Note the question [What are the benefits of a relative path such as `"../include/header.h"` for a header?](https://stackoverflow.com/questions/597318/) – Jonathan Leffler Jul 03 '19 at 20:17
  • @JonathanLeffler I'll check it out and test it...thanks – amir eghbalian Jul 03 '19 at 20:20
  • @JonathanLeffler Thank you so much...You were right... the problem is related to the path...I put all the files in a single directory and used the Makefile and now it works fine – amir eghbalian Jul 03 '19 at 20:42