简体   繁体   中英

Sample makefile for gcc

Is there any sample makefile project that only consists of .c and .h files?

I am using gcc, Windows 8.1

I found this, but it does not work:

#########################
# customise these
CFILES := sampleA.c main.c sampleB.
PROG := prog
CFLAGS := -Wall -Wextra -g
LDFLAGS :=
########################

# -MMD generates dependencies while compiling
CFLAGS += -MMD
CC := gcc

OBJFILES := $(CFILES:.c=.o)
DEPFILES := $(CFILES:.c=.d)

$(PROG) : $(OBJFILES)
        $(LINK.o) $(LDFLAGS) -o $@ $^

clean :
        rm -f $(PROG) $(OBJFILES) $(DEPFILES)

-include $(DEPFILES)

I get the following error:

 undefined reference to sendto@24,socket@12,inte_addre@4, htons@04,bind@12, select@20,recvfrom@24, WSAGetLastError@0,WSAStartup@8,ioctlsocket@12

You can refer the below Makefile

exe ?= prog
CC = gcc
CFLAGS = -g -Wall -Wextra -MMD
LDFLAGS =

all: $(exe)
$(exe) : sampleA.c sampleB.c main.c
         $(CC) $(CFLAGS) $^ -o $@

clean: 
        -rm $(exe)

Here prog is output executable at the end of make.

You are missing some required library, wsock32 and ws2_32 if I'm not mistaken.

Use this Makefile:

EXE :=  prog.exe
SRC :=  $(wildcard *.c)
OBJ :=  $(SRC:.c=.o)
DEP :=  $(OBJ:.o=.d)

CC          :=  gcc
CPPFLAGS    :=  -MMD -MP
CFLAGS      :=  -W -Wall -g
LDFLAGS     :=
LDLIBS      :=  -lwsock32 -lws2_32

.PHONY: all clean fclean re

all: $(EXE)

$(EXE): $(OBJ)
    $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@

clean:
    $(RM) $(OBJ) $(DEP)

fclean: clean
    $(RM) $(EXE)

re: fclean all

-include $(DEP)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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