2

I was reading an old post on here: Constants in Objective-C which talks about how to set up a file for your constants when doing iPhone dev in objective C. One of the responses (which got a lot of helpful votes) says that "Constants.m should be added to your application/framework's target so that it is linked in to the final product."

My question is, how do you do this, and what is accomplished by this? Does this make it so you don't have to import the constants file in the files you need to use it for?

Also, does the same method talked about in the response apply to integer constants, etc?

Thanks.

Community
  • 1
  • 1
Ser Pounce
  • 14,196
  • 18
  • 84
  • 169

2 Answers2

3

It makes it so that the constant values are available in the linked binary. Your header file will be included by reference (since you #import it) but the compiler needs to know that you want that .m file to be part of the final binary.

In XCode 4, choose your project from the top of the Project Navigator (the file tree view tab on the left). Select your target from the list of targets that appears in a new column to the right of the Project Navigator. Select the Build Phases tab on the right. Open up the Compile Sources item. Here are all of the files that are being compiled to build your binary. Make sure your constants file is in here. If it is not, find it in the Project Navigator and drag it into the Compile Sources pane.

Seamus Campbell
  • 17,816
  • 3
  • 52
  • 60
1

In Xcode 4, beside the method Seamus mentioned, you can also select that .h file, open right view (at the top-right conner, there're three), in the first tab there's target membership, select the target you want to add it to and you're set.

You have to make sure you #import and also the file has to be in build target. #import make sure your complier know where to look for, include the file in target make sure when complier go and look, there's that file (The file won't be include in your build if it's not part your building target even it's in your project).

Derek Li
  • 3,089
  • 2
  • 25
  • 38