使用 Objective C 执行 C++ 二进制文件。获取 "cannot execute binary file"

Executing C++ binary using Objective C. Getting "cannot execute binary file"

我已经编写了一个 Objective C 模块来调用 C++ 二进制文件。我正在为此使用 NSTask。代码片段如下。

bool filePathExists;

NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *filePath = @"/Users/MyName/Documents/CodeLocation/Debug/CPPBinary";

if ([fileManager fileExistsAtPath:filePath]) {
    filePathExists = YES;
} else {
    filePathExists = NO;
}

if (filePathExists) {
    NSTask *terminalOperation = [[NSTask alloc] init];
    terminalOperation.launchPath = @"/bin/bash";
    NSArray *argumentsArray = [NSArray arrayWithObjects:filePath, nil];
    [terminalOperation setArguments:argumentsArray];
    [terminalOperation launch];
    [terminalOperation waitUntilExit];
}

NSString *temporaryData = [[NSString alloc]initWithFormat

当 objective C 访问 C++ 二进制文件时,出现以下错误

/Users/MyName/Documents/CodeLocation/Debug/CPPBinary:
/Users/MyName/Documents/CodeLocation/Debug/CPPBinary:
cannot execute binary file

尽管我观察到二进制路径在 XCode 控制台中显示了两次。这是否实质上意味着我的代码正在运行并且在第二次尝试执行相同操作时失败了?如果是这种情况,我希望在第一次成功时显示可执行弹出窗口(并接受 cin 输入)运行,但我没有弹出任何可执行文件。

PS:我可以通过从终端执行 bash 来执行二进制文件,并且工作正常。它不能仅通过 XCode

工作

filePath 和 launchPath 必须互换,这样才行。下面是代码

bool filePathExists;

NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *filePath = @"/bin/bash";

if ([fileManager fileExistsAtPath:filePath]) {
    filePathExists = YES;
} else {
    filePathExists = NO;
}

if (filePathExists) {
    NSTask *terminalOperation = [[NSTask alloc] init];
    terminalOperation.launchPath = @"/Users/MyName/Documents/CodeLocation/Debug/CPPBinary";
    NSArray *argumentsArray = [NSArray arrayWithObjects:filePath, nil];
    [terminalOperation setArguments:argumentsArray];
    [terminalOperation launch];
    [terminalOperation waitUntilExit];
}

NSString *temporaryData = [[NSString alloc]initWithFormat