#if DEBUG directive in compiled class library

非 Y 不嫁゛ 提交于 2019-12-12 05:06:01

问题


I am going to use #if DEBUG directive in a class library. The compiled ClassLibrary.dll will be used in a separate Application.exe.

Will my debug code from this class library be executed in the following situations?

  1. library is compiled in DEBUG mode, application is compiled in RELEASE
  2. library is compiled in RELEASE, application is compiled in DEBUG

回答1:


A compiler directive is interpreted at compile time and not at runtime. Therefore it does not matter, if the using application is compiled in RELEASE or DEBUG mode. Therefore

1.) library is compiled in DEBUG mode, application is compiled in RELEASE => Yes

2.) library is compiled in RELEASE, application is compiled in DEBUG => No




回答2:


I had a similar problem wherein, the release mode was still picking up debug mode settings though I configured debug mode settings in the debug preprocessor directive. Turns out the problem was with how the reference dll of the class library project was added to my project. There are 2 ways to add a reference to your project.

Option1: Add .dll from debug/release folder of the project to the project that is dependent on it. Problem: This introduces a problem, where in if you have added reference to the debug version of the dll, even if you run the application in release mode, its still going to refer to the debug version.
Fix: You will have to replace the .dll corresponding to what mode you want to run in. Eg: If you are going to want to run in release mode, you will have to replace the dependency dll with its corresponding release version.

Option2: Add the project reference (.csproj) file of the project you want to refer.
Advantage: This ensures when the whole solution is built in release mode, it correctly resolves all dependencies to be in release mode and when you build the solution in debug, it gets all dependency projects in debug mode. This doesn't need your intervention.

Where is option1 useful? -> When the projects your project wants to reference don't exist in the same solution as yours.. Think of scenarios where you want to use 3rd party dlls.. In this case you either don't have the 3rd party code but got only the dlls or it lies in a different solution.

When is option2 useful? -> When both referencing project and referenced project exist in the same solution (this is my scenario). If you remember, all my projects exist in the same solution.

Link that explains how to add project references in 2 ways: https://www.c-sharpcorner.com/article/project-reference-vs-dll-reference-in-visual-studio/



来源:https://stackoverflow.com/questions/40320115/if-debug-directive-in-compiled-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!