views:

57

answers:

1

I am new to Objective C and have the following error when trying to import a class defined by me

Building target “MusicCube” of project “MusicCube” with configuration “Debug” — (1 error)
     cd /Users/varsha_vijay/Downloads/MusicCube
    setenv MACOSX_DEPLOYMENT_TARGET 10.5
    setenv PATH "/Xcode3.1.4/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Xcode3.1.4/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Xcode3.1.4/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -arch i386 -isysroot /Xcode3.1.4/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk -L/Users/varsha_vijay/Downloads/MusicCube/build/Debug-iphonesimulator -F/Users/varsha_vijay/Downloads/MusicCube/build/Debug-iphonesimulator -filelist /Users/varsha_vijay/Downloads/MusicCube/build/MusicCube.build/Debug-iphonesimulator/MusicCube.build/Objects-normal/i386/MusicCube.LinkFileList -mmacosx-version-min=10.5 -framework Foundation -framework UIKit -framework OpenGLES -framework QuartzCore -framework AudioToolbox -framework CoreGraphics -framework OpenAL -o /Users/varsha_vijay/Downloads/MusicCube/build/Debug-iphonesimulator/MusicCube.app/MusicCube
ld: duplicate symbol _num_flight_vertices in /Users/varsha_vijay/Downloads/MusicCube/build/MusicCube.build/Debug-iphonesimulator/MusicCube.build/Objects-normal/i386/Flight.o and /Users/varsha_vijay/Downloads/MusicCube/build/MusicCube.build/Debug-iphonesimulator/MusicCube.build/Objects-normal/i386/EAGLView.o
collect2: ld returned 1 exit status
  ld: duplicate symbol _num_flight_vertices in /Users/varsha_vijay/Downloads/MusicCube/build/MusicCube.build/Debug-iphonesimulator/MusicCube.build/Objects-normal/i386/Flight.o and /Users/varsha_vijay/Downloads/MusicCube/build/MusicCube.build/Debug-iphonesimulator/MusicCube.build/Objects-normal/i386/EAGLView.o
  collect2: ld returned 1 exit status
Build failed (1 error)

Here is the way my program is organized.

1. I defined a c header file vertices.h

#ifndef VERTICES_H
#define VERTICES_H
const int num_flight_vertices = 8;
#endif

2. I am importing this header file in my Flight.h class

#import "vertices.h"

@interface Flight : NSObject {

3. I am importing this Flight.h file in my EAGLView.h class

#import "vertices.h"
@interface EAGLView : UIView {
@private
Flight *flight;

when I build this, I get the error.

Can someone help me with this.

Thanks, VJ

+2  A: 

Are you importing vertices.h anywhere else in your project?

Generally, you declare constants in a header file, and define them in the implementation file, which avoids this problem. So you could do this:

// vertices.h
#ifndef VERTICES_H
#define VERTICES_H
extern const int num_flight_vertices;
#endif

// vertices.c
#import vertices.h
const int num_flight_vertices = 8;
mipadi
Since you are declaring it extern, there's no need for the #ifndef protection. "extern" means "I will give it a value somewhere else." You can do that as much as you like. In ObjC, you generally don't need #ifndef protection for much because of the use of #import anyway.
Rob Napier