简体   繁体   中英

Runtime error when compiling Objective-C project using g++

I've made a really small project using Objective-C, and If I run it with the Xcode, It works really well.

But, I need to compile it using below command lines:

g++ Main.mm -o Main `gnustep-config --objc-flags` `gnustep-config --base-libs` -O2 -DONLINE_JUDGE -DBOJ

This command line is what the site I'm trying to upload my code to compile uses to compile Objective-C projects.

But, whenever I compile whit that command line, I get runtime errors for using NSMutableArray , NSSet and NSString .

Errors:

zsh: command not found: gnustep-config
zsh: command not found: gnustep-config
Undefined symbols for architecture x86_64:
  "_OBJC_CLASS_$_NSMutableArray", referenced from:
      objc-class-ref in Main-49f6e3.o
  "_OBJC_CLASS_$_NSSet", referenced from:
      objc-class-ref in Main-49f6e3.o
  "_OBJC_CLASS_$_NSString", referenced from:
      objc-class-ref in Main-49f6e3.o
  "___CFConstantStringClassReference", referenced from:
      CFString in Main-49f6e3.o
  "_objc_msgSend", referenced from:
      _main in Main-49f6e3.o
  "_objc_opt_new", referenced from:
      _main in Main-49f6e3.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

And, here is my source code:

#import <Foundation/Foundation.h>

int main(void) {
    int num = 0;
    int answer = 0;
    scanf("%i", &num);
    NSMutableArray *cl = [NSMutableArray new];
    
    for (int k = 0; k < num; k++) {
        char str[4];
        getchar();
        scanf("%[^\n]s", str);
        NSString *userInput = [NSString stringWithUTF8String:str];
        
        if (userInput.length == 1) {
            [cl removeObjectAtIndex:0];
        } else {
            [cl addObject:[userInput substringFromIndex:[userInput length] - 1]];
        }
        
        NSMutableArray *result = [NSMutableArray new];
        for (int i = 1; i <= cl.count; i++) {
            for (int j = 0; j <= cl.count-i; j++) {
                [result addObject:[[cl subarrayWithRange:NSMakeRange(j, i)] componentsJoinedByString:@""]];
            }
        }
        
        answer += [[NSSet setWithArray:result] allObjects].count;
    }
    
    printf("%d\n", answer%1000000007);
    
    return 0;
}

Those are linker errors, not runtime errors.

You need to link against the objc library and Foundation framework. Try:

g++ Main.mm -lobjc -framework Foundation ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM