makefile obj文件路径,带有目标文件目录的Makefile

I have the following structure for a project and I am just starting to introduce a Makefile to build the software:

├── Makefile

├── README.md

├── cg

│   └── cg.c

└── utilities

├── utilities.c

└── utilities.h

I am trying to put object files in a directory called obj yet I can't seem to get it working.

My makefile looks like:

CC=mpicc

CFLAGS=-O3 -std=c99

LIBS=

MKDIR_P = mkdir -p

make_build_dir:

@mkdir -p obj/

utilities.o: utilities/utilities.c

$(CC) $(CFLAGS) -o ./obj/$@ -c $<

cg.o: cg/cg.c

$(CC) $(CFLAGS) -o ./obj/$@ -c $<

.PHONY: make_build_dir

cg.exe: make_build_dir utilities.o cg.o

$(CC) $(CFLAGS) -o $@ $<

clean:

rm -fr obj

rm cg.exe

Yet this generates the following error:

a@a:b/b ‹master*›$ make cg.exe

mpicc -O3 -std=c99 -o ./obj/utilities.o -c utilities/utilities.c

mpicc -O3 -std=c99 -o ./obj/cg.o -c cg/cg.c

cg/cg.c:133:3: warning: implicit declaration of function 'decompose' is invalid in C99

[-Wimplicit-function-declaration]

decompose(num_chunks, chunks_per_rank,me, &settings);

^

1 warning generated.

mpicc -O3 -std=c99 -o cg.exe make_build_dir

clang: error: no such file or directory: 'make_build_dir'

make: *** [cg.exe] Error 1

How can I get it to generate the object files in the obj directory and then an executable in the top-level directory?

解决方案

This linking part of the makefile

cg.exe: make_build_dir utilities.o cg.o

$(CC) $(CFLAGS) -o $@ $<

has two issues. First, $< refers to the first prerequesite of the target cg.exe, and that is make_build_dir. Declaring it as .PHONY doesn't help here, it's simply passed to $(CC). Second, utilities.o cg.o both don't exist at this location. You can change the rule to

cg.exe: obj/utilities.o obj/cg.o

$(CC) $(CFLAGS) -o $@ $^

Note the automatic variable $^ which refers to all prerequisites. Additionally, the object file targets should be

obj/cg.o: cg/cg.c

$(CC) $(CFLAGS) -o $@ -c $<

(identical for utilities.o).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值