有没有办法在每个编译单元中包含一个头文件而不修改每个源文件? [英] Is there a way to include a header in every compilation unit without modifying every source file?

查看:117
本文介绍了有没有办法在每个编译单元中包含一个头文件而不修改每个源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下内容:


  • 包含数千个C ++源文件的大型项目

  • 不共同头文件(每个源文件中都没有包含任何头文件)
  • 表示项目使用g ++编译并由make管理


    有没有办法在每个编译单元中包含一个定义(例如宏)而不修改每个源文件以包含新的头文件?

    解决方案

    从man gcc:


    - 包含文件



    处理文件就好像#include文件作为主源文件的第一行
    出现。但是,第一个搜索
    文件的目录是预处理程序的工作目录,而不是包含主源文件的目录
    。如果在那里没有找到,那么在正常情况下在#include...搜索链
    的剩余部分中搜索

    如果给出了多个-include选项,则这些文件将包含在
    中,这些文件将显示在命令行中。



    Given the following:

    • large project with thousands of C++ source files
    • no common header file (no one header that's included in every source file)
    • said project is compiled with g++ and managed by make

    Is there any way to include a definition (e.g. macro) into every compilation unit without modifying every source file to include a new header file?

    解决方案

    From man gcc:

    -include file

    Process file as if "#include "file"" appeared as the first line of the primary source file. However, the first directory searched for file is the preprocessor's working directory instead of the directory containing the main source file. If not found there, it is searched for in the remainder of the "#include "..."" search chain as normal. If multiple -include options are given, the files are included in the order they appear on the command line.

    这篇关于有没有办法在每个编译单元中包含一个头文件而不修改每个源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆