473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to restrict 'some' code from getting compiled/built? (without #define)


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
Jun 27 '08 #1
11 1404
Sam
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-----

Jun 27 '08 #2
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
Jun 27 '08 #3
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
Jun 27 '08 #4

<sh*******@mail.ruwrote in message
news:8f**********************************@s50g2000 hsb.googlegroups.com...
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
Thanks, this helps.
Jun 27 '08 #5

"Erik Wikström" <Er***********@telia.comwrote in message
news:YW*****************@newsb.telia.net...
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.
I have done this, wanted to also prevent unused code from getting in the
exe.
Jun 27 '08 #6
On Jun 6, 11:37 pm, "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?
Generally, if a class has two different interfaces, it's two
different classes. You'd have to be more concrete; the usual
solution for conditionally including functions and/or objects is
to put the conditional objects in separate source files, and
either link the corresponding object files in or not.

In your case, it looks to me like you have two separate
hierachies: one with just foo1, and one with both. This could
be done using multiple inheritance, something like:

class Base
{
public:
virtual void foo1() = 0 ;
} ;

class Derived : public virtual Base
{
public:
virtual void foo1() ;
} ;

and:

class BaseOptional : public virual Base
{
public:
virtual void foo2() = 0 ;
} ;

class DerivedOptional : public virtual Derived
{
public:
virtual void foo2() ;
} ;

Put the ...Optional in a separate directory and library.

Personally, however, I'd reconsider the design before I adopted
such complexity. Couldn't two separate hierarchies be made to
work as well, if not better?

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #7

"Sam" <sa*@email-scan.comwrote in message
news:co*****************************@commodore.ema il-scan.com...
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.
Hmmm, worth a try.

ps. There is maybe something wrong with your news reader, your message came
in a 'text attachment', while the actual post was empty (atleast thats how
it came in outlook express). You might want to check this out.
Jun 27 '08 #8
Sam
Sachin Garg writes:
>
Hmmm, worth a try.

ps. There is maybe something wrong with your news reader, your message came
in a 'text attachment', while the actual post was empty (atleast thats how
it came in outlook express). You might want to check this out.
There's nothing wrong with my newsreader. This is a well known bug in your
Outlook Express, which still fails to implement a thirteen-year old standard
for digitally-signed messages.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkhKphEACgkQx9p3GYHlUOIEbwCfcWz1HK7zHk LcXyY5MEm3UP7w
r0wAn2B628rczMKT5MBvY51IhE3UFGBL
=MTHu
-----END PGP SIGNATURE-----

Jun 27 '08 #9

"Sam" <sa*@email-scan.comwrote in message
news:co******************************@commodore.em ail-scan.com...
Sachin Garg writes:

Hmmm, worth a try.

ps. There is maybe something wrong with your news reader, your message
came
in a 'text attachment', while the actual post was empty (atleast thats
how
it came in outlook express). You might want to check this out.

There's nothing wrong with my newsreader. This is a well known bug in your
Outlook Express, which still fails to implement a thirteen-year old
standard
for digitally-signed messages.
ok
Jun 27 '08 #10

"James Kanze" <ja*********@gmail.comwrote in message
news:d1**********************************@j22g2000 hsf.googlegroups.com...
On Jun 6, 11:37 pm, "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?

Generally, if a class has two different interfaces, it's two
different classes. You'd have to be more concrete; the usual
solution for conditionally including functions and/or objects is
to put the conditional objects in separate source files, and
either link the corresponding object files in or not.

In your case, it looks to me like you have two separate
hierachies: one with just foo1, and one with both. This could
be done using multiple inheritance, something like:

[code snipped]

Put the ...Optional in a separate directory and library.

Personally, however, I'd reconsider the design before I adopted
such complexity. Couldn't two separate hierarchies be made to
work as well, if not better?
Duh, I was

Back to the drawing board :-)
Jun 27 '08 #11

"Sachin Garg" <sa********@c10n.infowrote in message
news:g2**********@aioe.org...
>
"James Kanze" <ja*********@gmail.comwrote in message
news:d1**********************************@j22g2000 hsf.googlegroups.com...
>On Jun 6, 11:37 pm, "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?

Generally, if a class has two different interfaces, it's two
different classes. You'd have to be more concrete; the usual
solution for conditionally including functions and/or objects is
to put the conditional objects in separate source files, and
either link the corresponding object files in or not.

In your case, it looks to me like you have two separate
hierachies: one with just foo1, and one with both. This could
be done using multiple inheritance, something like:

[code snipped]

Put the ...Optional in a separate directory and library.

Personally, however, I'd reconsider the design before I adopted
such complexity. Couldn't two separate hierarchies be made to
work as well, if not better?

Duh, I was

Back to the drawing board :-)
Hit 'send' a bit too soon :-p

I was probably trying to use this conditional compilation on top of existing
design to patch up for this new requirement. You are right that a fresh
rethink on the design can give more elegant solutions.

Back to the drawing board :-)

Thanks.
Jun 27 '08 #12

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

Similar topics

3
by: Paul | last post by:
Hi all, at present I I've built a website which can be updated by admin and users. My problem, I've combined "log in" and "access levels" to restrict access to certain pages, using the built...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
7
by: tweak | last post by:
Can someone give me a short example as how to best use this keyword in your code? This is my understanding: by definition restrict sounds like it is suppose to restrict access to memory...
2
by: pemo | last post by:
In Harbison and Steele's book, they say that using 'restrict' allows functions like memcpy() to be prototyped like this: void * memcpy(void * restrict s1, const void * restrict s2, size_t n); ...
11
by: pemo | last post by:
If you were to compile/run the code below, and get the result '30', I'd be very interested to know what compiler you're using - and its optimisation settings #include <stdio.h> int test(int...
12
by: Me | last post by:
I'm trying to wrap my head around the wording but from what I think the standard says: 1. it's impossible to swap a restrict pointer with another pointer, i.e. int a = 1, b = 2; int *...
21
by: Niu Xiao | last post by:
I see a lot of use in function declarations, such as size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE* restrict fp); but what does the keyword 'restrict' mean? there is no...
2
by: venkat | last post by:
Hi, i came across restrict qualifier while looking the code. I haven't able to understand what does this do?. Can some one help me how does this makes the things restrict to an specified...
23
by: raashid bhatt | last post by:
what is restrict keyword used for? eg int *restrict p;
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.