Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

building makefile project with multiple .c files

I have the following project structure (using eclipse)

project/
 |____ inc/
 |       |___ config_prog.h
 |       |___ prog_lib.h
 |
 |____ data/
 |       |___ img.bmp
 |
 |__ prog_lib.c
 |__ main.c
 |__ makefile

And i made the following makefile :

CC=gcc
CFLAGS= -O0 -c -Wall -mavx2 -mfma
LDFLAGS=

SOURCES=$ main.c inc/prog_lib.c
OBJECTS=$(SOURCES:.c=.o)

EXECUTABLE=main

all: $(TASKMAP) $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@ -lm

.c.o:
    $(CC) $(CFLAGS) $< -lm -o $@

clean: 
    rm -fr $(OBJECTS) $(EXECUTABLE)

but when i try to build the project i get the following error:make: *** No rule to make target 'inc/prog_lib.c', needed by 'all'. Stop.

Could someone tell me the problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Read more about GNU make and take time to read its documentation.

Your variable assignment

 SOURCES=$ main.c inc/prog_lib.c

is wrong. It should be

 SOURCES= main.c prog_lib.c

You probably need also (better ask for -g to facilitate debugging with gdb)

CFLAGS= -O0 -c -Wall -g -mavx2 -mfma -Iinc

Try also make -p to understand the builtin rules. You should use them better (and then using -c in your CFLAGS is probably wrong).

Consider using remake, perhaps as remake -x, to debug your Makefile; or at least make --trace.

BTW, I believe that your file hierarchy is too complex and inappropriate. For such a simple and small project, having an inc/ directory is not worth the pain.

For inspiration, study the Makefile of some existing free software, e.g. on github.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...