如何在c ++中单独编译源代码 [英] How to compile source code separately in c++

查看:170
本文介绍了如何在c ++中单独编译源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我希望有人能帮助我解决这个我在过去两个星期努力解决的根本问题。我有一个包含4个项目的解决方案,项目文件所依赖的一些库。在每个这些项目中,包括一个logic.cpp文件的副本,它包含一个长的逻辑列表,在伪代码如下:

  BOOL myLogic(){

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

case 2:
doB();
break;

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

生成工具的exe。而对于项目#2,它生成我正在建设的工具的dll版本和其他2个项目,它们作为我的工具的实用程序文件。如果你注意到有20个案例,逻辑可以运行,它是相当大。



所以,我现在的问题是所有这些源代码被编译到我的单个exe或dll,即使这些情况下甚至可能没有达到时部署在一些场景。我想要实现的是打破这个开关情况,并编译20个不同的exe和dll集。因此



1)应用程序占用空间较小。
2)当反向工程时,来源可以在一定程度上受到保护。



因此,我想征求社群的意见,关于解决这个问题,如果我仍然想继续使用Visual Studio的内置编译。 (我可以用Build Solution构建20套exe和dll)。



谢谢,我很感激任何建议。如果我在我的问题中不够清楚,请随时澄清。

解决方案


  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.

这篇关于如何在c ++中单独编译源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆