如何在 Makefile 中添加额外的源文件
我有一个 Makefile,用于在 mac os x 上构建可执行文件,使用 mpacc 编译器,链接 mkl_lapack.h 库。 现在这个Makefile工作正常了,唯一的问题是,如果我想编译Eigenvalues.c链接其他.c文件,我不知道要添加什么,如果我想链接myfile.c,我必须在哪里写它生成文件?
MKL_INCLUDE=/opt/intel/mkl/include
MKLROOT=/opt/intel/mkl/lib
CC = mpicc
LD = mpicc
IFLAGS = -I$(MKL_INCLUDE)
CFLAGS = -Wall -O2 $(IFLAGS) -std=c99
LFLAGS = $(MKLROOT)/libmkl_intel_lp64.a $(MKLROOT)/libmkl_sequential.a $(MKLROOT)/libmkl_core.a -lpthread -lm
PROGRAMS = Eigenvalues
all: $(PROGRAMS)
Eigenvalues:
$(CC) $(CFLAGS) -o $@ $^ $(LFLAGS)
%.o: %.c
@echo C compiling $@
$(CC) -c $(CFLAGS) -o $@ $<
clean:
rm -rf *.o $(PROGRAMS)
Eigenvalues: Eigenvalues.c
I have a Makefile that I use to build executable on mac os x, using mpicc compiler, linking mkl_lapack.h library.
Now this Makefile is perfectly working, the only problem is that I don't know what to add if I want to compile Eigenvalues.c linking other .c files, if I want to link myfile.c where do I have to write it in Makefile?
MKL_INCLUDE=/opt/intel/mkl/include
MKLROOT=/opt/intel/mkl/lib
CC = mpicc
LD = mpicc
IFLAGS = -I$(MKL_INCLUDE)
CFLAGS = -Wall -O2 $(IFLAGS) -std=c99
LFLAGS = $(MKLROOT)/libmkl_intel_lp64.a $(MKLROOT)/libmkl_sequential.a $(MKLROOT)/libmkl_core.a -lpthread -lm
PROGRAMS = Eigenvalues
all: $(PROGRAMS)
Eigenvalues:
$(CC) $(CFLAGS) -o $@ $^ $(LFLAGS)
%.o: %.c
@echo C compiling $@
$(CC) -c $(CFLAGS) -o $@ lt;
clean:
rm -rf *.o $(PROGRAMS)
Eigenvalues: Eigenvalues.c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需让
Eigenvalues
目标依赖于所有.o
文件(而不是.c
文件,因为您有!) 组成了应用程序。按照惯例,这些对象的列表被放入一个变量中:顺便说一句,由于您使用的是标准变量名称
$(CC)
和$(CFLAGS)
,您可以完全省略%.o: %.c
规则; Make 有一个内置规则可以做同样的事情。Simply have the
Eigenvalues
target depend on all the.o
files (not the.c
files, as you have!) that make up the application. Conventionally, the list of these objects is put in a variable:By the way, since you are using the standard variable names
$(CC)
and$(CFLAGS)
, you can leave out the%.o: %.c
rule entirely; Make has a built-in rule that does the same thing.试试这个伙计!
Try this mate!