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.
Related
Given this MVP code:
objc_funcs.h
#import <Foundation/Foundation.h>
NSString* doFoo(void);
objc_funcs.m
#import <Foundation/Foundation.h>
NSString* doFoo()
{
return #"fffuuu";
}
main.mm
#import "objc_funcs.h"
int main(int argc, char * argv[]) {
doFoo();
return 0;
}
If I leave it this way, the build will result in
Undefined symbols for architecture x86_64:
"doFoo()", referenced from:
_main 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)
Though if I rename main.mm -> main.m, the build will go just fine.
WAIDW? đŸ˜–
The problem relates to C vs C++ linkage.
The normal way this is handled in C headers is testing for the __cplusplus preprocessor macro and inserting an extern "C" if needed. CoreFoundation provides the CF_EXTERN_C_BEGIN and CF_EXTERN_C_END macros that handle this:
#if !defined(CF_EXTERN_C_BEGIN)
#if defined(__cplusplus)
#define CF_EXTERN_C_BEGIN extern "C" {
#define CF_EXTERN_C_END }
#else
#define CF_EXTERN_C_BEGIN
#define CF_EXTERN_C_END
#endif
#endif
Using these your objc_funcs.h becomes:
#import <Foundation/Foundation.h>
CF_EXTERN_C_BEGIN
NSString* doFoo(void);
CF_EXTERN_C_END
if you don't want to use them you could use
#import <Foundation/Foundation.h>
#ifdef __cplusplus
extern "C"
#endif
NSString* doFoo(void);
You have to make it valid for C++ compiler by either converting objc_funcs to .mm file or wrapping the import in main.mm with extern "C" {}:
extern "C" {
#import "objc_funcs.h"
}
You could read more about it here
Just starting out with Objective C so please be gentle. I've got a class as follows:
Card.h
#import <Foundation/Foundation.h>
#interface Card : NSObject
#property (nonatomic) NSUInteger x;
-(NSUInteger) getNum;
#end
Card.m
#import "Card.h"
#implementation Card
-(NSUInteger) getNum {
return self.x;
}
#end
main.c
#include <CoreFoundation/CoreFoundation.h>
#include "Card.h"
int main(int argc, const char * argv[])
{
return 0;
}
When I compile, I get a load of errors, first one is:
NSObjCRuntime.h: Parse Issue: Expected identifier or '('.
I know this is something stupid - just hoping somebody here can spot what i'm doing wrong.
Are you actually compiling this with an Objective C compiler? The traditional extension to instruct GCC and clang to use ObjC is .m (not .c).
Alright, I'm trying to call a function in xcode but apparently it isn't working. I made an objective-c class, and typed in the following code into the implementation file:
#import "Person.h"
#implementation Person
void printthis()
{
NSLog(#"Hi, I have been printed");
}
int main(int argc, const char * argv[])
{
#autoreleasepool {
printthis();
}
return 0;
}
#end
Apparently, it returns the following error in xcode:
ld: 1 duplicate symbol for architecture x86_64
clang: error:
linker command failed with exit code 1 (use -v to see invocation)
Did you already have a main function somewhere else (probably main.m ?). If so the linker got confused -- you are not supposed to have duplicates of main function
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.
Step 1. Create a simple console app in Xcode
Step 2. Create simple objective C class in same project
Step 3. Try to import like this:
#include <stdio.h>
#import "MyClass.h"
int main (int argc, const char * argv[])
{
// insert code here...
printf("Hello, World!\n");
return 0;
}
Why won't this compile? Xcode error is "too many errors emitted stopping now"
Because your source file is a C module. Change the extension to .m. Better yet, start with the Foundation-based tool project template in Xcode.