472,782 Members | 1,304 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

Conditional compilation question

I'm a C++ programmer of many years, trying to get my feet wet in C#.

I have a question about conditional compilation. In C++, I would
sometimes define a constant in an include file, and then have blocks of
code in different source files that were conditionally compiled based
on that constant. Now that C# has done away with include files, is
there any way of doing the same thing, short of defining the constant
multiple times at the head of each source file that has the
conditionally compiled code? There must be a better way...

Thanks.

- Dave

Jun 8 '06 #1
10 3026
Dave,

There are conditional complier statements, and its done the same way
you would do it in C++, via the #if #endif statements.

In VS2005, open project properties and goto the Build tab; theres a
text box to define if the constants exist or not.

Be careful though, it is harder to test an assembly which uses
conditional compilation (as I'm sure you know).

Andy

Dave wrote:
I'm a C++ programmer of many years, trying to get my feet wet in C#.

I have a question about conditional compilation. In C++, I would
sometimes define a constant in an include file, and then have blocks of
code in different source files that were conditionally compiled based
on that constant. Now that C# has done away with include files, is
there any way of doing the same thing, short of defining the constant
multiple times at the head of each source file that has the
conditionally compiled code? There must be a better way...

Thanks.

- Dave


Jun 8 '06 #2
Dave,

C# supports conditional compilation, but it is more simple that the one in
C++
1. As you noted already there is no header files and because of this you
have to either define the preprocessor symbol in each file or you can define
it gloabally for the project in the project settings.

2. You can only define a symbol (giving it a name) unlike C++ you cannot
give it a value and then comapare against it value.

For example you can

#define VS_2005
....

#if VS_2005
....
#else
....
#endif

You can check only if some name is declared. Preprocessor declarations of
version numbers and checking with < , > or = are not supported.
--
HTH
Stoitcho Goutsev (100)
"Dave" <dp*@eliassen.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
I'm a C++ programmer of many years, trying to get my feet wet in C#.

I have a question about conditional compilation. In C++, I would
sometimes define a constant in an include file, and then have blocks of
code in different source files that were conditionally compiled based
on that constant. Now that C# has done away with include files, is
there any way of doing the same thing, short of defining the constant
multiple times at the head of each source file that has the
conditionally compiled code? There must be a better way...

Thanks.

- Dave

Jun 8 '06 #3
Thanks, all.

Yes, I already know all this, but it still seems like C# is lacking in
this regard relative to C++. In my case, I am referencing several
dll's from within my solution, and I want the conditional compilation
constant to be defined or not for all the code in the solution,
including the dll's. There isn't a way to have a constant defined for
the entire "solution", is there? In C++, this is easily done by having
a #define in an include file that all the dll and library modules in
the project include. This is done all the time...

- Dave

Jun 8 '06 #4
If you're using msbuild you could do:

msbuild /property:DefineConstants=TRACE;DEBUG;Custom. MySolution.sln

-Brian

"Dave" <dp*@eliassen.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
Thanks, all.

Yes, I already know all this, but it still seems like C# is lacking in
this regard relative to C++. In my case, I am referencing several
dll's from within my solution, and I want the conditional compilation
constant to be defined or not for all the code in the solution,
including the dll's. There isn't a way to have a constant defined for
the entire "solution", is there? In C++, this is easily done by having
a #define in an include file that all the dll and library modules in
the project include. This is done all the time...

- Dave

Jun 8 '06 #5
Dave,
In my case, I am referencing several
dll's from within my solution, and I want the conditional compilation
constant to be defined or not for all the code in the solution,
including the dll's.
This is not even possible in C++. These are pre-processor directives as such
they are taken into consideration before compilaton. If you reference a dll
this dll is already compiled, so all directives has been already processed
the sam if you implicitly link to a dll in C++. There is no #define in
already compiled code.
--

Stoitcho Goutsev (100)

"Dave" <dp*@eliassen.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com... Thanks, all.

Yes, I already know all this, but it still seems like C# is lacking in
this regard relative to C++. In my case, I am referencing several
dll's from within my solution, and I want the conditional compilation
constant to be defined or not for all the code in the solution,
including the dll's. There isn't a way to have a constant defined for
the entire "solution", is there? In C++, this is easily done by having
a #define in an include file that all the dll and library modules in
the project include. This is done all the time...

- Dave

Jun 8 '06 #6
Note that, as dictated by the standard, constant if() expressions are
evaluated at compile time, So, you can do, in C# code, what you were
doing with the preprocessor:

class Config
{
public const bool EnableThis = false;
}
if (Config.EnableThis)
{
/// blah-blah-blah
}
else
{
/// otherwise
}

Only the "otherwise" code will be compiled into the assembly.

Dave wrote:
I have a question about conditional compilation. In C++, I would
sometimes define a constant in an include file, and then have blocks of
code in different source files that were conditionally compiled based
on that constant. Now that C# has done away with include files, is
there any way of doing the same thing, short of defining the constant
multiple times at the head of each source file that has the
conditionally compiled code? There must be a better way...


Jun 8 '06 #7
Thanks, all.

Yes, I already know all this, but it still seems like C# is lacking in
this regard relative to C++. In my case, I am referencing several
dll's from within my solution, and I want the conditional compilation
constant to be defined or not for all the code in the solution,
including the dll's. There isn't a way to have a constant defined for
the entire "solution", is there? In C++, this is easily done by having
a #define in an include file that all the dll and library modules in
the project include. This is done all the time...

- Dave

Jun 8 '06 #8

Stoitcho Goutsev (100) wrote:
Dave,
In my case, I am referencing several
dll's from within my solution, and I want the conditional compilation
constant to be defined or not for all the code in the solution,
including the dll's.


This is not even possible in C++. These are pre-processor directives as such
they are taken into consideration before compilaton. If you reference a dll
this dll is already compiled, so all directives has been already processed
the sam if you implicitly link to a dll in C++. There is no #define in
already compiled code.
--

Stoitcho Goutsev (100)


Sure it's possible. In C++ developer studio you can have a
"workspace", analogous to the "solution" in Visual Studio .NET. The
workspace can include multiple dll's and executables. If the source
code in any dll or exe includes an include file that you change the
value of a constant in, the dll or exe will be rebuilt. People do this
~all~ the time, in any large project.

I think the solution is to do what James suggested. I think this is a
bit more of a hassle than what you do in C++, but it works.

Thanks everyone!

- Dave

Jun 8 '06 #9
I think you are mixing referencing dlls vs referencing projects. DLLs are
already compiled assemblies and cannot be modified.

Anyways I'm glad you find a solution to your problem.
--

Stoitcho Goutsev (100)

"Dave" <dp*@eliassen.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...

Stoitcho Goutsev (100) wrote:
Dave,
> In my case, I am referencing several
> dll's from within my solution, and I want the conditional compilation
> constant to be defined or not for all the code in the solution,
> including the dll's.


This is not even possible in C++. These are pre-processor directives as
such
they are taken into consideration before compilaton. If you reference a
dll
this dll is already compiled, so all directives has been already
processed
the sam if you implicitly link to a dll in C++. There is no #define in
already compiled code.
--

Stoitcho Goutsev (100)


Sure it's possible. In C++ developer studio you can have a
"workspace", analogous to the "solution" in Visual Studio .NET. The
workspace can include multiple dll's and executables. If the source
code in any dll or exe includes an include file that you change the
value of a constant in, the dll or exe will be rebuilt. People do this
~all~ the time, in any large project.

I think the solution is to do what James suggested. I think this is a
bit more of a hassle than what you do in C++, but it works.

Thanks everyone!

- Dave

Jun 8 '06 #10
I'm not confused, but may be confusing. I'm talking about having an
exe with a project - call it "Executable.exe". That exe uses code from
a dll, call it "DLL.dll". You have a solution that contains the
projects for both Executable, and DLL. I wanted a way to have a
conditional compilation directive I could change from within that
solution, that would rebuild any code in the Executable or DLL, that
referenced the conditional compilation directive.

Stoitcho Goutsev (100) wrote:
I think you are mixing referencing dlls vs referencing projects. DLLs are
already compiled assemblies and cannot be modified.

Anyways I'm glad you find a solution to your problem.
--

Stoitcho Goutsev (100)

"Dave" <dp*@eliassen.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...

Stoitcho Goutsev (100) wrote:
Dave,

> In my case, I am referencing several
> dll's from within my solution, and I want the conditional compilation
> constant to be defined or not for all the code in the solution,
> including the dll's.

This is not even possible in C++. These are pre-processor directives as
such
they are taken into consideration before compilaton. If you reference a
dll
this dll is already compiled, so all directives has been already
processed
the sam if you implicitly link to a dll in C++. There is no #define in
already compiled code.
--

Stoitcho Goutsev (100)


Sure it's possible. In C++ developer studio you can have a
"workspace", analogous to the "solution" in Visual Studio .NET. The
workspace can include multiple dll's and executables. If the source
code in any dll or exe includes an include file that you change the
value of a constant in, the dll or exe will be rebuilt. People do this
~all~ the time, in any large project.

I think the solution is to do what James suggested. I think this is a
bit more of a hassle than what you do in C++, but it works.

Thanks everyone!

- Dave


Jun 8 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Steven T. Hatton | last post by:
I've made no secret of the fact that I really dislike the C preprocessor in C++. No aspect of the language has caused me more trouble. No aspect of the language has cause more code I've read to be...
2
by: Steve Jorgensen | last post by:
To begin with an example... Let's say you were wanting to write code usign early binding to the MSXML library, but then be able to switch between early and late binding at will. Conditional...
1
by: chris han | last post by:
Hi, all, I'm trying to use Conditional Compilation Statements in my code. using System; #define DEBUG public class MyClass { public static void Main() {
12
by: wanghz | last post by:
Hi, Could I ask some questions about the conditional compilaion? Suppose I have three simple files: a.c, b.c and c.h /* --------a.c--------- */ #include <stdio.h> #include "c.h" int...
2
by: FireStarter | last post by:
Guys, in the code that follows, why does the method F() still compile, even if DBG is undefined? Inside method G(), the code inside <#if DBG> does not compile (notice that I can write whatever I...
1
by: A.M-SG | last post by:
Hi, We have a solution with several c# projects within it. How can I define solution wide conditional compilation symbols?
4
by: Bob | last post by:
Hi, In VS2003 conditional compilation constants and their state could be defined at project level. I was using this to control what features where offered by various builds. i.e....
1
by: Marek | last post by:
I use VS2005 with framework 2.0 and I just found a behavior I consider odd. Here is the code that illustrates th eproblem: public static void MethodA() { MethodB() } #if DEBUG
6
by: maxwell | last post by:
I'm trying to use the gpp utility (Gnu points to http://en.nothingisreal.com/wiki/GPP) to do conditional compilation in Python, and I'm running into a problem: the same '#' character introduces...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.