用多个.c文件构建makefile项目 [英] building makefile project with multiple .c files

查看:196
本文介绍了用多个.c文件构建makefile项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下项目结构(使用Eclipse)

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

然后我创建了以下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)

但是当我尝试构建项目时,出现以下错误:make: *** No rule to make target 'inc/prog_lib.c', needed by 'all'. Stop.

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?

推荐答案

详细了解 GNU make ,并花一些时间阅读其文档.

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

您的变量分配

 SOURCES=$ main.c inc/prog_lib.c

是错误的.应该是

 SOURCES= main.c prog_lib.c

您可能还需要(最好要求-g来方便使用gdb进行调试)

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

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

也请尝试make -p了解内置规则.您应该更好地使用它们(然后在CFLAGS中使用-c可能是错误的).

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

考虑使用 remake (也许是remake -x)来调试您的Makefile;或至少make --trace.

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

顺便说一句,我相信您的文件层次结构太复杂且不合适.对于这样一个简单而小型的项目,拥有inc/目录是不值得的.

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.

为了获得灵感,请研究一些现有的免费软件Makefile.在 github 上.

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

这篇关于用多个.c文件构建makefile项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆