Why did the HelloWorld codes using cocoa's library fail to build? - objective-c

The codes are like this:
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
// insert code here...
NSLog(#"Hello, World!");
}
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:#"Hi there."];
[alert runModal];
return 0;
}
What I want is: when called from command line, this program pops up an alert box, when I close the alertbox. The program exits.
But when building, it complains like this:
Ld /Users/hanfei/Library/Developer/Xcode/DerivedData/KeyCatcher-hijnrqhwiafuxtdjmdubtsijyhwh/Build/Products/Debug/KeyCatcher normal x86_64
cd /Users/hanfei/Desktop/KeyCatcher
setenv MACOSX_DEPLOYMENT_TARGET 10.9
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -L/Users/hanfei/Library/Developer/Xcode/DerivedData/KeyCatcher-hijnrqhwiafuxtdjmdubtsijyhwh/Build/Products/Debug -F/Users/hanfei/Library/Developer/Xcode/DerivedData/KeyCatcher-hijnrqhwiafuxtdjmdubtsijyhwh/Build/Products/Debug -filelist /Users/hanfei/Library/Developer/Xcode/DerivedData/KeyCatcher-hijnrqhwiafuxtdjmdubtsijyhwh/Build/Intermediates/KeyCatcher.build/Debug/KeyCatcher.build/Objects-normal/x86_64/KeyCatcher.LinkFileList -mmacosx-version-min=10.9 -fobjc-arc -fobjc-link-runtime -framework Foundation -Xlinker -dependency_info -Xlinker /Users/hanfei/Library/Developer/Xcode/DerivedData/KeyCatcher-hijnrqhwiafuxtdjmdubtsijyhwh/Build/Intermediates/KeyCatcher.build/Debug/KeyCatcher.build/Objects-normal/x86_64/KeyCatcher_dependency_info.dat -o /Users/hanfei/Library/Developer/Xcode/DerivedData/KeyCatcher-hijnrqhwiafuxtdjmdubtsijyhwh/Build/Products/Debug/KeyCatcher
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_NSAlert", referenced from:
objc-class-ref in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
When I choose the template for new project, I select command line tool instead of CoCoa Application as I just need CoCoa to display an alertbox. Does anyone have ideas about this..

That's because NSAlert is not #imported.
AppKit.h is not included by default when you create a command line tool , as you can see in the link, AppKit imports NSAlert.h.
EDIT:
First, to compile you need to add the Cocoa framework in your project.
Second, all the code should be enclosed in the #autoreleasepool section
#autoreleasepool {
// insert code here...
NSLog(#"Hello, World!");
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:#"Hi there."];
[alert runModal];
}
Your code will compile and run, but I think you'll have some other runtime errors.

I got the same error in C++ project that was using a NSAlert, this worked for me:
1) Adding
#include <Cocoa/Cocoa.h>
2) Adding Cocoa Framework in project->Buld Phases->Link Binary With Libraries.
3) Setting "Type" of all my .cpp files to "Objective-C++ Source" in the most right column in xCode.

you need to add this before main function
#import <AppKit/AppKit.h>

You need to import AppKit as mentioned by others and – as important as the framework – you need a run loop. So insert before the runModal line
[NSApplication sharedApplication];
to launch the "application" and the run loop

Related

Undefined symbols for architecture x86_64: after adding an extension target to a react-native project

I created a react-native project using react-native init
I then add an action extension to that project using File -> new -> target -> Action Extension, with the name "Stash"
I try adding a StashView that inherits from UIView, and contains an RCTRootView, as instructed in the Integration with Existing App instructions:
Stash/StashView.h:
#import <UIKit/UIKit.h>
#interface StashView : UIView
#end
Stash/StashView.m:
#import "StashView.h"
#import "RCTRootView.h"
#implementation StashView
- (void)awakeFromNib {
NSString *urlString = #"http://localhost:8081/stash.ios.bundle";
NSURL *jsCodeLocation = [NSURL URLWithString:urlString];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName: #"Stash"
launchOptions:nil];
[self addSubview:rootView];
rootView.frame = self.bounds;
}
#end
When I compile, I get the error:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_RCTRootView", referenced from:
objc-class-ref in StashView.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code
1 (use -v to see invocation)
The full stack trace:
Ld
/Users/danoved/Library/Developer/Xcode/DerivedData/ToDoBox-gmtanlmumdrkqactpypioaempcuc/Build/Products/Debug-iphonesimulator/Stash.appex/Stash
normal x86_64
cd /Users/danoved/Source/myprojects/ToDoBox
export IPHONEOS_DEPLOYMENT_TARGET=8.1
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.1.sdk
-L/Users/danoved/Library/Developer/Xcode/DerivedData/ToDoBox-gmtanlmumdrkqactpypioaempcuc/Build/Products/Debug-iphonesimulator
-F/Users/danoved/Library/Developer/Xcode/DerivedData/ToDoBox-gmtanlmumdrkqactpypioaempcuc/Build/Products/Debug-iphonesimulator
-filelist /Users/danoved/Library/Developer/Xcode/DerivedData/ToDoBox-gmtanlmumdrkqactpypioaempcuc/Build/Intermediates/ToDoBox.build/Debug-iphonesimulator/Stash.build/Objects-normal/x86_64/Stash.LinkFileList
-Xlinker -rpath -Xlinker #executable_path/Frameworks -Xlinker -rpath -Xlinker #executable_path/../../Frameworks -Xlinker -objc_abi_version -Xlinker 2 -OjbC /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.1.sdk/System/Library/PrivateFrameworks/PlugInKit.framework/PlugInKit -e _NSExtensionMain -fobjc-arc -fobjc-link-runtime -fapplication-extension -Xlinker -no_implicit_dylibs -mios-simulator-version-min=8.1 -Xlinker -dependency_info -Xlinker /Users/danoved/Library/Developer/Xcode/DerivedData/ToDoBox-gmtanlmumdrkqactpypioaempcuc/Build/Intermediates/ToDoBox.build/Debug-iphonesimulator/Stash.build/Objects-normal/x86_64/Stash_dependency_info.dat
-o /Users/danoved/Library/Developer/Xcode/DerivedData/ToDoBox-gmtanlmumdrkqactpypioaempcuc/Build/Products/Debug-iphonesimulator/Stash.appex/Stash
What am I doing wrong? Something in the build settings?
Figured it out!
Based on the answer here
I had to open the Link Binary with Libraries carrot in the Build Phases of the extension, and add all of the react libraries to it:

invoke framework in termimal the window seems to be blocked

now ,I have a framework named "MyFramework.framework" which include a window.nib, and I write a example to invoke it in terminal.
code:
//<MyFramework/myUI.h>
#ifndef Frmwork_myUI_h
#define Frmwork_myUI_h
#ifdef __cplusplus
extern "C"{
#endif
void ShowDialog();
#ifdef __cplusplus
}
#endif
#endif
//test.mm
#include <MyFramework/myUI.h>
#include <Foundation/NSRunLoop.h>
int main(int argc, char *argv[])
{
ShowDialog() ;
while (!isTransmitCompleted)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
return 0;
}
//build test.mm
g++ -c test.mm -o test.o
g++ test.o -o test -lpthread -framework Cocoa -framework AppKit -framework CoreData -framework Foundation -framework MyFramework
//run
$./test
the Dialog can appear, but it seems be blocked ,it has no focus , I can not input anything, I can do nothing ,what is wrong?
now , I got the solution.
In OS X 10.6 ,I write a npapi plugin for Safari, In this plugin I invoke an framework which shows a Modal Window to get password. after close this modal window ,the Safari seems to block.
In my framework , I show this modal window by runModalForWindow function ,when I change to runModalSession , everything works perfectly. I don`t know why but it really works for me .

Theos for armv7 and arm64

I'm trying to get theos working on OSX Mavericks. I recently purchased an iPhone 5s and have since then jailbroken it. Now I am trying to get Theos working so I can start working on some tweaks again. I had it working on OSX Lion and for IOS 5 and 6. I have a very simple program which should display a UIAlert when an application launches. The problem is, when i run the make command in an attempt to compile my code i get this error:
Making all for tweak test...
Preprocessing Tweak.xm...
Compiling Tweak.xm...
Linking tweak test...
Undefined symbols for architecture armv7:
"_OBJC_CLASS_$_UIAlertView", referenced from:
objc-class-ref in Tweak.xm.b0410391.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [obj/test.dylib.1cc22e7c.unsigned] Error 1
make[1]: *** [internal-library-all_] Error 2
make: *** [test.all.tweak.variables] Error 2
Williams-MacBook-Pro-2:test williamfsmillie$
Here is my code for Tweak.xm:
%hook SBApplicationIcon
-(void)launch{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"TEST" message:#"message...." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
%orig;
}
%end
And my makefile:
export SDKVERSION=7.0
include theos/makefiles/common.mk
TWEAK_NAME = test
test_FILES = Tweak.xm
ARCHS = armv7 arm64
test_FRAMEWORKS = UIKit
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
install.exec "killall -9 SpringBoard"
Thanks!
It's an old question, but still I figured I should answer it for people who have the same question. You need to call objc_getClass for it to work, like this:
UIAlertView *alert = [[objc_getClass("UIAlertView") alloc] initWithTitle:#"TEST" message:#"message...." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
Note that this is not required on the left hand side of the assignment.
Edit your makefile and insert the following at the top:
export ARCHS = armv7 armv7s arm64
export TARGET = iphone:clang:7.0:7.0
Also, link the Foundation framework with your tweak.
To fix the alert issue, you must include UIKit/UIKit.h (Sorry, I can't comment)
You need to include the UIKit framework in your Makefile by adding XXX_FRAMEWORKS = UIKit where XXX is your project name
If you're using iOS 7, you have to hook the correct method: try -(void)launchFromLocation:(int)location.
Because it uses a parameter, your code should look like this:
-(void)launchFromLocation:(int)location {
// your stuff
%orig(location);
}
I would say update your Headers.
Download new set from
rpetrich

compiling Objective-C on Ubuntu using GCC

ok i have this program here:
int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog (#"Hello world!");
[pool drain];
return 0;
}
the problem is when i compile it with the command
"gcc -framework Foundation prog1.m -o prog1"
i get this:
"gcc: error trying to exec 'cclobj' : execvp: No such file or Directory"
do i need to install any packages??
"
Its not the only way but GNUStep worked for me. For a good writeup on setting it up look here.
Note: Your exact error is listed about halfway down that page. Your missing package seems to be 'gobjc'.
You need to install "gobjc"
Example
gcc -x objective-c -o
check this link Compiling Objective-C using the gcc
http://webcache.googleusercontent.com/search?q=cache:iIgkFc-JoRYJ:https://www.cs.indiana.edu/classes/c304/ObjCompile.html+http://www.it.uc3m.es/mibanez/lao/lab1/tutorial3/ObjCompile.html&cd=1&hl=en&ct=clnk&client=safari

Link error while accessing NSSpeechSynthesizer

I'm trying to compile a trivial command-line tool with XCode:
#import <Cocoa/Cocoa.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//NSSpeechSynthesizer *speeker = [[NSSpeechSynthesizer alloc] initWithVoice: nil];
NSLog(#"%#", [NSSpeechSynthesizer availableVoices]);
NSLog(#"Hello, World!");
[pool drain];
return 0;
}
and even thought I'm importing Cocoa.h, I'm getting a link error:
Undefined symbols:
"_OBJC_CLASS_$_NSSpeechSynthesizer",
referenced from:
objc-class-ref-to-NSSpeechSynthesizer
in byc.o ld: symbol(s) not found
collect2: ld returned 1 exit status
Anybody knows what's going on???
You imported the header, so compilation worked, but linking failed because you didn't link against a framework that provides NSSpeechSynthesizer. You need to link against either the Application Kit framework (in addition to Foundation) or the Cocoa umbrella framework (instead of Foundation).
Whichever framework you choose, add it to your Linked Frameworks group in your project's group tree (by right-clicking on the group and choosing “Add Existing Framework”), and make sure you also add it to your target.