简体   繁体   中英

How to compile source code separately in c++

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.

What about some handwork? Indroduce some defines for your scenarios or use some standard ones like "_ISDLL" and encase the cases :-) from which you know they can not be reaches in "#ifdefs"

#ifdef _ISDLL
case x:

    break;
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM