athos athos - 1 month ago 6x
C++ Question

How to split a C++ project into two in Visual Studio?

Scenario 1, under solution SolA there's only 1 project named PrjA. PrjA is a Win32 console application, the compilation result is PrjA.exe.

How shall I split PrjA int PrjA1 and PrjB, so that the management of the source code is easier, however, the compilation result, PrjA1.exe, will be almost the same as PrjA.exe?

What I want to split is the source code files, but I hope the compilation result, the exe file remains almost the same after the code split.

For example, PrjA originally has 300 .cpp source code files, compiled to PrjA.exe sized 400KB. I hope to split PrjA into 2 projects: PrjA1, containing 200 .cpp files, and PrjB, containing 100 .cpp files. But after compilation, I hope PrjA1.exe is also around 400KB.

I'm not sure how to set PrjB, what will be its compilation result. Assume PrjB compiles to PrjB.DLL, sized 100KB, I hope PrjA1.exe will somehow embed PrjB.dll inside, sized 400KB. I don't want a 300KB PrjA1.exe, which will dynamically link to 100KB PrjB.dll in runtime.

So question for scenario 1, the question is, how shall I set PrjB, and how shall I set the link between PrjA1 and PrjB?

Scenario 2, same as Scenario 1, just this round PrjA's compilation result is a windows DLL, named PrjA.dll, how shall I split PrjA into PrjA1 and PrjB?

The development environment is Visual Studio 2013 under windows 7 64-bits, but the compilation results are 32-bit.


This is how you move existing code into static library. I use VS2015, it should be the same with VS2013, but I can't check that. I start with the following solution structure: Initial solution structure, all code in one project

First thing to do is to add new project to the solution. File -> Add -> New project -> Win32 Project

Check Static library and uncheck precompiled header Win32 Application wizard

Then click Show all files icon in the solution explorer for both projects. This turn filters off and shows project folder as it is. It should look like this: Solution explorer shows files in project folders

Drag and drop all needed files from one project to another: Project structure after relocation

Now reference your library. Uncheck Show all files icon to show References. Right click -> Add reference and check your static library project: enter image description here enter image description here

Last thing to do is to add include directory to the project that uses the library. This is done in project properties. Be sure to check All configurations and All platforms: enter image description here

That's it. Now when you build your solution, VS builds your static library, builds your main project and links them together. The result should be identical as if everything is in the same project.