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
- Select Solution > Add > New Project.
- Create a Shared Items Project, for example
Common.
- Add
MyCode.h and MyCode.cpp to that shared project.
- 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.