AdmiralMyxtaR
AdmiralMyxtaR

Reputation: 95

Is there a way to include a file into all project files in C++?

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?

Upvotes: 3

Views: 524

Answers (3)

eerorika
eerorika

Reputation: 238431

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.

Upvotes: 3

Garcia
Garcia

Reputation: 36

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

Upvotes: 1

Sven Nilsson
Sven Nilsson

Reputation: 1879

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

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

Upvotes: 3

Related Questions