【问题标题】:How to compile in Xcode C file with C11 language dialect?如何用 C11 语言方言编译 Xcode C 文件?
【发布时间】:2017-03-09 21:01:35
【问题描述】:

我要编译这个源代码:

#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 中无效
  • 隐式声明库函数“exit”,类型为“void (int)attribute((noreturn))”
  • 函数 'wait' 的隐式声明在 C99 中无效
  • 函数调用的参数太少,预期为 1,有 0

Project settings:

系统:

产品名称:Mac OS X 产品版本:10.12.1 构建版本:16B2555

Xcode 版本 8.0 (8A218a)

Apple LLVM 版本 8.0.0 (clang-800.0.38) 目标:x86_64-apple-darwin16.1.0 线程模型:posix

【问题讨论】:

    标签: xcode llvm libc c11


    【解决方案1】:

    有几个问题:

    1. Xcode 看不到函数定义,这就是为什么它说“函数 'foobar' 的隐式声明在 C99 中无效”。尽管 Xcode 尝试编译您的代码,但假设未知函数将在链接阶段得到解决。在exitwait 的情况下,它会起作用,但要抑制警告,您只需要包含stdlib:#include &lt;stdlib.h&gt;。 从gets_s 开始,您需要执行一些额外操作才能使其正常工作(我不知道它来自哪里)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      • 2020-04-20
      • 1970-01-01
      • 2017-09-03
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多