0

我正在尝试通过以下操作从我的应用程序中编译一个文件:

- (IBAction)build:(id)sender
{
    pid_t pid=fork();
    int status;
    if(!pid)
    {
        execl("/Developer/usr/bin/gcc","-o main ~/main.c");
        exit(0);
    }
    waitpid(pid, &status, 0);
}

我的主目录中的文件 main.c 未编译(我没有找到可执行文件)。
我会知道:
1)如何将参数传递给gcc?我正在传递 ~/main.c,但我想传递带有文档完整路径的文件名。如何获取 NSDocument 的文件名?
2)我如何获得控制台输出?我还需要打印gcc输出,如何做到这一点?

编辑:我修改了代码以编译从应用程序保存的文件:

- (IBAction)build:(id)sender
{
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:@"/Developer/usr/bin/gcc"];
    [task setArguments: @[@"-o",@"main",[self fileURL]] ];
    NSPipe *pipe = [NSPipe pipe];
    [task setStandardOutput:pipe];
    NSFileHandle *file = [pipe fileHandleForReading];
    [task launch];
    NSData *data = [file readDataToEndOfFile];
    NSString *output = [[NSString alloc] initWithData:data
                                             encoding:NSUTF8StringEncoding];
    NSLog (@"gcc output:\n%@", output);
}

但是每当构建方法被执行时,这就是我得到的:

2012-11-07 19:04:30.636 Coder[1856:303] -[NSURL fileSystemRepresentation]: unrecognized selector sent to instance 0x100154c40
2012-11-07 19:04:30.638 Coder[1856:303] -[NSURL fileSystemRepresentation]: unrecognized selector sent to instance 0x100154c40
2012-11-07 19:04:30.642 Coder[1856:303] (
    0   CoreFoundation                      0x00007fff95b060a6 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff909793f0 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff95b9c6ea -[NSObject(NSObject) doesNotRecognizeSelector:] + 186
    3   CoreFoundation                      0x00007fff95af45ce ___forwarding___ + 414
    4   CoreFoundation                      0x00007fff95af43b8 _CF_forwarding_prep_0 + 232
    5   Foundation                          0x00007fff8cbf8e67 -[NSConcreteTask launchWithDictionary:] + 814
    6   Coder                               0x0000000100001734 -[Document build:] + 452
    7   AppKit                              0x00007fff8d266a59 -[NSApplication sendAction:to:from:] + 342
    8   AppKit                              0x00007fff8d2668b7 -[NSControl sendAction:to:] + 85
    9   AppKit                              0x00007fff8d2667eb -[NSCell _sendActionFrom:] + 138
    10  AppKit                              0x00007fff8d264cd3 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 1855
    11  AppKit                              0x00007fff8d264521 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 504
    12  AppKit                              0x00007fff8d263c9c -[NSControl mouseDown:] + 820
    13  AppKit                              0x00007fff8d25b60e -[NSWindow sendEvent:] + 6853
    14  AppKit                              0x00007fff8d257744 -[NSApplication sendEvent:] + 5761
    15  AppKit                              0x00007fff8d16d2fa -[NSApplication run] + 636
    16  AppKit                              0x00007fff8d111cb6 NSApplicationMain + 869
    17  Coder                               0x00000001000011a2 main + 34
    18  libdyld.dylib                       0x00007fff93a577e1 start + 0
    19  ???                                 0x0000000000000003 0x0 + 3
)

trojanfoe 代码可以正常工作,但经过此修改后,它就不起作用了。

4

2 回答 2

1

改为使用NSTask参考)。

未经测试:

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *sourceFile = [docDir stringByAppendingPathComponent:@"main.c"];
NSString *outFile = [docDir stringByAppendingPathComponent:@"main"];

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/Developer/usr/bin/gcc"];
[task setArguments:[NSArray arrayWithObjects: @"-o", outFile, sourceFile, nil]];

NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];

NSFileHandle *file = [pipe fileHandleForReading];

[task launch];

NSData *data = [file readDataToEndOfFile];

NSString *output = [[NSString alloc] initWithData:data
                                         encoding:NSUTF8StringEncoding];
NSLog (@"gcc output:\n%@", output);

[output release];
[task release];
于 2012-11-07T12:19:51.490 回答
1

参数列表 toexecl()应以 . 结尾(char *) NULL

所以调用应该是这样的:

execl("/Developer/usr/bin/gcc","-o main ~/main.c", (char *) NULL);

甚至更好地允许gcc找到参数本身:单独的参数:

execl("/Developer/usr/bin/gcc", "-o", "main", "~/main.c", (char *) NULL);

更新:

正如trojanfoe提到的,这里的扩展是一个问题。因此,为了将~in~/main.c扩展到用户的家中,需要涉及一个 shell,如下所示:

execl("/bin/sh", "-c", "gcc -o main ~/main.c", (char *) NULL);
于 2012-11-07T12:20:18.867 回答