1

我是 Linux 操作系统的绝对初学者,只是为了好玩,我在终端输入了 system() 接下来发生的事情如下:

satish@satish-Inspiron-N5010 ~ $ system()
>#include<iostream>
>int main()
bash: syntax error near unexpected token 'int'
satish@satish-Ispiron-N5010~ $

在这里我想知道什么是 system() ?它在这里的作用是什么?为什么我在 int main() 行中出现错误?我们可以在终端中使用 C/C++ 编程做什么?

4

2 回答 2

4

这里我想知道什么是system()?

当你说system()并点击Enter时,shell 以为你要定义一个名为system.

以 开头的任何内容#都被 shell 解释为注释。

int main()由于您的函数体尚未启动,因此您在该行中遇到错误。

shell 函数的语法是:

function: function name { COMMANDS ; } or name () { COMMANDS ; }
    Define shell function.

一个如何定义一个函数并使用它的例子:

$ system()
> { echo $SHELL; }
$ system
/bin/bash
于 2013-10-29T10:03:07.820 回答
0

你不能在终端上用 C/C++ 编程做任何事情。终端无法识别 c/c++ 语言。linux 终端根据您使用的 shell 识别 bash、csh 脚本语言。因此你会得到 main() 的错误,因为它们在 bash 脚本中没有这样的东西。对于 c/c++ 程序,你有 c/c++ 编译器,如 gcc,linux 中的 g++,编译你必须输入 gcc/g++ hello.c/.cpp。浏览这个基本教程http://www.ee.surrey.ac.uk/Teaching/Unix/

关于system(),用于在linux中执行shell命令http://linux.die.net/man/3/system

于 2013-10-29T09:59:02.300 回答