I have not been able to find an answer for this on Google. I am building a command line utility in Cocoa and when I try to create an instance of NSWorkspace
I get a compiler error. This is the code I am implementing in main.m
, which is pretty simple:
NSArray *runningApps = [[NSWorkspace sharedWorkspace] runningApplications];
for (NSRunningApplication *app in runningApps) {
if (![app terminate])
[app forceTerminate];
}
When I attempt to compile and run the program this is what I get:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_NSWorkspace", 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)
I've used this same code in other apps and had no problem. I know NSWorkspace
is defined in <Foundation/Foundation.h>
and I have <Cocoa/Cocoa.h>
imported which covers Foundation. Any ideas on what the issue could be?
I know NSWorkspace is defined in
<Foundation/Foundation.h>
No, it's in <AppKit/NSWorkspace.h>
which is imported by <AppKit/AppKit.h>
. <Cocoa/Cocoa.h>
includes both <AppKit/AppKit.h>
and <Foundation/Foundation.h>
.
Your tool probably links against Foundation only. Change it to link against AppKit too, or just all of Cocoa. In your target's "Link Binary with Libraries" build phase, add "Cocoa.framework" or "AppKit.framework".