I am getting this error when i try to compile Object-c program in cygwin Windows 7, but this program executed in Xcode.
main.m:5:3: error: ‘NSString’ undeclared (first use in this function)
#include <stdio.h>
int main (int argc, const char * argv[])
{
NSString *str1 = #"1st string";
NSString *str2 = #"2nd string";
NSLog(#"Hello, World!");
return 0;
}
Executed using the following CMD in cygwin,
gcc -c -Wno-import main.m
can you one help me how solve this compilation error.
Windows doesn't come with a Foundation library, so NSString isn't available by default. You should try GNUstep or you could cross-compile from Xcode on a Mac using Cocotron. Whichever you choose, look at its documentation to find out how to use it (at a minimum you'll need to #import <Foundation/Foundation.h> and link the Foundation library).
Make sure that you are have the following code at the top of your .h or .m file:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
You can also put the code in your Prefix.pch file.
Related
I have the following code:
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import </usr/local/Cellar/glew/1.12.0/include/GL/glew.h>
#import </usr/local/Cellar/glfw2/2.7.9/include/GL/glfw.h>
int main(int argc, const char * argv[])
{
#autoreleasepool
{
if(!glfwInit())
{
exit(EXIT_FAILURE);
}
}
return 0;
}
I get the following errors when compiling: glew.h User-defined Issue Gltypes.h included before glew.h Modules Issue Declaration of PFNGLCOPYTEXSUBIMAGE3DPROC must be imported from module 'OpenGL.GL3' before it is required
There are another 19 errors that are all semantic errors. Does anyone know how to fix this?
As it turned out I didn't properly compile the library files.
I read through the advice here Constants in Objective-C, but I find two errors in the accepted answer:
I keep getting linker errors when I implement the approach of using .m and .h
In my constants.m file, I get the error "unknown type" for NSString
I am also getting an error following the advice from #VictorHanHee
as a further suggestion of how to link the constants to a .pch file:
I don't have a .pch file by default, and when I create a new one it
doesn't have the formatting expected from the answer.
All I have done is create constants.h and constants.m and put the constant declarations in them as provided in the accepted answer. I also created a .pch file, but as I said it doesn't at all match VictorHanHee's description.
Is this older posting outdated, or if not what am I missing? I don't really know how to go forward with a linker error. I simply want to have a file constants.h where I declare all constants and can include this file in as many classes as I want to make life easier. Can someone tell me how to do this nowadays or point to a more recent discussion?
Also here is the linker error (or part of log I can copy and paste anyway, not full message)
-[AccountInfoViewController viewDidLoad] in AccountInfoViewController.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
As you can see, I am trying to access the constant from viewDidLoad. I am simply trying to print it out with NSLog.
Here are the .h and .m files I was originally using:
.h
extern NSString * const PREFS_MY_CONSTANT;
.m
NSString * const PREFS_MY_CONSTANT = #"prefs_my_constant";
Ok here is the template that actually seems to work:
.h
#ifndef Project_prefs_h
#define Project_prefs_h
#endif
extern NSString * const PREFS_MY_CONSTANT;
.m
#import <Foundation/Foundation.h>
NSString * const PREFS_MY_CONSTANT = #"prefs_my_constant";
So the older post is perhaps misleading or I misunderstood them when they said remove everything apart from the constants - but if you don't actually want to remove everything, shouldn't you say so? That's a fair amount of text in there not to mention...
In recent version of Xcode there is no ProjectName-Prefix.pch created automatically file so you should import the file where it is required then try using the constant.
Easiest way:
// Prefs.h
#import <Foundation/Foundation.h>
#define PREFS_MY_CONSTANT #"prefs_my_constant"
Better way:
// Prefs.h
extern NSString * const PREFS_MY_CONSTANT;
// Prefs.m
NSString * const PREFS_MY_CONSTANT = #"prefs_my_constant";
There are a few different ways to create constants in Objective-C, but the easiest way is to use #define statements.
To start, create a new header file. This is where we’ll store all the constants. Name it something original like “Constants.h”.
Here’s a sample file with a few constants:
//
// Constants.h
// App Name
#import <Foundation/Foundation.h>
#define NUM_SECTIONS 7
#define NUM_SECTION_1_ITEMS 2
#define NUM_SECTION_2_ITEMS 14
#define NUM_SECTION_3_ITEMS 5
#define APP_TITLE #"App Name"
#define APP_AUTHOR #"Miscellanea"
To use these constants in your project, you need to import your header file in each implementation file (*.m) where you’ll be referencing them.
#import "Constants.h"
– (void)viewDidLoad
{
self.title = APP_TITLE;
for (int i = 0; i < NUM_SECTIONS; ++ i)
{
// etc.
}
}
Here is the template that does seem to work. It's not different from the old answer except that you don't delete everything apart from the constants.
.h
#ifndef Project_prefs_h
#define Project_prefs_h
#endif
extern NSString * const PREFS_MY_CONSTANT;
.m
#import <Foundation/Foundation.h>
NSString * const PREFS_MY_CONSTANT = #"prefs_my_constant";
Apart from this it appears no longer possible to use .pch file to automatically include the constants across all classes.
This is ridiculous, im trying to create a sound bool to turn of in app sounds. I keep getting
Undefined symbols for architecture i386:
"_kPlaySoundPrefsKey", referenced from:
-[AppDelegate application:didFinishLaunchingWithOptions:] in AppDelegate.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have checked that all my files are linked in build phases, I have deleted the appdelegate .m where im getting the error before I even get to call the bool in any of my view controllers, and re imported it in build phases. Checked I have relevant framweworks in place. I have even checked a previous app I made with same code and the code it appears to be exactly the same with no error (built with previous version of xcode). Taking it right back to basics I get the error as soon as I add the following code to my App Delegate,
.h
#import <UIKit/UIKit.h>
extern NSString *kPlaySoundPrefsKey;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
.m
#import "AppDelegate.h"
#import <AudioToolbox/AudioToolbox.h>
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
NSDictionary *defaultDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:kPlaySoundPrefsKey];
return YES;
}
If I change extern NSString *kPlaySoundPrefsKey; to NSString *kPlaySoundPrefsKey; it builds then crashes...Im out of ideas now
When you declaring something as extern you are telling the compiler the type AND that you will define it somewhere else. In your case you never define your variable.
So in your .h you would have:
extern NSString* kPlaySoundPrefsKey;
But in some .m you must also have
NSString* kPlaySoundPrefsKey = #"play_sounds"; // or some value
Since in your case these are constants you can also specify:
extern NSString* const kPlaySoundPrefsKey;
and
NSString* const kPlaySoundPrefsKey = #"play_sounds";
The addition of the const qualifier will cause a compiler error if someone ever writes something like
kPlaySoundPrefsKey = #"some_other_value";
I am also got the same error even if I am properly declared and define the extern const string ,
The problem is, my constant file not in the compile source list .
Make sure that your .m file appears in the "Compile Sources" Build Phase for your build target. Sometimes, adding files to a project in Xcode doesn't add all implementation files to the appropriate targets.
Hope this will helpful for some people.
Refe: linker-error-undefined-symbols-symbols-not-found
First, ensure it is defined:
// AppDelegate.h
extern NSString* const kPlaySoundPrefsKey; // << declaration
// AppDelegate.m
NSString * const kPlaySoundPrefsKey = #"kPlaySoundPrefsKey"; // << definition
see also:
"extern const" vs "extern" only
3 questions about extern used in an Objective-C project
Linker error using extern "C" in Objective-C code
Thanks for your help guys
I added
NSString *kPlaySoundPrefsKey = #"playSoundKey";
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultDict];
to app delegate. That fixed it
I'm trying to run through all the Objective C I possibly can as fast as I can for my new job. I've divided up my training so I do 1/2 with cocoa tutorials and I thought it also made sense to try to learn Objective C from a general perspective as well so the other 50% i'm trying to learn out of the "Programming In Objective C" book by Stephan Kochan (2004). Many of his examples up through the first 50 pages or so seem to imply that there is a way to make a simple class (interface, implementation, and main) all in the same file. I have been trying to compile this in the mac Snowleopard terminal for the sake of speed, but as soon as I call a method inside the main I get compile errors.
I know its not my gcc compiler because the file will compile, and I can even write a printf statement, but as soon as I try to call a method, I get compile errors. I would appreciate it if someone could demonstrate a simple inclusive objectivec.m file that actually works with a method call.
here's my code
#import <stdio.h>
#import <objc/Object.h>
#interface testing1 : Object
{
int number;
}
-(void) setNum:(int)a;
-(void) print;
#end
#implementation testing1;
-(void) setNum:(int) a{
number = a;
}
-(void) print{
printf("this is the number %i \n", number);
}
#end
int main(int argc, char* argv[] )
{
//testing1 *test =[testing1 new];
//[test setNum: (int)34];
printf("testing");
//[test print];
}
this will compile in the terminal with --- gcc tester1 -o myProg -l objc
I've tried to call those methods several different ways but it does not work
any help is appreciated. Perhaps I need to break it up and use make - I don't know
Thanks
MIke
You have ; at the end of #implementation -
#implementation testing1; // Semi colon shouldn't be present at the end
With that modification made, you should see the result. Online result of the program
[test setNum: (int)34];
Here typecasting 34 to int is unnecessary. You can just pass the message setNum to reference test with 34 followed by colon.
[test setNum:34 ];
Interface declarations should go in header while the implementation to source files. Only source files get compiled. Before even the compilation phase, pre-processor just copies the content of all imported files to the translation units. So,
testing.h
#import <Foundation/NSObject.h>
#interface testing1 : NSObject
{
int number;
}
-(void) setNum:(int)a;
-(void) print;
#end
testing.m
#import <stdio.h>
#import "testing.h"
// You should definitely import just stdio.h header here because of printf function usage
// testing.h also to be imported or else compiler doesn't know what is testing1.
#implementation testing1
-(void) setNum:(int) a{
number = a;
}
-(void) print{
printf("this is the number %i \n", number);
}
#end
main.m
#import <stdio.h>
#import "testing.h"
int main(int argc, char* argv[] )
{
testing1 *test =[testing1 new];
[test setNum: 34];
printf("testing");
[test print];
return 0;
}
Now you need to compile the two source files from which corresponding object files are generated. Linker combines these object files to give the final executable.
gcc -o test testing.m main.m -framework Foundation
Run -
./test
The book you're following is out of date. The Object class was effectively deprecated way back in 1994 with the introduction of OpenStep.
In Objective-C 2.0, the Object class simply isn't available at all. That's why your program produces compiler warnings/errors.
You should instead subclass NSObject and import <Foundation/Foundation.h>. Use the -framework Foundation gcc parameter, instead of -l objc.
First of all, you used #import <stdio.h>. You only need that when your using C only. Delete the sentence and use this instead.
#import <Foundation/Foundation.h>
Secondly, why are you using #import <objc/Object.h>? The "<" and ">" are only for frameworks. If you want to import another file you made, use #import "Object.h" instead. (Unless you're talking about NSObject, then you don't need to import anything because it is part of the Foundation framework.)
And lastly, you should take away the semicolon after the #implementation because it's unnecessary.
I am using gnustep for objective-c on windows. If i keep interface and implementation files of a class together with main file, it compiles without error and gives expected output.
Following is the example:
// File "classA.h"
#import <Foundation/Foundation.h>
#interface classA: NSObject
{
int a;
}
-(void) print;
#end
// File "classA.m"
#import "classA.h"
#implementation classA
-(void) print
{
a = 10;
NSLog(#"a = %i", a);
}
#end
// File "test.m"
#import "classA.h"
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(#"start");
classA *objA = [[classA alloc] init];
[objA print];
[objA release];
NSLog(#"done");
[pool drain];
return 0;
}
However, if i put the interface and implementation files separately, on compiling using following command
gcc `gnustep-config --objc-flags` -o program program.m -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base
i get following error
undefined reference to `__objc_class_name_myNewClass'
collect2: ld returned 1 exit status
How do i keep the files separate and still compile the program successfully
Thanks for help
Regards
You have a linker error here. Most likely, you are not including a required header file for myNewClass.
I know this is old, but double check the name of compiled files just in case the error is in a different file.
Reasons:
The class myNewClass is not referenced in the code above and there are no other imports other than Foundation.
The name of the file in the code comments and the file you appear to be compiling differ.
// File "test.m"
gcc `gnustep-config --objc-flags` -o program program.m
By the look of it, the given code should compile when split out.
Also, about the question "Also how do i included "Compiled files"?", assuming you mean a .o file, you just need to include the header file (.h) and make sure the compiler can find the matching .o file. Related: How do I link object files in C? Fails with "Undefined symbols for architecture x86_64"