Ubuntu compile and run C ++ code

After installing Ubuntu, (sometimes called home user directory folder or Home folder, its path /home/username, where username is the user name used to log Linux .Linux we will create a login for each user in the home directory a folder specifically used to store the user to use configuration files, text documents, pictures, and other executable program, and other users to distinguish) is a series of input commands:

1. First, enter the command pwd view the directory where you are. pwd command: View the current path.

2. Enter the ls command to view all the directories in the directory.

3. Enter the command cd command: switch the current directory to another directory (cd directory name to be entered) entered in the directory, (cd ..) returns the parent directory.

cd /: slash representation is entered into the root directory.
(1) cd enter the user's home directory
(2) cd ~ enter the user's home directory
(3) cd - Returns the directory before entering the directory
(4) cd .. back to the parent directory
(5) cd ../ .. Back the two directories
(6) cd! $ parameters on the command cd is used as a parameter

4. After entering the directory, enter the command (mkdir file name) represents the creation of a new file in the directory

mkdir command to create the specified directory name, requires the creation of almost dizzy with permissions in the current directory, and the directory can not be created in the current directory already.

mkdir [options] [directory]

Format options:

-m --mode = mode, create a directory at the same time when setting directory permissions;

-p --parents if the established parent directory does not exist, and this directory is created. Namely to ensure the directory name exists, that does not exist on a build.

-v --verbose each time you create a new directory will display information

-h --help Help

E.g:

Create an empty directory hello

mkdir hello

Creating multiple directories recursively

mkdir -p hello/word

Create a directory permissions for 111

mkdir -m 111 test

Create a test / test1 under the current directory permissions to 111

mkdir -p-m 111 test/test1

After 5.mkdir hello, enter the command cd hello into the folder, and now is to build the executable C / C ++ file it, enter the command: touch + filename.c / filename.cpp, touch is to establish command file, .c / .cpp suffix indicates the type of file. Create a file called c file Hello.c here;

A. Use Gedit editor (Gedit is a simple and practical text editor, elegant interface, supports syntax highlighting, easy to use than the Vim editor under Windows and it is no different)

(If you are using other Linux distributions, it may not be installed by default Gedit, this time you have to install their own, specific steps:

$ Sudo apt-add-repository ppa : ubuntu software add-ON-Rails source ubuntu / ppa #
$ sudo APT-GET Update # updated list of software
$ sudo apt-get install gedit- gmate # installation)

 

($ Cd hello # enter the directory hello, this is the directory where the source files
$ touch hello.c # using a touch command to create an empty file called hello.c
$ gedit hello.c # use the gedit command to edit hello.c) so completes the creation of the source file, and can use Gedit edit source files, ( Ctrl+Ssave the file to complete the editing source code. at this point need to close Gedit window, $ gedit main.c关闭完窗口后这条command execution ends considered in order to continue in the console input other commands).

B.使用GCC编译器(使用 GCC 来编译C语言程序)GCC 仅仅是一个编译器,没有界面,必须在命令行模式下使用。通过gcc命令就可以将源文件编译成可执行文件

6.文件建立好后就是双击文件用Linux系统自带的Vim文本编辑器来写代码啦;

保存好c文件后接下来就是编译成可执行文件,输入命令:gcc Hello.c -o hello,gcc是编译c文件的命令,若是.cpp文件则用g++,-o后面的hello就是可执行的文件名(可随意起一个filename);

注意:如果没有在Hello.c文件中正确输入代码,或者完全没有输入任何东西是编译失败的。

最后,就是执行啦,输入命令:./hello,“./”是运行可执行文件的命令;

扩展:

(1)$ gcc hello.c  #在 gcc 命令后面紧跟源文件名

打开 hello.cpp 目录(是个文件夹),会看到多了一个名为 hello.out 的文件,这就是最终生成的可执行文件。

这样就一次性完成了编译和链接的全部过程,非常方便。Linux 不以文件后缀来区分可执行文件,Linux 下的可执行文件后缀理论上可以是任意的, 这里的.out只是用来表明它是 GCC 的输出文件,不管源文件的名字是什么,GCC 生成的可执行文件的默认名字始终是a.out

(2)如果不想使用默认的文件名,那么可以通过-o选项来自定义文件名,例如:$ gcc hello.c -o hello.out  (这样生成的可执行程序的名字就是main.out。)

(3)因为 Linux 下可执行文件的后缀仅仅是一种形式上的,所以可执行文件也可以不带后缀,例如: $ gcc hello.c -o hello   (这样生成的可执行程序的名字就是main)

(4)通过-o选项也可以将可执行文件输出到其他目录,并不一定非得在当前目录下,例如: $ gcc hello.c -o ./out/hello.out    或者 $ gcc hello.c -o out/hello.out

表示将可执行文件输出到当前目录下的out目录,并命名为hello.out./表示当前目录,如果不写,默认也是当前目录。 (注意:out 目录必须存在,如果不存在,gcc 命令不会自动创建,而是抛出一个错误。)
 

C.运行可执行程序

7.上面我们生成了可执行程序,要运行他,只用在控制台中输入程序的名字就可以,如下所示:

$ ./hello.out        即   $ ./a.out  

./表示当前目录,整条命令的意思是运行当前目录下的 a.out 程序。如果不写./,Linux 会到系统路径下查找 a.out,而系统路径下显然不存在这个程序,所以会运行失败。

所谓系统路径,就是环境变量指定的路径,我们可以通过修改环境变量添加自己的路径,或者删除某个路径。很多时候,一条Linux命令对应一个可执行程序,如果执行命令时没有指明路径,那么就会到系统路径下查找对应的程序。

输入完上面的命令,按下回车键,程序就开始执行了,它会将输出结果直接显示在控制台上,

如果程序在其它目录下,运行程序时还要带上目录的名字,例如:

$ ./out/hello.out   或者  $ out/main.out

这个时候加不加./都一样,Linux 能够识别出out是一个目录,而不是一个命令,它默认会在当前路径下查找该目录,而不是去系统路径下查找,所以不加./也不会出错。

注意,如果程序没有执行权限,可以使用sudo命令来增加权限,例如:

$ sudo chmod 777 a.out

完整步骤:

$ cd hello.cpp  #进入源文件所在目录
$ touch hello.c  #新建空白的源文件
$ gedit hello.c  #编辑源文件
$ gcc hello.c  #生成可执行程序
$ ./hello.out  #运行可执行程序
   hello 
$   #继续输入其它命令

 

 

 

 

 

 

 

 

 

 

发布了28 篇原创文章 · 获赞 4 · 访问量 1万+

Guess you like

Origin blog.csdn.net/m0_37957160/article/details/103486635
Recommended