How share header & source codes in VC++

drjackool 1,016 Reputation points
2026-09-06T05:14:59.1+00:00

Hi

In Visual C++, I have several headers and sources that I want use them in several projects and they are common.

I put them in a folder named 'Common' in my projects folder like below:

[My Projects]
+--[Common]
   +--MyCode.h
   +--MyCode.cpp
+--[Project 1]
+--[Project 2]
...

And in project settings in VC Directories I add 'Common' folder location in "include" ans "source" folders. But problem is when compiling the project I get "Unresolved external symbol" because the compiler not compiles the source codes 'MyCode.cpp'. How to solve this problem?

I alters 'MyCode.cpp' some times and I do not want build MyCode.lib.

Also I do not want add files in Solution Explorer. I want use my common codes like VC++ headers

Thanks

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


Answer accepted by question author

AI answer

2026-09-06T05:15:13.8466667+00:00

Adding the Common folder to Include Directories only makes headers visible to the compiler. It does not cause MyCode.cpp to be compiled or linked, which is why unresolved external symbol occurs.

Use one of these supported approaches:

  1. Add the shared .cpp file to each project
    • A file is compiled only when it is part of the project.
    • Files shown in Solution Explorer are added to all configurations for that project.
    • If MyCode.cpp is not in the project, Visual C++ does not build it just because its folder is listed in VC++ Directories.
  2. Create a separate project for the common code
    • Put MyCode.cpp and MyCode.h in a static library or DLL project.
    • Then set project dependencies so dependent projects build in the correct order and can find the outputs at link time.
    • This is the recommended way to share native code across multiple projects.
  3. If only headers are shared, keep using Include Directories
    • Header files can be shared through the include path.
    • Source files cannot be shared that way; they must be compiled as part of a project or library.

If the goal is to avoid building a separate .lib, the practical option is to add MyCode.cpp into each consuming project so it gets compiled there. If the goal is to maintain one compiled copy reused by many projects, use a separate library project.

Also check these project settings if the file is already present but still not found:

  • Configuration Properties > VC++ Directories > Include Directories for MyCode.h
  • Configuration Properties > Linker > Input > Additional Dependencies and Linker > General > Additional Library Directories if using a .lib

VC++ Directories does not provide a general “source search path” that makes .cpp files compile automatically.


AI-generated content may be incorrect. Read our transparency notes for more information.

Was this answer helpful?

1 person found this answer helpful.

2 additional answers

Sort by: Oldest
  1. RLWA32 52,816 Reputation points
    2026-09-06T07:31:32.6933333+00:00

    You can use a Directory.Build.props file as described in Customize C++ builds. The props will be applied to every C++ project created under the folder tree containing the file. It can provide those projects with the common headers and can also be used to provide MyCode.cpp to the projects.

    Folders -

    folders

    For example, the Common folder contains func1.h, func2.h and MyCode.cpp (definitions of func1 and func2).

    Directory.Build props contains -

    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
         <ForceImportAfterCppProps>$(MsbuildThisFileDirectory)\MyCommon.props</ForceImportAfterCppProps>
      </PropertyGroup>
    </Project>
    

    MyCommon.props contains -

    <?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 Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
        <ClCompile>
          <AdditionalIncludeDirectories>C:\Users\RLWA32\source\repos\RlwA32\MyProjects\Common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
        </ClCompile>
      </ItemDefinitionGroup>
      <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
        <ClCompile>
          <AdditionalIncludeDirectories>C:\Users\RLWA32\source\repos\RlwA32\MyProjects\Common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
        </ClCompile>
      </ItemDefinitionGroup>
      <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
        <ClCompile>
          <AdditionalIncludeDirectories>C:\Users\RLWA32\source\repos\RlwA32\MyProjects\Common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
        </ClCompile>
      </ItemDefinitionGroup>
      <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
        <ClCompile>
          <AdditionalIncludeDirectories>C:\Users\RLWA32\source\repos\RlwA32\MyProjects\Common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
        </ClCompile>
      </ItemDefinitionGroup>
      <!-- Comment out the following ItemGroup to prevent MyCode.cpp from automatic inclusion in projects -->
      <ItemGroup>
        <ClCompile Include="C:\Users\RLWA32\source\repos\RlwA32\MyProjects\Common\MyCode.cpp" />
      </ItemGroup>
    </Project>
    

    For example, the TestCommon1 project appears like this -

    TestCommon1

    Note that MyCode.cpp was automatically included in your project without any manual intervention. And it would be included in every other project created under the folder tree.

    If this is undesirable, comment out the ItemGroup as described above. If you want to include MyCode.cpp in some projects but not others you would need to manually cause it to be included. An ugly way of accomplishing the would be to use the #include directive for MyCode.cpp.

    For example,

    TestCommon2

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

  2. Tony Thach (WICLOUD CORPORATION) 1,120 Reputation points Microsoft External Staff Moderator
    2026-09-07T03:02:22.1833333+00:00

    Hi  @drjackool , and thanks for posting your question. 

    Adding Common to VC++ Directories > Include Directories only tells the preprocessor where to find:

    #include "MyCode.h"
    

    It does not cause MyCode.cpp to be compiled. A .cpp file must be a ClCompile item in the consuming project, imported through MSBuild, or compiled in another project. Otherwise, no .obj containing its definitions is produced, which results in unresolved external symbol.

    There is no C++ “source search path” that automatically compiles every .cpp file in a directory.

    Recommended: use a Shared Items Project

    A C++ Shared Items Project (.vcxitems) is designed for this scenario. It does not produce a .lib or .dll. Instead, its files become part of each referencing project's build. Microsoft documents that a .vcxitems project is not built independently and that its files participate in the referencing project's build process.

    project-and-solution-files

    1. Select Solution > Add > New Project.
    2. Create a Shared Items Project, for example Common.
    3. Add MyCode.h and MyCode.cpp to that shared project.
    4. In each consuming C++ project, add a reference to the Common shared project.

    The resulting project import is equivalent to:

    <ImportGroup Label="Shared">
      <Import Project="..\Common\Common.vcxitems" Label="Shared" />
    </ImportGroup>
    

    This gives you:

    • One physical copy of MyCode.cpp
    • No separate library output
    • Automatic compilation in every referencing project
    • Immediate rebuilding when MyCode.cpp changes

    The shared project and consuming projects normally need to be loaded in the same solution. Although the shared project appears in Solution Explorer, you do not need to add MyCode.cpp manually to every consuming project.

    Alternative: apply it automatically with Directory.Build.props

    If all projects are below one parent folder and you want MSBuild to include the common source automatically, use folder-level build customization. Directory.Build.props applies to projects under its directory tree, and Microsoft documents this approach for C++ projects.

    customize-cpp-builds

    Folder layout:

    MyProjects
    |
    +-- Directory.Build.props
    +-- MyCommon.props
    |
    +-- Common
    |   +-- MyCode.h
    |   +-- MyCode.cpp
    |
    +-- Project1
    +-- Project2
    

    Directory.Build.props:

    <Project>
      <PropertyGroup>
        <ForceImportAfterCppProps>
          $(MSBuildThisFileDirectory)MyCommon.props
        </ForceImportAfterCppProps>
      </PropertyGroup>
    </Project>
    

    MyCommon.props:

    <Project>
      <PropertyGroup>
        <CommonDir>$(MSBuildThisFileDirectory)Common</CommonDir>
      </PropertyGroup>
    
      <ItemDefinitionGroup>
        <ClCompile>
          <AdditionalIncludeDirectories>
            $(CommonDir);%(AdditionalIncludeDirectories)
          </AdditionalIncludeDirectories>
        </ClCompile>
      </ItemDefinitionGroup>
    
      <ItemGroup Condition="'$(UseCommonSources)' == 'true'">
        <ClCompile Include="$(CommonDir)\MyCode.cpp" />
      </ItemGroup>
    
    </Project>
    

    Then enable the common source in selected .vcxproj files by defining the property before Microsoft.Cpp.props is imported, for example in the Globals property group:

    <PropertyGroup Label="Globals">
      <!-- Existing project properties -->
      <UseCommonSources>true</UseCommonSources>
    </PropertyGroup>
    

    Projects without that property still receive the include directory but do not compile MyCode.cpp.

    This version avoids hard-coded paths such as C:\Users\..., works in source control and on other computers, and does not require separate definitions for Debug/Release and Win32/x64.

    Property sheet option

    For explicit per-project control, place the same ItemDefinitionGroup and ItemGroup in a reusable .props file, then attach it through:

    View > Property Manager > Add Existing Property Sheet

    Property sheets are the supported way to share reusable C++ project settings.

    create-reusable-property-configurations

    Avoid including the .cpp directly

    This can appear to work:

    #include "MyCode.cpp"
    

    However, it is generally not recommended. If more than one translation unit includes that file, the same definitions can be compiled multiple times, resulting in duplicate-symbol or One Definition Rule problems.

    If the implementation is intended to behave like header-only code, move it into the header and mark appropriate non-template functions inline. Otherwise, use a Shared Items Project or an MSBuild .props import.

    For the requirements described, Shared Items Project is the closest match. Use Directory.Build.props instead when the source should be included automatically based on the projects' folder hierarchy.

    If this instruction is applicable to your situation, I would greatly appreciate it if you could follow the instruction here so others experiencing similar behavior can benefit from it as well.

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.