3

Suppose we have a pretty big amount of files in a project, so writing #include "somefile" to all of them will take a long time, so it there a way to include "somefile" into all files in the project?

  • 5
    It won't take a long time if you write a script that does the addition. I recommend you to consider *why* you would want to include a header in all project files. Do all those files really depend on the header? If they do, then why aren't they already including it? If they don't then is there any reason to include it? – eerorika Jan 18 '17 at 10:06
  • 1
    In GCC: `-include`. – Kerrek SB Jan 18 '17 at 10:09
  • Why would you want to force all files to include something that has not been necessary up to now? Are you playing some "clever" preprocessor tricks? Because, if you are, you shouldn't. And if you are not, there should be no need to force inclusion into files that do not syntactically require it, just add the `#include` to all files where the compiler asks for it. – cmaster - reinstate monica Jan 18 '17 at 17:04

3 Answers3

3

Some compilers have an option for this. For example, the option for GCC is -include.

-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.


Of course, using such option will make your project depend on the support for such compiler feature. A portable solution is to simply edit all your files.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
3

If it is visual studio, use the /FI option (force include).

https://msdn.microsoft.com/en-us/library/8c5ztk84.aspx

Sven Nilsson
  • 1,861
  • 10
  • 11
1

If you are working with an IDE surely there must be a mechanism of "find and replace" that you can use!

The idea here would be to find a string (another #include for example) that you know that exists in the same place that you want to put the #include's and replace it with your new #include concatenated with a newline and the string that was already there. The trick here is to do it across all files at the same time.

Examples on some common IDE's:

-Visual Studio - Visual Studio Code Replace multiple files at once

-Eclipse - Is there a way to find/replace across an entire project in Eclipse?

-Notepad++ - How To “Find And Replace” Words In Multiple Files

Community
  • 1
  • 1
Garcia
  • 36
  • 4