0

I have written a Objective C module that invokes a C++ binary file. I am using NSTask for that. Code snippet is below.

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

When objective C is hitting C++ binary, I am getting below error

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

What I have although observed is that Binary path is displayed twice in XCode console. Does that essentially mean my code is working and it's failing when attempting to do same second time? If this were to be the case, I would expect that executable pop up be displayed (and take cin inputs) for first success run but I don't get any executable popped up.

PS: I am able to execute binary by doing bash from terminal and that is working fine. Its not working only through XCode

rmaddy
  • 314,917
  • 42
  • 532
  • 579
JMD
  • 337
  • 4
  • 16
  • I've removed the C++ tag. The language used to create the binary you want to execute isn't relevant. – Captain Obvlious Sep 12 '15 at 16:33
  • 1
    If you make `filePath` something like "/bin/ls", does it work? (That would focus the problem on either the CPPBinary -- or its path -- vs. the code.) – Phillip Mills Sep 12 '15 at 16:39
  • I am unable to fight "right" bin in my OS X. I looked into folders in my Mac for Bin, Bash and Terminals and none of them have executables such as bash (I was hoping I can paste my executable in that folder as Phillip mentioned) – JMD Sep 13 '15 at 00:53

1 Answers1

2

filePath and launchPath must be interchanged and this will work. Below is the code

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
JMD
  • 337
  • 4
  • 16