条件编译问题 [英] Conditional compilation question

查看:77
本文介绍了条件编译问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是多年的C ++程序员,试图用C#弄湿我的脚。


我有一个关于条件编译的问题。在C ++中,我会

有时在包含文件中定义一个常量,然后在不同的源文件中有块的

代码,这些源代码是基于条件编译的。

在那个常数上。既然C#已经废除了包含文件,那么

有什么方法可以做同样的事情,而不是在每个源文件的头部多次定义常量

有条件编译的

代码?必须有更好的方式...


谢谢。


- 戴夫

解决方案

戴夫,


有条件编译器声明,它的完成方式相同

你会这样做在C ++中,通过#if #endif语句。


在VS2005中,打开项目属性并转到Build选项卡;有一个

文本框来定义是否存在常量。


但要小心,测试使用的装配更难

条件汇编(我很确定你知道)。


Andy


Dave写道:

我是多年的C ++程序员,试图在C#中沾沾自喜。

我有一个关于条件编译的问题。在C ++中,我有时会在包含文件中定义一个常量,然后在不同的源文件中包含
代码块,这些源代码文件是基于该常量有条件地编译的。现在C#已经废除了包含文件,是否有任何方法可以做同样的事情,而不是在每个有条件的源文件的头部多次定义常量
编译代码?必须有更好的方式...

谢谢。

- 戴夫




< blockquote> Dave,


C#支持条件编译,但更简单的是

C ++

1.你注意到已经没有头文件了,因此你需要在每个文件中定义预处理器符号,或者你可以在项目中定义

。项目设置。


2.你只能定义一个符号(给它一个名字)不像C ++你不能

给它一个值然后comapare它价值。


例如你可以


#define VS_2005

....


#if VS_2005

....

#else

....

#endif


您只能检查是否声明了某个名称。预处理器声明

版本号并使用< ,>或=不支持。

-

HTH

Stoitcho Goutsev(100)

Dave < dp*@eliassen.com>在消息中写道

新闻:11 ********************** @ i39g2000cwa.googlegr oups.com ...

我是多年的C ++程序员,试图在C#中沾沾自喜。

我有一个关于条件编译的问题。在C ++中,我有时会在包含文件中定义一个常量,然后在不同的源文件中包含
代码块,这些源代码文件是基于该常量有条件地编译的。现在C#已经废除了包含文件,是否有任何方法可以做同样的事情,而不是在每个有条件的源文件的头部多次定义常量
编译代码?必须有更好的方法...

谢谢。

- 戴夫



谢谢,所有。


是的,我已经知道了这一切,但看起来C#在这方面仍然缺乏相对于C ++的价值。在我的情况下,我在我的解决方案中引用了几个

dll',我希望条件编译

常量定义或不定义所有代码解决方案,

包括dll''。没有一种方法可以为整个解决方案定义一个常量来定义,是吗?在C ++中,这可以通过在包含文件中使用
#define轻松完成,该文件包含项目中包含的所有dll和库模块。这是一直做的......


- 戴夫


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

解决方案

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




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



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


这篇关于条件编译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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