2016-10-27 86 views
0

我想编译这个源代码:如何使用C11语言方言编译Xcode C文件?

#include <stdio.h> 
#include <unistd.h> 
#include <fcntl.h> 
#include <string.h> 

int main(int argc, const char *argv[]) { 
While: 

    printf("MacBook-Pro-...:~ ...$ "); 

    char command[128]; 
    gets_s(command); 

    if (strncmp(command, "exit", 4) == 0) 
    exit(0); 

    pid_t return_value = fork(); 

    if (return_value == 0) { 
    int outfile; 
    if ((outfile = dup(1)) == -1) 
     return -1; 

    close(1); 

    if ((outfile = open("/Users/.../1.txt", 
         O_WRONLY | O_TRUNC | O_CREAT, 0644)) >= 0) { 
     execl("/bin/sh", "sh", "-c", command, NULL); 
    } 

    close(outfile); 
    exit(0); 
    } else { 
    wait(); 


    FILE *fp; 
    char str[128]; 
    fp = fopen("/Users/.../1.txt", "r"); 

    while(!feof(fp)) { 
     if(fgets(str, 126, fp)) 
     printf("%s", str); 
    } 

    fclose(fp); 
    goto While; 
    } 

    return 0; 
} 

但我有一些错误: 语义问题

  • 功能“gets_s”的隐式声明是在C99
  • 隐式声明的库函数无效类型为'void(int)attribute((noreturn))'
  • 函数'wait'的隐式声明在C99中无效
  • 太少参数的函数调用,预计1,有0

Project settings:

系统:

产品名称:Mac OS X的 的ProductVersion:10.12.1 BuildVersion:16B2555

Xcode版本8.0(8A218a)

苹果LLVM版本8.0.0(铛-800.0.38) 目标:x86_64的 - 苹果darwin16.1.0 线程模型:POSIX

回答

0

有几个问题:

  1. Xcode中没有看到函数定义,这就是为什么它说'隐式声明函数'foobar'在C99中无效“。尽管Xcode试图编译你的代码,假设未知函数将在链接阶段被解析。在exitwait的情况下,它会工作,但为了抑制警告,您只需包含stdlib:#include <stdlib.h>。 从gets_s开始,您需要做一些额外的操作才能使其工作(我不知道它来自哪里)。

  2. 第二个问题是wait函数的签名如下所示:int wait(int *),而您没有给它任何参数。只需添加0NULL,如果您不需要从子进程退出代码。