15

As the title says: Is there a pragma directive for include directories using VS20** C++? I think about something like:

#pragma comment(include, "..\externals\blah\includes\")

So that I can use includes in this style, and blah.h also can use this style inside?

#include <blah.h>

I know that I can set include directories in my project settings, but I am asking for a preprocessor directive, because else i would have to set it for every compiler profile.

Regards Nem

Christoph Meißner
  • 1,511
  • 2
  • 21
  • 40

6 Answers6

9

you can create a txt file (ex. IncludeDirs.txt). Inside that file you can add the include folders:

/I "."
/I ".."
/I ".\OtherFolder"

then in the properties->configuration properties->C/C++-> Command line add the following string:

@includedirs.txt

You can create a different file for each profile (Debug, Release, etc.)

FreeRideX
  • 91
  • 1
  • 1
  • Nice - only downside is it being buried in the project settings in a 'non-standard' location, but if it only needs to be done once, it might make things a lot easier.. – ZXcvbnM Mar 22 '16 at 20:06
  • Great idea! Linux equivalent (using clang by happenstance): `clang++ $(cat CompilerOptions.txt) ...` – Andrew Mar 29 '22 at 08:24
4

I don't think there's any way to do this. include_alias is only useful on a file-by-file basis.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
2

You can create a .props file that contains selected project properties of choice.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ImportGroup Label="PropertySheets" />
    <PropertyGroup>
        <IncludePath>C:\ExampleDir\Include;$(IncludePath)</IncludePath>
    </PropertyGroup>
</Project>

You can move blocks of existing properties from your .vcxproj file(s).
It may also contain per Configuration conditions for properties (this is also something you can learn from insides of a .vcxproj file).

Important: The .props file needs to be referenced from the Property Manager tab

[View] - [Property Manager] menu - select [Add Existing Property Sheet...]

while focusing a specific build configuration.

These property files are well reusable between different projects. Combined with Environment Variable values (e.g. use $(MySdkPath) instead of direct "C:\MySdk") this approach provides a good and universal solution.

ValB
  • 31
  • 5
1

I am not aware of any.

A while ago I solved this problem by making "all" and "all.cpp" files in every directory to include each header and source file in it. It needs some manual work to create and maintain but I believe it is worth it. This way I can just write something like #include <Frigo/Math/all>. It might be even possible to create a script that updates them automatically.

Frigo
  • 1,709
  • 1
  • 14
  • 32
1

You could add a "master folder" to your project properties and then put the right folder in the include directive, for example:

folder "includes" with subfolders "Unicode"/"ASCII", both with a header-file named "String.h" (you shouldn't use the same name)

in code use:

#ifdef UNICODE // or your preprocessor flag
#  include <Unicode/string.h>
#else 
#  include <ASCII/string.h>
#endif

you also can use DEBUG or any keyword, as long as you specify it in Project-Settings --> C/C++ --> Preprocessor --> Preprocessor defintions

Bitmap
  • 12,402
  • 16
  • 64
  • 91
BeschBesch
  • 373
  • 2
  • 11
  • Sorry, but I know that. I'm not sure whether you understand my question correctly. If you would have read my post, you would know that this is not what i intended with my question. – Christoph Meißner Aug 15 '11 at 21:17
1

I am coming too late, but I tried

#define path_include <C:/foo_folder/##a>

and then

#include path_include(blah.h)

#include path_include(bluh.h)

It works.

Jonathan Ciapetti
  • 1,261
  • 3
  • 11
  • 16
  • I think the asker wants to modify the include path with a pragma. I don't think this answers their question. – starball Sep 02 '22 at 17:38
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 04 '22 at 12:12
  • This is a clever idea, but it would be imperative to be able to preserve existing `#include`s without modifying them – Jonathan Aug 03 '23 at 01:58