V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fyyz
V2EX  ›  C

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

  •  
  •   fyyz · 2016-07-22 20:55:57 +08:00 · 1563 次点击
    这是一个创建于 2840 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    # 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++)的版本之类的信息。

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

    4 条回复    2016-07-22 21:16:49 +08:00
    msg7086
        1
    msg7086  
       2016-07-22 21:11:19 +08:00
    把参数写入头文件里,然后把头文件编译到程序中咯。
    congeec
        2
    congeec  
       2016-07-22 21:14:54 +08:00
    configure arguments:
    答案就再 configure 里,通俗的讲是在构建项目这一步
    zhuangzhuang1988
        3
    zhuangzhuang1988  
       2016-07-22 21:16:42 +08:00
    看源码吧...
    SErHo
        4
    SErHo  
       2016-07-22 21:16:49 +08:00
    就拿 Nginx 那个来说,当执行 ./configure 后,会生成几个头文件,其中有个 ngx_auto_config.h 里面就有一个宏定义为配置信息。所以实现方法大概就是 1 楼说的咯。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2117 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:28 · PVG 18:28 · LAX 03:28 · JFK 06:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.