在编译时提供定义时如何检查是否定义了宏。
我有Makefile,它在编译示例C文件时解析并传递此字符串(“ -DMACRO_TEST=${DEFINED}
”)。
变量DEFINED可以具有或可以不具有值(0或非零)。
我想知道当变量DEFINED本身没有任何值时,如何检查MACRO_TEST
是否已定义。这类似于在编译C文件时传递“ -DMACRO_TEST =“。
#include <stdio.h>
#ifdef MACRO_TEST
char msg[] = "MACRO_TEST is defined";
#else
char msg[] = "MACRO_TEST is NOT-defined";
#endif
int main ()
{
printf("msg = %s\n", msg);
return 0;
}
命令行输出
$ gcc tmp.c -DMACRO_TEST=0
$ ./a.out
msg = MACRO_TEST is defined
$ gcc tmp.c -DMACRO_TEST=
$ ./a.out
msg = MACRO_TEST is defined
在第二种情况下进行编译时,我期望它会返回
msg = MACRO_TEST is NOT-defined
答案 0 :(得分:2)
如果省略任何选项tensorboard = TensorBoard(log_dir="logs/{}".format(NAME))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'],
)
model.fit(X, y,
batch_size=32,
epochs=10,
validation_split=0.3,
callbacks=[tensorboard])
或在 public void guess(View view)
{
if (e.getText().toString().length()==0) {
Toast.makeText ( this , "YOUR MESSAGE FOR EMPTY" Toast.LENGTH_SHORT ).show();
return;
}
}
命令行中显式添加-DMACRO_TEST
(=未定义宏),则不会定义该宏。在这种情况下,预处理程序指令-UMACRO_TEST
将为FALSE,您将获得
gcc
在#ifdef MACRO_TEST
分支中。
(假设您没有在代码中定义此宏。)
答案 1 :(得分:1)
一些测试宏的常见情况:
#ifdef MACRO_TEST
// MACRO_TEST is defined, whatever its content, even empty
// for example:
// -DMACRO_TEST
// -DMACRO_TEST=
// -DMACRO_TEST=0
// -DMACRO_TEST=1
// -DMACRO_TEST=abcd
#else
// MACRO_TEST is not defined, not even with an empty content
#endif
#if MACRO_TEST // it must not be defined as empty (-DMACRO_TEST= --> error)
// MACRO_TEST has a non-zero integer value
// for example:
// -DMACRO_TEST
// -DMACRO_TEST=1 -- equivalent to -DMACRO_TEST
// -DMACRO_TEST=23
#else
// MACRO_TEST has a zero integer value
// for example:
// (not defined at all, then considered as zero)
// -DMACRO_TEST=0
// -DMACRO_TEST=what
#endif
答案 2 :(得分:0)
只需使用#if xxx
。预处理器会将未定义的xxx
视为零。
#include <stdio.h>
#if MACRO_TEST
char msg[] = "MACRO_TEST is defined";
#else
char msg[] = "MACRO_TEST is NOT-defined";
#endif
int main ()
{
printf("msg = %s\n", msg);
return 0;
}
而且,您不必在命令行上提供值,只要-Dxxx
就足够了,编译器/预处理器将假定为1
:
cc -Wall -DMACRO_TEST defined.c
更新。所有四种情况:
#include <stdio.h>
#if MACRO_TEST
char msg[] = "MACRO_TEST is defined and non-zero";
#else
char msg[] = "MACRO_TEST is NOT-defined or zero";
#endif
int main (int argc, char**argv)
{
printf("%s: msg = %s\n", argv[0], msg);
return 0;
}
#!bin/sh
cc -Wall defined.c -o x
cc -Wall -DMACRO_TEST defined.c -o xd
cc -Wall -DMACRO_TEST=0 defined.c -o xd0
cc -Wall -DMACRO_TEST=1 defined.c -o xd1
./x
./xd
./xd0
./xd1
rm x xd xd0 xd1
结果:
./x: msg = MACRO_TEST is NOT-defined or zero
./xd: msg = MACRO_TEST is defined and non-zero
./xd0: msg = MACRO_TEST is NOT-defined or zero
./xd1: msg = MACRO_TEST is defined and non-zero