如何在 Makefile 中添加额外的源文件

发布于 2024-12-25 21:06:54 字数 688 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野心澎湃 2025-01-01 21:06:54

只需让 Eigenvalues 目标依赖于所有 .o 文件(而不是 .c 文件,因为您有!) 组成了应用程序。按照惯例,这些对象的列表被放入一个变量中:

PROGRAMS = Eigenvalues
Eigenvalues_OBJS = Eigenvalues.o foo.o bar.o #etc

all: $(PROGRAMS)

Eigenvalues: $(Eigenvalues_OBJS)
        $(CC) $(CFLAGS) -o $@ $^ $(LFLAGS) 

# delete the "Eigenvalues: Eigenvalues.c" line,
# leave everything else as you have it

顺便说一句,由于您使用的是标准变量名称 $(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:

PROGRAMS = Eigenvalues
Eigenvalues_OBJS = Eigenvalues.o foo.o bar.o #etc

all: $(PROGRAMS)

Eigenvalues: $(Eigenvalues_OBJS)
        $(CC) $(CFLAGS) -o $@ $^ $(LFLAGS) 

# delete the "Eigenvalues: Eigenvalues.c" line,
# leave everything else as you have it

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.

网名女生简单气质 2025-01-01 21:06:54

试试这个伙计!

PROGRAMS = Eigenvalues

MKL_INCLUDE=/opt/intel/mkl/include 
MKLROOT=/opt/intel/mkl/lib 

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  


all: $(PROGRAMS).c

OBJS = \
Eigenvalues.o \
myfile.o\

##############################################################################
.SUFFIXES : .c .o

CC = mpicc 
LD = mpicc 
RM = rm -rf

$(PROGRAMS).c : $(OBJS)
    $(CC) $(CFLAGS) -o $@ $^ $(LFLAGS)  

clean: 
    $(RM) *.o $(OBJS) $(PROGRAMS) 

.c.o :
    $(CC) -c $(CFLAGS) -o $@ 
lt;

Try this mate!

PROGRAMS = Eigenvalues

MKL_INCLUDE=/opt/intel/mkl/include 
MKLROOT=/opt/intel/mkl/lib 

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  


all: $(PROGRAMS).c

OBJS = \
Eigenvalues.o \
myfile.o\

##############################################################################
.SUFFIXES : .c .o

CC = mpicc 
LD = mpicc 
RM = rm -rf

$(PROGRAMS).c : $(OBJS)
    $(CC) $(CFLAGS) -o $@ $^ $(LFLAGS)  

clean: 
    $(RM) *.o $(OBJS) $(PROGRAMS) 

.c.o :
    $(CC) -c $(CFLAGS) -o $@ 
lt;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文