aluky
aluky

Reputation: 531

#if DEBUG directive in compiled class library

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

Upvotes: 1

Views: 1366

Answers (2)

theandroid
theandroid

Reputation: 943

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/

Upvotes: 0

Ralf Bönning
Ralf Bönning

Reputation: 15415

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

Upvotes: 6

Related Questions