在编译期间更改预处理值
Changing preprocessed values during compile time
我已经使用预处理器指令编写了一些代码来跳过某些语句 executed.But 我在 main 中的 C 代码有兴趣更改以前#defined 的值并根据条件分配新值并更改结果在 运行 time.In 期间也有预处理语句的数量 我必须在 运行 时间内更改预处理语句。我该怎么做?
In short I have to change the pre processed statements during run time
这不可能。阅读 C preprocessing & cpp. Compile-time and run-time are different (and the compiled code could even run on a different machine, read more about cross-compiling). If using GCC,使用 gcc -C -E foo.c > foo.i
将您的 foo.c
源文件预处理为 foo.i
预处理形式(然后使用编辑器或页面查看生成的 foo.i
)
也许您想在运行时加载额外的代码。这对于纯 C99 标准代码是不可能的。也许您的操作系统提供 dynamic loading. POSIX specifies dlopen
. You might also want to use JIT compiling techniques to construct machine code at runtime, e.g. with libraries like GCCJIT, asmjit, GNU lightning, libjit, LLVM, ...
另请参阅 homoiconic languages. Consider coding in Common Lisp (e.g. with SBCL)。
不可能。预处理发生在编译时间之前。
编译器只看到预处理器的结果,仅此而已。
我已经使用预处理器指令编写了一些代码来跳过某些语句 executed.But 我在 main 中的 C 代码有兴趣更改以前#defined 的值并根据条件分配新值并更改结果在 运行 time.In 期间也有预处理语句的数量 我必须在 运行 时间内更改预处理语句。我该怎么做?
In short I have to change the pre processed statements during run time
这不可能。阅读 C preprocessing & cpp. Compile-time and run-time are different (and the compiled code could even run on a different machine, read more about cross-compiling). If using GCC,使用 gcc -C -E foo.c > foo.i
将您的 foo.c
源文件预处理为 foo.i
预处理形式(然后使用编辑器或页面查看生成的 foo.i
)
也许您想在运行时加载额外的代码。这对于纯 C99 标准代码是不可能的。也许您的操作系统提供 dynamic loading. POSIX specifies dlopen
. You might also want to use JIT compiling techniques to construct machine code at runtime, e.g. with libraries like GCCJIT, asmjit, GNU lightning, libjit, LLVM, ...
另请参阅 homoiconic languages. Consider coding in Common Lisp (e.g. with SBCL)。
不可能。预处理发生在编译时间之前。
编译器只看到预处理器的结果,仅此而已。