gpt4 book ai didi

c - 在 Ubuntu 中从另一个 C 文件中运行一个 C 文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:44:13 24 4
gpt4 key购买 nike

我需要在 Ubuntu 上的另一个 C 程序中运行一个 C 程序。就像是scanf i//假设输入了 i=2

switch (i){
case 1: print xyz;
break;
case 2: cc abc.c -lpthread (and then) ./a.out //execute this command to execute file with name abc
break;
}

该怎么做?我彻底搜索了谷歌,但找不到合适的答案。


编辑:我现在从 bash 文件运行上述执行命令。它以一种简单的方式工作并解决了我的要求:D

#! /bin/bash
read a
if [ $a -eq 1 ]
then
cc ex.c -lpthread
./a.out
else
echo "hi"
fi

最佳答案

你需要利用fork() and exec()产生一个子进程(不管那个进程在做什么——我注意到你正在编译和运行一个新进程)。

如果您想生成一个进程并等待它,只需依靠返回(错误)代码来确定成功,那么 system() 是一个选项。

关于c - 在 Ubuntu 中从另一个 C 文件中运行一个 C 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23060873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com