Running C language programs under Linux

Source: Internet
Author: User

The most common Linux distributions are distributions, and a typical Linux distribution includes the Linux kernel, desktop environment, and a variety of commonly used tools, including Ubuntu (Bantu), CentOS, Deepin (Deep Linux). This tutorial takes Deepin as an example of how to compile a C program.

Gedit

Gedit is a simple and useful text editor with a beautiful interface that supports syntax highlighting and is easier to use than Vim. This tutorial takes gedit as the C code Editor.

Installation of Gedit:

sudo apt-add-repository Ppa:ubuntu-on-rails/ppa    //Add Ubuntu software source sudo apt-get update    //Update software list sudo apt-get Install Gedit-gmate    //Installation


Create a C source file in the current directory and open it:

Touch Test.cgedit test.c


You can see the Gedit window and enter:

#include <stdio.h> #include <stdlib.h>int main () {printf ("hello,linux.\n"); exit (0);}


Gedit window


Note: Gedit supports highlighting in multiple languages, and generally automatically recognizes languages, and if you identify errors, select C under Edit-highlighting mode.

Gcc

The most widely used C + + compiler in Linux is GCC, and most Linux distributions are installed by default, and for developers and beginners, GCC is generally the preferred compilation tool under Linux. This tutorial does not hesitate to use GCC to compile C programs.

After saving the file, exit, open the terminal and CD to the current directory, enter the following command:
GCC Test.c-o Test
You can link C code compilation directly to an executable file.

You can see that there is one more file in the current directory test, which is the executable file. Unlike Windows,linux, which does not differentiate executables by file suffixes, executable suffixes under Linux can theoretically be arbitrarily changed.

Of course, you can also compile it in steps:

1) pretreatment
GCC-E Test.c-o test.i
In the current directory will be more than a preprocessing result file test.i, open test.i can see, on the basis of test.c stdio.h and stdlib.h the contents of the inserted.

2) Compile as assembly code
Gcc-s Test.i-o Test.s
Where-s parameter is exited after compilation is complete,-O is the specified file name.

3) assembly as the target file
Gcc-c Test.s-o TEST.O
. O is the target file. The target file is similar to an executable file, which is the executable code that the machine recognizes, but the structure is slightly different because there are no links.

3) Link and generate the executable file
GCC Test.o-o Test

If you have more than one source file, you can compile it like this:
Gcc-c Test1.c-o TEST1.O
Gcc-c Test2.c-o TEST2.O
GCC test1.o test2.o-o Test

Note: If you do not specify a filename, GCC generates a file named A.out. out file only to differentiate the compiled file, Linux does not have the standard executable suffix name, the general executable file does not have the suffix name.

The test file generated after compilation is the program, run it:
./test
If you do not have permission to run, you can use the sudo command to increase the permissions (note the Linux partition):
sudo cdmod test 777

We can use the-pedantic,-wall,-werror options for the program's error checking:

    • The-pedantic option helps programmers discover some code that does not conform to the Ansi/iso C standard (not all);

    • -wall allows GCC to display warning messages;

    • -werror can stop GCC from continuing when it encounters an error in the compilation.


These 3 options are very useful.

Transfer from http://mp.weixin.qq.com/s?__biz=MzA3MTA3MTgxNg==&mid=200634381&idx=3&sn= B79e75576ce2eeb494a458f2d79cb994&3rd=mza3mdu4ntyzmw==&scene=6#rd

Running C language programs under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.