0

I have an external dependency in my project which I can't change. It is built with some very complex Python which calls some more build tools.

This all eventually boils down to compile error: the C++ just cant find a header file.

The header file is included like this:

#include <sqlite3.h>

The question:

Is it possible to add a custom include directory to Visual Studio C++ with the help of just environment variables. And if it is - how?

I can't change the solution nor project nor source files - they all are being download automatically

I'm using most recent visual studio - 2017

LoLance
  • 25,666
  • 1
  • 39
  • 73
Yuri Yaryshev
  • 991
  • 6
  • 24
  • 1
    Don't know about environment variables (is that *really* your requirement or is this a classic xy-problem?), but if your question is 'add header location withot modifying project files' then the principles here https://stackoverflow.com/questions/15141429/how-to-set-preprocessordefinitions-as-a-task-propery-for-the-msbuild-task (use `AdditionalIncludeDirectories` instead of PreprocessorDefinitions) or here https://stackoverflow.com/questions/2676417/how-do-include-paths-work-in-visual-studio work. – stijn Jun 07 '19 at 07:37
  • @Yuri, hi friend, any update for this issue? If the environment variable can't help, maybe you can try using Directory.build.props file. – LoLance Jun 11 '19 at 03:23

1 Answers1

0

Is it possible to add a custom include directory to Visual Studio C++ with the help of just environment variables.

I'm afraid the answer is negative.It's not possible to achieve this goal only with Environment Variable.

  1. Please check this: When compiling the project in VS, if the compiler encounters an #include statement, it tries to open the specified file. If the file is an absolute path, it only tries to load from that specific path.

If the file is a relative path, it tries to load from the directory of the file being compiled first. If the file is not found in the same folder as the cpp file, the compiler tries each of the paths in its 'Include Directories' list to find the file.

  1. And in VS IDE, we can set the Include Directories or Additional Include Directories to set the search path.So if I have a header file Test.h, and I use statement like #include <AbsolutePath\Test.h>, the compiler can find the header.

If i use statement like #include <Test.h>, the header file can't be found until I set the search path. In this situation, I can set the Additional Include Directories for all both debug and release to make the compiler find the header file (Test.h locates in Company folder):

enter image description here

Note: But one point we should know is most of the time, when we change the settings in Project=>Properties we actually is modifying the project file.(xx.vcxproj).

As you mentioned above, we can't make any change to project files, so we can't achieve the goal by this way. Instead i think you can try using Directory.build.props file.

Create a Directory.Build.props file, move it to root directory of your project folder. For me: C:\Users\userName\source\repos folder. Add the content like below into it:

<Project>
 <ItemDefinitionGroup>
    <ClCompile>
      <AdditionalIncludeDirectories>C:\Company;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    </ClCompile>
  </ItemDefinitionGroup>
</Project>

We can put the header file into the C:\Company(We can specify our folder in this way) folder and the compiler can find it. Also, we're not doing any change to Solution, project or source file.

Please let me know if it's helpful:) Any feedback feel free to contact me.

LoLance
  • 25,666
  • 1
  • 39
  • 73