编译器信息/编译参数 是如何写入到二进制文件,并且可以显式地用 -V 等参数查看的?

2016-07-22 20:55:57 +08:00
 fyyz

标题说得不是很清楚。先举个例子来看一下吧。 以下内容经过了重新排版。

# nginx -V
nginx version: nginx/1.10.1
built with OpenSSL 1.0.2h  3 May 2016
TLS SNI support enabled
configure arguments:
        --prefix=/etc/nginx
        --conf-path=/etc/nginx/nginx.conf
        --sbin-path=/usr/bin/nginx
        --pid-path=/run/nginx.pid
        --lock-path=/run/lock/nginx.lock
        --user=http
        --group=http
        --http-log-path=/var/log/nginx/access.log
        --error-log-path=stderr
        --http-client-body-temp-path=/var/lib/nginx/client-body
        --http-proxy-temp-path=/var/lib/nginx/proxy
        --http-fastcgi-temp-path=/var/lib/nginx/fastcgi
        --http-scgi-temp-path=/var/lib/nginx/scgi
        --http-uwsgi-temp-path=/var/lib/nginx/uwsgi
        --with-ipv6
        --with-pcre-jit
        --with-file-aio
        --with-http_addition_module
        --with-http_auth_request_module
        --with-http_dav_module
        --with-http_degradation_module
        --with-http_flv_module
        --with-http_geoip_module
        --with-http_gunzip_module
        --with-http_gzip_static_module
        --with-http_mp4_module
        --with-http_realip_module
        --with-http_secure_link_module
        --with-http_ssl_module
        --with-http_stub_status_module
        --with-http_sub_module
        --with-http_v2_module
        --with-mail
        --with-mail_ssl_module
        --with-stream
        --with-stream_ssl_module
        --with-threads

可以看到,输出的内容有编译参数,其中就包括了配置文件路径、二进制文件路径等等,除此之外还有 OpenSSL 库的版本。

很明显,这样的信息是写入到二进制文件中去的。就比如说默认配置文件路径是 /etc/nginx/nginx.conf ,如果这个信息没有写入到二进制文件当中,那么 nginx 的二进制文件就根本不知道默认配置文件在哪里,而默认配置文件的参数又不是必定在 /etc/nginx ,也有可能是其他路径,但是这不是在编写 nginx 的源码时确定的,而是在 ./configure --some-args 的时候确定的。

有些程序的二进制文件给予 -v / -V / --version 之类的参数,还会输出编译器(比如 gcc/g++)的版本之类的信息。

那么,诸如 编译器信息 /编译参数 之类的信息是如何写入到二进制文件当中去的呢?

1583 次点击
所在节点    C
4 条回复
msg7086
2016-07-22 21:11:19 +08:00
把参数写入头文件里,然后把头文件编译到程序中咯。
congeec
2016-07-22 21:14:54 +08:00
configure arguments:
答案就再 configure 里,通俗的讲是在构建项目这一步
zhuangzhuang1988
2016-07-22 21:16:42 +08:00
看源码吧...
SErHo
2016-07-22 21:16:49 +08:00
就拿 Nginx 那个来说,当执行 ./configure 后,会生成几个头文件,其中有个 ngx_auto_config.h 里面就有一个宏定义为配置信息。所以实现方法大概就是 1 楼说的咯。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/294297

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX