2016-10-27 140 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,如果您不需要從子進程退出代碼。