从 C/C++ 内部访问 GCC 编译器开关程序

发布于 2024-10-10 17:59:37 字数 104 浏览 1 评论 0原文

是否可以访问从程序内部编译的 gcc 编译器开关 ac/c++ 程序?

在我的应用程序中,作为日志信息的一部分,我想编写程序编译时使用的开关,例如编译器的优化和预处理器变量输入。

It is possible to access the gcc compiler switches a c/c++ program was compiled with from inside the program?

In my application as part of the logging information I would like to write which switches the program was compiled with, such as optimizations and pre-processor variable input by the compiler.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

静水深流 2024-10-17 17:59:37

不是以任何标准方式。

通常是构建系统会在应用程序中内置的版本字符串中生成此类内容(但都不是自动的)。

Not in any standard way.

It is usually the build system that will generate such things in a version string that is built into the application (but none of it is automatic).

卖梦商人 2024-10-17 17:59:37

作为 Martin 答案的补充:作为此技术的一个示例,您可以查看 Vim 源代码 - grep for all_cflagsall_lflags

As an addition to Martin's answer: as an example of this technique you can look at Vim sources - grep for all_cflags or all_lflags.

别再吹冷风 2024-10-17 17:59:37

只有一些用于编译器开关的宏

http://gcc.gnu.org /onlinedocs/cpp/Common-Predefined-Macros.html

 __OPTIMIZE__
 __OPTIMIZE_SIZE__
 __NO_INLINE__

__OPTIMIZE__ 在所有优化编译中定义。如果编译器针对大小而非速度进行优化,则定义 __OPTIMIZE_SIZE__。如果没有函数将内联到其调用者中(未优化时,或者内联已被 -fno-inline 专门禁用时),则定义 __NO_INLINE__ 。

如果您确实需要完整的编译字符串,则应修改构建/制作脚本以将字符串作为常量或定义保存在特殊的 .h 文件中。

There are only some macro for compiler switches

http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html

 __OPTIMIZE__
 __OPTIMIZE_SIZE__
 __NO_INLINE__

__OPTIMIZE__ is defined in all optimizing compilations. __OPTIMIZE_SIZE__ is defined if the compiler is optimizing for size, not speed. __NO_INLINE__ is defined if no functions will be inlined into their callers (when not optimizing, or when inlining has been specifically disabled by -fno-inline).

If you do need a full compile string, you should modify your build/make script to save the string in the special .h file as constant or as define.

独自←快乐 2024-10-17 17:59:37

另一种解决方案是简单地使用 shell 脚本包装 gcc 编译器调用,将标志保存到头文件中。然后,您可以将头文件包含在源文件中,例如:

#!/bin/sh

echo "#define GCC_OPTIONS \"$*\"" > gcc_options.h
exec gcc $@

调用该脚本 gcc_wrap -O0 main.c 将生成包含以下内容的头文件,然后继续编译 main.c 。

#define GCC_OPTIONS "-O0 main.c"

An alternate solution is to simply wrap the gcc compiler invocation with a shell script that saves the flags to a header file. You can then include the header file in a source file, e.g.:

#!/bin/sh

echo "#define GCC_OPTIONS \"$*\"" > gcc_options.h
exec gcc $@

Invoking that script as gcc_wrap -O0 main.c will produce the header file with the following contents and then proceed with the compilation of main.c.

#define GCC_OPTIONS "-O0 main.c"
肥爪爪 2024-10-17 17:59:37

在我的一个项目中,每个构建都会进入自己的目录,并且通常整个构建都有一个特定的名称,例如“parallel-debug”或“singlethread-O2”。通常,该目录中的日志文件从其位置隐式地向我们提供了所有信息。

无论如何,您可以做的是将 $(CC) 或 $(FLAGS) 或任何变量回显到文本文件中,然后让您的程序在启动时读取该文件。这不是元魔法,Scott Meyers 可能不会就有效的 C++ VII 采访您,但这个问题似乎并不值得那么头痛。

On one of my projects, every build went into its own directory, and usually the whole build had a specific name like "parallel-debug" or "singlethread-O2". Usually a log file in that directory gave us all the info implicitly from its location.

Anyway, what you can do is echo the $(CC) or $(FLAGS) or whatever variables into a text file, and then have your program read that file on startup. Its not meta magic and Scott Meyers might not interview you for effective C++ VII, but this problem doesn't seem to merit that much headache.

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