简体   繁体   English

如何创建Makefile正确编译我的C代码?

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

The problem is that my webserver works fine when I compile my code in terminal using gcc directly: 问题是,当我直接使用gcc在终端中编译代码时,我的Web服务器工作正常:

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

but after using Makefile the code doesn't work fine. 但是使用Makefile之后,代码无法正常工作。 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. 我知道我的代码还可以,并且为了确保Makefile正常工作,我也使用了一些Makefile生成器,但它仍然相同。

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正常运行,我也使用了一些Makefile生成器,但它仍然相同

Makefile that I'm using: 我正在使用的Makefile:

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. 我希望我的代码也可以与Makefile一起使用,但不能与Makefile一起使用。 What should I do? 我该怎么办?

How can I create a Makefile to compile my C code correctly? 如何创建Makefile正确编译我的C代码?

You need a source code editor (eg emacs ) which knows the peculiarities of Makefile s in particular the weird role of tab characters in them. 您需要一个源代码编辑器 (例如emacs ),该编辑器知道Makefile的特性 ,尤其是其中制表符字符的怪异作用。

CFLAGS=-c -o

That is probably wrong. 这可能是错误的。 You might want CFLAGS= -g -Wall instead. 您可能希望改为CFLAGS= -g -Wall Hint: run make -p to understand the builtin rules of make and take advantage of them. 提示:运行make -p了解的内置规则make ,并把他们的优势。

WEBSERVERINIT_SOURCE=./include/webserverinit.c

And that looks strange (I guess that you should replace include with src ). 这看起来很奇怪(我想您应该用src替换include )。


First, you need to read documentation . 首先,您需要阅读文档 In particular, about invoking GCC on the command line, and later read the GNU make documentation . 特别是有关在命令行上调用GCC ,然后再阅读GNU make文档 Spend several hours to read each of them. 花几个小时阅读每个。

Then, you might want to use the -v and -H flags to gcc . 然后,您可能要对gcc使用-v-H标志。 Both are helpful. 两者都是有帮助的。

At last, you should use remake : it is an improved variant of make (designed to ease debugging of your Makefile ). 最后,您应该使用remake :它是make的改进版本(旨在简化Makefile调试)。 You'll use it with the -x flag. 您将其与-x标志一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM