Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
Hi,

I am sharing a source file across different project /build.

Say..
for Project1

MainClass.m
Code:
#import Class1.h
#import Class2.h
.........
........
.......
<using Class1 instance>
<using Class2 instance>  // DEAD CODE IN PROJECT 1

for Project2

MainClass.m

Code:
#import Class1.h
#import Class2.h
.........
........
.......
<using Class1 instance>  // DEAD CODE IN PROJECT 2
<using Class2 instance>


I dont want to import Class2 in Project1 and Class1 in Project2.
How to conditionally import files / compile block of statements

Time being am doing conditional execution of code block based on a key in Localizable.strings file as both project have different Localizable.strings file

But i want to prevent it from compiling as well.

Plz help...
 

kalimba

macrumors regular
Jun 10, 2008
102
0
Use Preprocessor Macros?

You could create different preprocessor definitions unique to each project and use #ifdef's around the code you want to include or exclude for certain projects. For example, create definitions for "PROJECT1" and "PROJECT2" (each gets defined only in its respective project, of course). Then, in your source files use this approach:

Code:
... <some common code>

#ifdef PROJECT1
// code builds only for project #1
...
#endif

#ifdef PROJECT2
// code builds only for project #2
...
#endif

... <more common code>

One gotcha to keep in mind: when adding the preprocessor definitions to your project, be sure to add them for all build configurations (Debug, Release, whatever other configs).
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
You could create different preprocessor definitions unique to each project and use #ifdef's around the code you want to include or exclude for certain projects. For example, create definitions for "PROJECT1" and "PROJECT2" (each gets defined only in its respective project, of course). Then, in your source files use this approach:

Code:
... <some common code>

#ifdef PROJECT1
// code builds only for project #1
...
#endif

#ifdef PROJECT2
// code builds only for project #2
...
#endif

... <more common code>

One gotcha to keep in mind: when adding the preprocessor definitions to your project, be sure to add them for all build configurations (Debug, Release, whatever other configs).

@kalimba

Hope you are talking about the Preprocessor definition that we can setup in target/project Info. It will be much helpful if you tell what we can add there.

1. Is it a key-value pair?
2. Is it same as #define ?

I resolved in following way

Time being i achieved this by adding a #define in .pch file for each project.
and do #import based on that definition.

Its working fine...

Thanks.
(i will be more thankful if u clarify my above mentioned doubts)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.