如何传递值之作,它被传递到C code [英] How to pass a value to Make, which gets passed to C code
问题描述
我寻求帮助,让我的Makefile做我想做的事情。
I'm looking for help getting my Makefile to do what I want it to do.
我已经找到了如何preprocessing code添加到我的C源代码,这将只编译如果我编译调试:
I have figured out how to add preprocessing code to my C source which will compile only if I'm compiling for debug:
#if DEBUG
printf("main()\n");
{
/* Pauses execution so gdb can attach. */
int i=9;
pid_t PID;
char hostname[256];
gethostname(hostname, sizeof(hostname));
printf("PID %d on %s ready for attach.\n", PID=getpid(), hostname);
fflush(stdout);
while (i>0) {
sleep(5);
i--;
}
}
#endif
我也想通了,如果我添加 -DDEBUG = 1
我的编译语句,那上面code将被编译(否则它不会编译)。
And I've figured out that if I add -DDEBUG=1
to my compile statement, that the above code will be compiled (otherwise it's not compiled).
接下来,我想一个标志传递给我的Makefile将要么包含或不包含 -D
选项。目前,我有我的评论并取消酌情两个独立的编译行。这里是我的Makefile(这是我从别人继承和我有困难的时候理解)。看到说 CFLAGS
行:
Next, I want to pass a flag to my Makefile which will either include, or not include the -D
option. Currently, I have two separate compile lines which I comment and uncomment as appropriate. Here is my Makefile (which I inherited from someone and am having a difficult time understanding). See the lines that say CFLAGS
?:
SHELL = /bin/sh
prefix = /home/schwarz/sundials/instdir
exec_prefix = ${prefix}
includedir = ${prefix}/include
libdir = ${exec_prefix}/lib
CPP = cc -E
CPPFLAGS =
CC = cc
# CFLAGS = -Wall -g
CFLAGS = -Wall -g -DDEBUG=1
# CFLAGS = -g -O2
LDFLAGS =
LIBS = -lm
MPICC = /usr/local/mpi/bin/mpicc
MPI_INC_DIR = /usr/local/mpi/bin/../include
MPI_LIB_DIR = /usr/local/mpi/bin/../lib
MPI_LIBS =
MPI_FLAGS =
INCLUDES = -I${includedir} -I${MPI_INC_DIR}
LIBRARIES = -lsundials_cvode -lsundials_nvecparallel ${LIBS}
LIBRARIES_BL =
EXAMPLES = FPU # cvAdvDiff_non_p cvDiurnal_kry_bbd_p cvDiurnal_kry_p
OBJECTS = ${EXAMPLES:=.o}
# -----------------------------------------------------------------------------------------
.SUFFIXES : .o .c
.c.o :
${MPICC} ${CPPFLAGS} ${CFLAGS} ${MPI_FLAGS} ${INCLUDES} -c $<
# -----------------------------------------------------------------------------------------
all: ${OBJECTS}
@for i in ${EXAMPLES} ; do \
echo "${MPICC} -o $${i} $${i}.o ${MPI_FLAGS} ${CFLAGS} ${LDFLAGS} -L${libdir} ${LIBRARIES} -L${MPI_LIB_DIR} ${MPI_LIBS} ${LIBRARIES_BL}" ; \
${MPICC} -o $${i} $${i}.o ${MPI_FLAGS} ${CFLAGS} ${LDFLAGS} -L${libdir} ${LIBRARIES} -L${MPI_LIB_DIR} ${MPI_LIBS} ${LIBRARIES_BL}; \
done
clean:
rm -f ${OBJECTS}
rm -f ${EXAMPLES}
我在网上搜索 - 我发誓! - 但无法弄清楚如何将参数传递到makefile文件。 如何设置我的Makefile,以便它不同类型不同的编译需要在同一$ C $的C编译的?的更妙的是,如果我能传递一个特定值的Makefile文件将一个特定的值传递给编译,我可以用它来初始化 I
在code。 (IOW,我编译为使8
而C被编译为 INT I = 8;
)。任何的,即使有可能在做什么呢?
I have searched the web - I swear! - but cannot figure out how to pass a parameter into the makefile. How can I set up my Makefile so that it does different types of compiles of the same code for different compile needs? Even better would be if I could pass a specific value to the Makefile, which would pass a specific value to the compile, which I could use to initialize i
in the code. (IOW, I compile as make 8
and the C is compiled as int i=8;
). Is any of that even possible in Make?
顺便说一句,我读过有关Makefile文件,以及他们如何通过列出与标签(冒号)文件的工作,其次是他们的依赖。但我不明白这个Makefile是如何工作的,或为什么它会发出两条编译语句。因此,一个简单的解释也将是有益的。
As an aside, I've read about Makefiles and how they work by listing files with labels (colons), followed by their dependencies. But I can't understand how this Makefile works or why it issues two compile statements. So a brief explanation would also be helpful.
推荐答案
要调试的,并选择发行版本:
To choose between debug and release versions:
ifeq($(debug),1)
CFLAGS = -Wall -g -DDEBUG=1
else
CFLAGS = -Wall -g
endif
,然后调用化妆:
and then invoke make:
make debug=1
使用DEBUG进行编译,或者:
to compile with DEBUG, or:
make
编译没有调试。
有关初始化I:
#include <stdio.h>
#ifndef INIT_I
# define INIT_I 9
#endif
int main() {
int i = INIT_I;
return 0;
}
和生成文件:
ifneq ($(init_i),)
CFLAGS = "-DINIT_I=$(init_i)"
endif
all:
gcc d.c $(CFLAGS) -E
要设置的初始我:
make init_i=10
或使用默认值:
make
这篇关于如何传递值之作,它被传递到C code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!