簡體   English   中英

使用 g++ 編譯 Objective-C 項目時出現運行時錯誤

[英]Runtime error when compiling Objective-C project using g++

我使用 Objective-C 做了一個非常小的項目,如果我用 Xcode 運行它,它運行得非常好。

但是,我需要使用以下命令行編譯它:

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

這個命令行是我試圖上傳我的代碼編譯的站點用來編譯 Objective-C 項目的。

但是,每當我使用該命令行進行編譯時,都會遇到使用NSMutableArrayNSSetNSString運行時錯誤。

錯誤:

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)

而且,這是我的源代碼:

#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;
}

這些是鏈接器錯誤,而不是運行時錯誤。

您需要鏈接到objc庫和Foundation框架。 嘗試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM