Project / Build identification in source code

Discussion in 'iOS Programming' started by sujithkrishnan, Feb 4, 2009.

  1. sujithkrishnan macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #1
    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...
     
  2. kalimba macrumors regular

    Joined:
    Jun 10, 2008
    #2
    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).
     
  3. sujithkrishnan thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #3
    @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)
     

Share This Page