如何限制“某些”代码被编译/构建? (没有#define) [英] How to restrict 'some' code from getting compiled/built? (without #define)

查看:84
本文介绍了如何限制“某些”代码被编译/构建? (没有#define)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要从我的代码中构建两个可执行文件,一个包含所有代码(和

因此应用程序功能),而另一些则没有。如何最好地管理

不应该进入其中一个可执行文件的代码?


我的第一个想法是使用带#define的条件编译和#ifdef

等但它变得凌乱,是否有更好的方法来管理它?


详细问题:


#。有一个A类虚拟foo1和foo2

#。有很多派生类的A实现了foo1和foo2

#。 Executable1需要同时使用foo1和foo2

#。 Executable2只需要使用foo1,永远不需要foo2


如何确保所有foo2代码永远不会进入executable2?很多

#ifdefs可用于此但是有更好的解决方案吗?也许一些

聪明地使用模板?还有其他不太复杂的东西吗?


谢谢

解决方案

Sachin Garg写道:


详细问题:


#。有一个A类虚拟foo1和foo2

#。有很多派生类的A实现了foo1和foo2

#。 Executable1需要同时使用foo1和foo2

#。 Executable2只需要使用foo1,永远不需要foo2


如何确保所有foo2代码永远不会进入executable2?很多

#ifdefs可用于此但是有更好的解决方案吗?



是:组织你的代码。


将源自foo2的所有代码放入单独的翻译单元。不要
在你的第二个可执行文件中包含那些翻译单元。

----- BEGIN PGP SIGNATURE -----

版本: GnuPG v1.4.9(GNU / Linux)

iEYEABECAAYFAkhJtn4ACgkQx9p3GYHlUOLH0QCfTsTTQQbUQx / Xn4UYTvOAD5gd

RyMAmgIKTAyCS6RaRnSt2z4hqnybQwiI

= SJfa

-----结束PGP SIGNATURE -----


6月7日凌晨1点37分,Sachin Garg < saching ... @ c10n.infowrote:


我需要从我的代码构建两个可执行文件,一个包含所有代码(和

因此应用程序功能)和其他没有这一切。如何最好地管理

不应该进入其中一个可执行文件的代码?


我的第一个想法是使用带#define的条件编译和#ifdef

等但它变得凌乱,是否有更好的方法来管理它?


详细问题:


#。有一个A类虚拟foo1和foo2

#。有很多派生类的A实现了foo1和foo2

#。 Executable1需要同时使用foo1和foo2

#。 Executable2只需要使用foo1,永远不需要foo2


如何确保所有foo2代码永远不会进入executable2?很多

#ifdefs可用于此但是有更好的解决方案吗?也许一些

聪明地使用模板?还有什么不那么复杂的吗?


谢谢



嗯,不知道这是不是你想要的,但是这里是怎么做的:


模板< bool使用>

class foo2

{

//在不需要的情况下实施

}


模板<>

class foo2< true>

{

//如果需要的话实施

}


模板< bool使用>

类foo2derived:public foo2<使用>

{

//实施以防万一它是不需要的

}


模板<>

类foo2derived:public foo2< true>

{

//如果需要的话实施

}


用法:


const bool UseFoo2 = ...;


foo2derived< UseFoo2f2d; //将编译所需的版本

foo2derived


2008-06-06 23:37,Sachin Garg写道:


我需要从我的代码构建两个可执行文件,一个包含所有代码(和

因此应用程序功能),而另一些则没有。如何最好地管理

不应该进入其中一个可执行文件的代码?


我的第一个想法是使用带#define的条件编译和#ifdef

等但它变得凌乱,是否有更好的方法来管理它?


详细问题:


#。有一个A类虚拟foo1和foo2

#。有很多派生类的A实现了foo1和foo2

#。 Executable1需要同时使用foo1和foo2

#。 Executable2只需要使用foo1,永远不需要foo2


如何确保所有foo2代码永远不会进入executable2?很多

#ifdefs可用于此但是有更好的解决方案吗?也许一些

聪明地使用模板?还有其他不太复杂的东西?



而不是阻止代码同时存在于两个可执行文件中它可能更容易阻止foo2被调用,可能是通过使用

#ifdefs选择程序接受哪种输入。


-

Erik Wikstr ?? m



I need to build two executables from my code, one having all the code (and
thus application features) and other not having it all. How to best manage
the code that shouldn''t go in one of the executables?

My first thought is to use conditional compilation with #define and #ifdef
etc but its getting messy, are there better ways to manage this?

The problem in detail:

#. There is a class A with virtual foo1 and foo2
#. There are ''lots'' of derived classes of A which implement foo1 and foo2
#. Executable1 needs to use both foo1 and foo2
#. Executable2 only needs to use foo1, never needs foo2

How to make sure that all the foo2 code never gets into executable2? Lots of
#ifdefs can be used for this but is there a better solution? Maybe some
clever use of templates? Something else less complicated?

Thanks

解决方案

Sachin Garg writes:

The problem in detail:

#. There is a class A with virtual foo1 and foo2
#. There are ''lots'' of derived classes of A which implement foo1 and foo2
#. Executable1 needs to use both foo1 and foo2
#. Executable2 only needs to use foo1, never needs foo2

How to make sure that all the foo2 code never gets into executable2? Lots of
#ifdefs can be used for this but is there a better solution?

Yes: organizing your code.

Put all code derived from foo2 into separate translation units. Do not
include those translation units in your second executable.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkhJtn4ACgkQx9p3GYHlUOLH0QCfTsTTQQbUQx/Xn4UYTvOAD5gd
RyMAmgIKTAyCS6RaRnSt2z4hqnybQwiI
=SJfa
-----END PGP SIGNATURE-----


On Jun 7, 1:37 am, "Sachin Garg" <saching...@c10n.infowrote:

I need to build two executables from my code, one having all the code (and
thus application features) and other not having it all. How to best manage
the code that shouldn''t go in one of the executables?

My first thought is to use conditional compilation with #define and #ifdef
etc but its getting messy, are there better ways to manage this?

The problem in detail:

#. There is a class A with virtual foo1 and foo2
#. There are ''lots'' of derived classes of A which implement foo1 and foo2
#. Executable1 needs to use both foo1 and foo2
#. Executable2 only needs to use foo1, never needs foo2

How to make sure that all the foo2 code never gets into executable2? Lots of
#ifdefs can be used for this but is there a better solution? Maybe some
clever use of templates? Something else less complicated?

Thanks

Hm, don''t know if that''s what you want, but here''s how it can be done:

template <bool Use>
class foo2
{
//implementation in case of it''s not needed
}

template <>
class foo2<true>
{
//implementation in case of it''s needed
}

template <bool Use>
class foo2derived : public foo2<Use>
{
//implementation in case of it''s not needed
}

template <>
class foo2derived : public foo2<true>
{
//implementation in case of it''s needed
}

Usage:

const bool UseFoo2 = ...;

foo2derived<UseFoo2f2d; //will compile the needed version of
foo2derived


On 2008-06-06 23:37, Sachin Garg wrote:

I need to build two executables from my code, one having all the code (and
thus application features) and other not having it all. How to best manage
the code that shouldn''t go in one of the executables?

My first thought is to use conditional compilation with #define and #ifdef
etc but its getting messy, are there better ways to manage this?

The problem in detail:

#. There is a class A with virtual foo1 and foo2
#. There are ''lots'' of derived classes of A which implement foo1 and foo2
#. Executable1 needs to use both foo1 and foo2
#. Executable2 only needs to use foo1, never needs foo2

How to make sure that all the foo2 code never gets into executable2? Lots of
#ifdefs can be used for this but is there a better solution? Maybe some
clever use of templates? Something else less complicated?

Instead of preventing the code from being in both executables it might
be easier to just prevent foo2 from being called, perhaps by using
#ifdefs to select which kinds of input the program accepts.

--
Erik Wikstr??m


这篇关于如何限制“某些”代码被编译/构建? (没有#define)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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