A tutorial on compiling C or C + + programs under Linux _c language

Source: Internet
Author: User
Tags mathematical functions

From the beginning to learn C + + We are all under Windows, then how (how) to compile C/s + + code in Linux? C + + code compiled at the Linux Terminal (command line)?

To compile C + + code, such as Ubuntu, Red Hat, Fedora, Debian, and other Linux branches under any Linux branch, we need to install the package:

1.GNU C and C + + compiler collection
2.Development Tools
3.Development Libraries
4.IDE or text editor to write programs
Step One: Install the C + + compiler and related kits

If you are using Fedora, Red Hat, CentOS, or scientific Linux, you can use the Yum command to quickly install the GNU/C + + compiler:

[Root@dabu.info]# yum groupinstall ' Development Tools '

If you are using Debian, Ubuntu Linux, then enter the Apt-get command to install the GNU/C + + compiler;
[Dabu@dabu.info]$ sudo apt-get update
[dabu@dabu.info]$ sudo apt-get install the build-essential manpages-dev
Step two: Confirm that the installation is successful

Display the compiler version and the installed folder by entering the following command:

[Dabu@dabu.info]$ whereis gcc
[dabu@dabu.info]$ which GCC
[dabu@dabu.info]$ gcc--version

The output is shown below:

How to (how to) compile C + + code in Linux

Create a demo.c file, use VI, Emacs, or Joe to enter the following C-source code:

  #include <stdio.h>/
  * Demo.c:my The A Linux
  /int main (void)
  {
  printf ("hello! This is a Test prgoram.\n ");
  return 0;
  }

Next compile:
The usage syntax for compiling is as follows:

[Dabu@dabu.info]$  gcc program-source-code.c-o executable-file-name

Or

[Dabu@dabu.info]$    cc Program-source-code.c-o executable-file-name

Explanation: Program-source-code.c is the C source code, Executable-file-name is the compiled executable file

Or

[Dabu@dabu.info]$ make   executable-file-name  #假设executable-file-name.c This file exists # #

Here's a demo.c example to compile the demo.c into an executable file:
Input:

[Dabu@dabu.info]$   cc   demo.c-  o Demo

Or

[Dabu@dabu.info]$ make   demo  #假设demo. C exists under the current folder

If you have no errors in the C + + source code, the compiler compiles successfully and creates an executable file called demo in the current directory. Otherwise, the source code has errors, you need to revise and recompile. Enter the following command to confirm that the executable file was generated:

[Dabu@dabu.info]$    ls-l demo*

How (how) to run or execute this executable file called demo on Linux

Enter the following command:

[Dabu@dabu.info]$    ./demo

Or

[Dabu@dabu.info]$   /path/to/demo  #即demo文件的绝对路径

The session process is shown below:

Compile and run a simple C + + program

Create a file name of Demo2 and save the following code in the file;

  #include "iostream"
  //Demo2. C-sample C + + prgoram
  int main (void)
  {
  std::cout << "hello! This is a C + + program.\n ";
  return 0;
  }

Compile demo2.c, enter:

[Dabu@dabu.info]$    g++ Demo2. C-o Demo2

Or

[Dabu@dabu.info]$ make   Demo2

To run the resulting executable file, enter:

[Dabu@dabu.info]$   ./demo2

How (how) to become GDB debug information and warning information?

C use the following syntax;

[Dabu@dabu.info]$   cc-g-wall input.c-o Executable

Explanation:-wall indicates that all warning messages are generated. -G means to generate debugging information

C + + uses the following syntax:

[Dabu@dabu.info]$   g++-g-wall input. C-o Executable

How (how) to get optimized code on Linux?

C uses the following syntax:

[Dabu@dabu.info]$   cc-o input.c-o Executable 

Explanation:-O (Uppercase O) represents optimized code, resulting in less-than-optimized files, which can be performed faster

C + + uses the following syntax:

[Dabu@dabu.info]$   g++-o-wall input. C-o Executable

How (how) to use the C language program of mathematical functions?

To increase the-LM parameter let GCC connect to the Math function library:

[Dabu@dabu.info]$  cc Myth1.c-o EXECUTABLE-LM

Explanation: The-l parameter is used to specify the library to which the program will link, the-l parameter is followed by the library name, such as the math library, his library name is M, and his library filename is libm.so, it is easy to see that the library file name of the first Lib and tail.
How (how) to compile a C + + program that uses Xlib graphics functions?

[Dabu@dabu.info]$  g++ Fireworks. C-o executable-lx11

Explanation: X11 represents Xlib Library
How (how) to compile multiple source files to build an executable program?

Suppose you want to compile light.c at the same time, SKY.C, fireworks.c these 3 C language source files, enter:

[Dabu@dabu.info]$  cc light.c sky.c fireworks.c-o executable

C + + compiles at the same time. C BC. C File3. C 3 source code files, input:

[Dabu@dabu.info]$  g++ AC. C BC. C File3. C-o My-program-name

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.