Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编译和运行C++;在Linux中用一个命令编写代码 我的问题很简单:有没有一种方法可以在一行中从Linux终端编译和运行C++代码?假设C++程序在一个名为Fo.CPP:的文件中编码。 g++ myfile.cpp -o myfile && ./myfile g++ foo.cpp -o foo && ./foo 注释:“& &”:只有在左边的命令成功了_C++_Linux_Terminal_Make - Fatal编程技术网

编译和运行C++;在Linux中用一个命令编写代码 我的问题很简单:有没有一种方法可以在一行中从Linux终端编译和运行C++代码?假设C++程序在一个名为Fo.CPP:的文件中编码。 g++ myfile.cpp -o myfile && ./myfile g++ foo.cpp -o foo && ./foo 注释:“& &”:只有在左边的命令成功了

编译和运行C++;在Linux中用一个命令编写代码 我的问题很简单:有没有一种方法可以在一行中从Linux终端编译和运行C++代码?假设C++程序在一个名为Fo.CPP:的文件中编码。 g++ myfile.cpp -o myfile && ./myfile g++ foo.cpp -o foo && ./foo 注释:“& &”:只有在左边的命令成功了,c++,linux,terminal,make,C++,Linux,Terminal,Make,,这才是正确的命令。这是一个比C++问题更像shell脚本的问题。在大多数shell中,有许多链接命令的方法。假设您使用的是bash(请尝试echo$SHELL以确保),请查看一个好的解决方案。尝试以下技巧: 将此行粘贴在cpp文件的顶部: //&>/dev/null;x="${0%.*}";[ ! "$x" -ot "$0" ]||(rm -f "$x";g++ -o "$x" "$0")&&exec "$x" "$@" 然后在cpp文件上添加执行权限,即(ch

,这才是正确的命令。这是一个比C++问题更像shell脚本的问题。在大多数shell中,有许多链接命令的方法。假设您使用的是bash(请尝试
echo$SHELL
以确保),请查看一个好的解决方案。

尝试以下技巧:

将此行粘贴在cpp文件的顶部:

//&>/dev/null;x="${0%.*}";[ ! "$x" -ot "$0" ]||(rm -f "$x";g++ -o "$x" "$0")&&exec "$x" "$@"
然后在cpp文件上添加执行权限,即(
chmod+x foo.cpp
),然后:


一行:是的;请参阅其他答案

在一个命令中:no.

通过“一个命令”,我假设OP意味着从命令行调用恰好一个二进制文件


一行:是的;请参阅其他答案

在一个命令中:确定-您可以利用一些隐式规则,并通过bash-here字符串推入您自己的运行规则:

$ ls hello* *ake*
ls: cannot access *ake*: No such file or directory
hello.c
$ cat hello.c
#include <stdio.h>
int main (int argc, char **argv) {
    printf("%s %s\n", "hello", "world");
    return (0);
}
$ make hello.run -f - <<< 'hello.run: hello; ./$<'
cc     hello.c   -o hello
./hello
hello world
$ 
$ls你好**ake*
ls:无法访问*ake*:没有这样的文件或目录
你好,c
$cat hello.c
#包括
int main(int argc,字符**argv){
printf(“%s%s\n”、“你好”、“世界”);
返回(0);
}

$make hello.run-f-实际上不是一个命令。@johndilling OP在问题中指定了“一行”和“一个命令”。OP要求一行,而不是一个命令(后者在
shell
术语中甚至没有意义:从技术上讲,上面的命令仍然是一个命令)嗯,题目和问题略有不同。…@AlexanderL.Belikoff:再看一看问题题目。很可怕,尽管很有趣(因此是+1),解决方案!它也不能完全满足OP的问题,因为在第一次创建任何.cpp文件时,它需要多个命令。@JBentley,呵呵,这不是为了正确使用,但对于小测试片段很有用。。。关于命令,这简直是吹毛求疵……;)+如果程序不在一个文件中(并且您需要一个makefile),那么makefile不一定是真正的文件-它们可以通过stdin和bash-here字符串到达。请参阅
makeexecute
,假设您对makefile进行了适当的更改。请参阅如何一次性执行command@DigitalTrauma是的,这很聪明,但不是一个命令。
$ ls hello* *ake*
ls: cannot access *ake*: No such file or directory
hello.c
$ cat hello.c
#include <stdio.h>
int main (int argc, char **argv) {
    printf("%s %s\n", "hello", "world");
    return (0);
}
$ make hello.run -f - <<< 'hello.run: hello; ./$<'
cc     hello.c   -o hello
./hello
hello world
$