使用Objective C执行C ++二进制文件。获取“无法执行二进制文件”

时间:2015-09-12 16:27:37

标签: objective-c cocoa nstask

我编写了一个调用C ++二进制文件的Objective 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

当目标C击中C ++二进制文件时,我的误差低于

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

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

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

工作

1 个答案:

答案 0 :(得分:2)

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