Search code examples
c++windowsvisual-studioheader-files

Dependent project cannot see headers from a library in refferences in Visual Studio


I am learning how to make a static library. I started with windows and Visual Studio.

The directory structure looks like this:

 - MyLibraryProject
   - include
     - MyLibraryProject
       - MyLibraryHeader.h
   - src
     - MyLibrarySource.cpp
   - build
     - MyLibraryProject.vcxproj
 - MyDependentProject
   - main.cpp
   - MyDependentProject.vcxproj

MyLibraryProject.vcxproj has the following settings:

Setting Value
Configuration type Static library (.lib)
Additional Include Directories $(MSBuildThisFileDirectory)../include/MyLibraryProject

MyDependentProject.vcxproj has no special settings, except I added MyLibraryProject onto refferences, the image features actual names I used:

enter image description here

If I use relative paths in main.cpp, I can build the project - the static linking works just fine and it runs:

#include "../MyLibraryProject/include/MyLibraryProject/MyLibraryHeader.h"

However, I want to include the headers like this:

// fatal error C1083: Cannot open include file: 'MyLibraryProject/MyLibraryHeader.h': No such file or directory
#include <MyLibraryProject/MyLibraryHeader.h>

And that just does not work. I also tried to use property sheet but couldn't get that to work either. I've been searching the internet, but generally found claims that if you add a reference, both headers and static libs will work.

Here's the full repository, if you're willing to take a look. Or ask in the comments if there's information missing.


Solution

  • Project references do not provide the dependent project with any information about headers. The most flexible way to do this instead (in Visual Studio) are property sheets. I created a file MyLibraryProject/build/MyLibraryProjectDependency.props:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <ImportGroup Label="PropertySheets" />
      <PropertyGroup Label="UserMacros" />
      <PropertyGroup />
      <ItemDefinitionGroup>
        <ClCompile>
          <AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
        </ClCompile>
      </ItemDefinitionGroup>
      <ItemGroup />
    </Project>
    

    And I added it to MyDependentProject.vcxproj in Property explorer in Visual Studio. This solved the issues and headers are now seen on the path I want them.