[英]Get built arguments in C
Is there any way to check the arguments used to compile? 有什么方法可以检查用于编译的参数吗?
Like: 喜欢:
gcc -std=c99 -ggdb3 source.c -o sate-enak
gcc -std = c99 -ggdb3 source.c -o sate-enak
In source.c
: 在
source.c
:
...
#ifdef (-ggdb3 variable is defined)
do_some_function();
#else
do_another_function();
#endif
...
With using this method, I can find out if the program is compiled for production or product. 使用这种方法,我可以确定程序是针对生产还是针对产品进行编译的。
With gcc
, not to my knowledge but you can achieve the same goal with a macro: 使用
gcc
,据我所知,但您可以通过宏来实现相同的目标:
In your Makefile: 在您的Makefile中:
CFLAGS_DEBUG = -ggdb3 -DDEBUG
CFLAGS = -std=c99 $(CFLAGS_DEBUG)
then in your program: 然后在您的程序中:
#ifdef DEBUG
do_some_function();
#else
do_another_function();
#endif
There isn't an easy way to spot the options used by the compiler. 没有一种简单的方法可以发现编译器使用的选项。 All else apart, most programs are built from many source files, and those source files may have been compiled with different sets of options.
除其他外,大多数程序是从许多源文件构建的,并且这些源文件可能已使用不同的选项集进行了编译。
Usually, if you want to know, you control it with a command-line #define
: 通常,如果您想知道,可以使用命令行
#define
:
gcc -DMODE=MODE_OPTIM -O3 …
gcc -DMODE=MODE_DEBUG -ggdb3 …
where you have a header that defines the meaning of MODE_OPTIM
and MODE_DEBUG
: 您具有定义
MODE_OPTIM
和MODE_DEBUG
含义的标头:
enum CompilationMode { MODE_OPTIM, MODE_DEBUG };
#ifndef MODE
#define MODE MODE_DEBUG
#endif
extern enum CompilationMode compiled_for;
And somewhere you define that: 在某个地方定义:
enum CompilationMode compiled_for = MODE;
And then you can test compiled_for
wherever you need to know which mode the program was built with. 然后你就可以测试
compiled_for
无论你需要知道哪些模式的程序内置。
Actually, this is runtime decision making. 实际上,这是运行时决策。 For compile time decision making, you replace the
enum
with: 为了进行编译时决策,请将
enum
替换为:
#define MODE_OPTIM 0
#define MODE_DEBUG 1
and you can test: 您可以测试:
#if MODE == MODE_DEBUG
do_some_function();
#else
do_another_function();
#endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.