1

My project uses a Settings.h to store common configuration settings like the server url etc. I have been tasked to create a new app from this code where only few parameters such as the server url , icon etc are different.

I have a new target in my xcode workspace for this. I have 2 Settings.h in separate folders like

awesomeProject/Settings/Settings.h
oldProject/Settings/Settings.h

The Settings.h is included in a few places in the project (not loaded via .pch). I have tried setting header search path for both the targets and this didn't work (as in the compiling awesomeProject target included the oldProject's Settings.h).

Is there a way to #include Settings.h based on the target without resorting to sprinkling #ifdef .. #endif constructs ?

Sandeep Chayapathi
  • 1,490
  • 2
  • 13
  • 31

2 Answers2

1

If I understand right and you have separate targets to that need to include different header files so you should be able to set the 'Header Search Paths' differently and get the correct file. You said you tried this but you might want to try doing a clean and full rebuild.

Alicia Cano
  • 200
  • 6
  • I tried cleaning the project before building and it didn't work. I then tried removing "Derived Data" from the organizer and this sort of worked. It looks like xcode will load the first header file it encounters while compiling. For instance http://take.ms/96m8N , if I re-arrange the newProject over the oldProject and no matter what target I build (where they all have custom header search path) the first set of header files are loaded. Not sure what to make of this. – Sandeep Chayapathi Apr 14 '14 at 17:55
  • the "Header Search Path" is the right way to go. In addition to this I needed to set a "User Defined Variable" called "USE_HEADERMAP" with value "NO" as detailed here http://stackoverflow.com/questions/2596695/controlling-which-project-header-file-xcode-will-include Note: the side effect of this is you are forced to add to "Header Search Paths" all the folders with relevant header files. – Sandeep Chayapathi Apr 14 '14 at 20:27
0

I would try to leverage preprocessor to accomplish your goal. One option is to define in build settings for each target macro SETTINGS_H, which will be "awesomeProject/Settings/Settings.h" or "oldProject/Settings/Settings.h" depending on the target. Then in source code you can #include SETTINGS_H. Of course, you might need to tweak header search path to be able to find each header.

Honestly speaking, #include SETTINGS_H is a tad ugly for my taste, so I prefer another approach. You can keep single "Settings.h" file, but use macros for constant values. For example, in "Settings.m"

NSString *const kServerURL = SERVER_URL;

and in target build settings add to GCC_PREPROCESSOR_DEFINITIONS

SERVER_URL='@"http://stackoverflow.com"'

You might need to play with quotation marks to achieve the desired effect. To organize project better, you can move macros' definitions to .xcconfig file and keep separate .xcconfig files for each target.

Volodymyr Sapsai
  • 413
  • 5
  • 13