ios - C/XCode : sh: ./child : No such file or directory. 命令在终端中运行,但不在 XCode 中运行

标签 ios c xcode terminal

所以我有一个 child.c 文件,我想使用 stdlib.h 的 system() 函数在我的 main.c 文件中编译和运行它。

child .c:

#include<stdio.h>
int main(){
printf("I am the child\n");
return 0;
}

主.c:

#include <stdio.h>
#include <stdlib.h>

int main() {
    system("cd ~/Desktop/HW3/HW3");
    system("gcc -o child child.c");
    system("./child");
    return 0;
}

当我使用以下命令在终端中编译并运行 main.c 时一切正常

abcs-mbp:HW3 abc$ cd
abcs-mbp:~ abc$ cd ~/Desktop/HW3/HW3
abcs-mbp:HW3 abc$ gcc -o main main.c
abcs-mbp:HW3 abc$ ./main

它运行 child.c 并打印以下内容:

I am the child

但是当我尝试在 XCode 中运行完全相同的 main.c 时,XCode 给了我以下错误:

clang: error: no such file or directory: 'child.c'
clang: error: no input files
sh: ./child: No such file or directory

有人知道为什么会这样吗?我认为它与路径有关,但我如何告诉 XCode child.c 的路径,然后告诉它编译 child.c?

我也试过

 system("cd ~/Desktop/HW3/HW3");
 system("gcc -o child child.c");
 system("./child");

system("/Users/vqianxiao/Desktop/HW3/HW3/ gcc -o child child.c");

但似乎没有任何效果......感谢任何帮助!

最佳答案

它不起作用的原因是多种因素的结合

1) each 'system' call is run in a separate shell instance, 
   so the second 'system' call 
   starts from the directory where the main program is running.

2) things do down from there

建议的修复:

system("cd ~/Desktop/HW3/HW3; && gcc -o child child.c; && ./child;");

注意,所有一个系统调用,命令末尾的终止符,并由 && 链接,所以一个命令必须成功才能继续执行下一个命令

关于ios - C/XCode : sh: ./child : No such file or directory. 命令在终端中运行,但不在 XCode 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29201069/

相关文章:

ios - 日期格式化程序不起作用?

ios - 为什么 UICollectionView 的 didSelect 方法不起作用?

ios - AV Foundation Framework 视频质量检查

c - 请求成员 n 不是结构或 union 的东西

ios - ABCreateStringWithAddressDictionary 隐式声明

c++ - 与链接器无关的链接器错误

arrays - 我正在尝试使用 rand() 从数组中打印随机字符

ios - 为什么lldb的po命令不能在dot中打印superview?

ios - 试图使 HTTP get 请求在 swift 上工作

ios - AppStore-iOS,无法在testflight中发布相同版本的新版本