[英]Access GCC Compiler Switches From Inside C/C++ Program
It is possible to access the gcc compiler switches ac/c++ program was compiled with from inside the program? 有可能访问gcc编译器交换机ac / c ++程序是从程序内部编译的吗?
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. 在我的应用程序中作为日志记录信息的一部分,我想编写程序编译的开关,例如编译器的优化和预处理器变量输入。
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). 通常构建系统将在应用程序中内置的版本字符串中生成此类内容(但它们都不是自动的)。
作为Martin回答的补充:作为这种技术的一个例子,你可以看看Vim源代码 - grep for all_cflags
或all_lflags
。
There are only some macro for compiler switches 编译器开关只有一些宏
http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
__OPTIMIZE__ __OPTIMIZE_SIZE__ __NO_INLINE__
__OPTIMIZE__
is defined in all optimizing compilations.__OPTIMIZE__
在所有优化编译中定义。__OPTIMIZE_SIZE__
is defined if the compiler is optimizing for size, not speed. 如果编译器针对大小而不是速度进行优化,则定义__OPTIMIZE_SIZE__
。__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). 如果没有函数将被内联到其调用者中(未进行优化时,或者内联已被-fno-inline专门禁用),则会定义__NO_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. 如果确实需要完整的编译字符串,则应修改build / make脚本,以将字符串保存在特殊的.h
文件中作为常量或定义。
An alternate solution is to simply wrap the gcc compiler invocation with a shell script that saves the flags to a header file. 另一种解决方案是使用shell脚本简单地包装gcc编译器调用,该脚本将标志保存到头文件中。 You can then include the header file in a source file, eg: 然后,您可以将头文件包含在源文件中,例如:
#!/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. 将该脚本调用为gcc_wrap -O0 main.c
将生成包含以下内容的头文件,然后继续编译main.c.
#define GCC_OPTIONS "-O0 main.c"
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". 在我的一个项目中,每个构建都进入了自己的目录,通常整个构建都有一个特定的名称,如“parallel-debug”或“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. 无论如何,你可以做的是将$(CC)或$(FLAGS)或任何变量回显到文本文件中,然后让你的程序在启动时读取该文件。 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. 它不是元魔法,斯科特迈耶斯可能不会采访你有效的C ++ VII,但这个问题似乎不值得那么头痛。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.