且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在c ++中单独编译源代码

更新时间:2023-01-30 22:00:56


  1. 创建一个新项目,编译成静态库。在该项目中为所有20个功能创建单独的源cpp文件。 (拆分到更多的源文件只是为了可维护性。)将logic.cpp分成20个单独的文件。


  2. 现在创建2x20个新项目:20个exe项目和20个dll项目。这些项目中的每一个都取决于在步骤1中创建的静态库项目,并且所有这些项目只是一个简单的存根,用于调用公共库中的一个功能。


当您构建解决方案时,每个功能将有20个不同命名的可执行文件和20个不同命名的dll。如果在链接器中打开了死代码消除,则没有exes / dll将包含特定函数不需要的代码。


I hope someone could help me address this fundamental problem that I have been trying to tackle for the last two weeks.

I have a solution that contains 4 projects, some libraries that the project files depend on. In each of these project, a copy of logic.cpp file has been included and it contains a long list of logic which in pseudo codes looks like this:

BOOL myLogic(){

 if(...)
 {
   switch(...)
   {
   case 1:
      doA();
      break;

   case 2:
      doB();
      break;

   ...
   case 20:
      doSomething();
      break;
  } 
 }
}

For project #1, it generates an exe of the tool. While for project #2, it generates the dll version of the tool that I'm building and the other 2 projects, they act as utility files for my tool. If you notice there are like 20 cases that the logic can run into and it is pretty massive.

So, my problem now is that all these source codes are being compiled into my single exe or dll even when some of these case may not even be reached when deployed in some scenarios. What I want to achieve is to break this switch case and compile 20 different sets of exe and dll. So

1) The application has a smaller footprint. 2) The sources could be protected to a certain extent when reverse engineered.

Hence, I would like to seek advise from the community on how do I go about solving this problem, if I would like to still continue using Visual Studio's inbuilt compilation. (I could build the 20 sets of exe and dll with the "Build Solution").

Thank you and I appreciate any advice. Feel free to clarify if I have not been clear enough in my question.

  1. Create a new project, that compiles into static library. In that project create separate source cpp files for all the 20 functionalities. (Splitting to more source files are just for the sake of maintainability.) Split logic.cpp into the 20 separate files. If there are common code parts, you can create more source files to contain those parts.

  2. Now create 2x20 new projects: 20 exe projects and 20 dll projects. Each of these projects depends on the static library project created in step 1, and all of these projects are nothing but a simple stub for calling exactly one of the functionalities from the common library.

When you build the solution, you will have 20 differently named executables and 20 differently named dlls, for each functionality. If dead code elimination is turned on in the linker, then none of the exes/dlls will contain code that is not required for the specific function.