0

I have a Visual Studio 2010 solution with several very closely related projects. Would like to get away from copy-paste code sharing and share some common classes between the projects. We have tried adding a reference to the class from project A into project B, but are getting compile errors where the shared class is referencing the wrong stdafx.h file. Note that precompiled headers are on (by default) for both projects.

We have also learned that it is not possible to #ifdef the correct stdafx.h file using preprocessor definitions.

At this point it looks like our options are to either build a static library project or multiple projects and reference that from both project A & B, or to try some pre-build event copying of the correct stdafx.h file into the shared-code subfolder. Third would be to re-architect both projects to not use stdafx.h at all.

All of these options seem like overkill to me. Is there an easier solution that I am missing?

Community
  • 1
  • 1
jacobsee
  • 1,438
  • 4
  • 18
  • 34
  • I think, best option is indeed creating a static library with the common code, as you spotted already... – Aconcagua Jun 14 '16 at 01:51
  • 1
    One solution is to build with precompiled headers turned off. This gets rid of stdafx.h entirely. – user4581301 Jun 14 '16 at 01:57
  • 1
    Have you tried to create a new filter and add the classes from other projects within that filter. This can help make it clear where the other projects classes are from and you can add the stdafx.h to that filter. – amisam Jun 14 '16 at 02:01

3 Answers3

1

The two that popped straight into my head when I was reading your question was to:

  1. turn off precompiled headers.
  2. compile the common code as a shared library.

In your instance 1 would allow you to be more productive quicker, but don't delete stdafx from the project it as that would mean having to go through your project and turning off the precompiled header. Simply remove all the stuff you added and leave it default for both projects. It's been a while since I had to do this, but I think I then would then right-click and compile each cpp file individually. This would give you the missing header files for that compilation unit. Add them and then rinse and repeat for each.

Running code through static analysis would highlight things that could speed up compilation (forward declares vs includes, etc.).

graham.reeds
  • 16,230
  • 17
  • 74
  • 137
1

When I have run into this problem I fixed it by:

  1. Rename all stdafx.h files specific to their project (stdafx_prj_A.h, stdafx_prj_B.h, etc.).
    • Also, you will need to update each solution to use the correct precompiled header file. The setting for this is in Configuration Properties -> C/C++ -> Precompiled Headers -> Precompiled Header File.
    • Do not forget to make this set for all Configurations and Platforms.
    • Be safe. Each StdAfx.cpp only needs to include the correct header file. So do not include all of the stdafx headers in 1 StdAfx.cpp file.
  2. Move any classes that are shared between projects to their own project.

This still allows you to use PCH files, and you do not have to worry about the compiler using the wrong file. If I recall correctly VS will use the currently specified PCH file name for any new class that you create.

tc2617
  • 66
  • 3
0

We create .cxx files with the original source EXCEPT for the #include "stdafx.h". This is the main file that implements the class.

Then we create separate project specific .cpp files that just contain (for example NumEdit.cpp)

#include "stdafx.h"
#include "\pathtoimplementation\NumEdit.cxx"

Then each project's debug and release folders have the project specific .obj file.

franji1
  • 3,088
  • 2
  • 23
  • 43