国产宅男网站在线|亚洲A级性爱免费视频|亚洲中精品级在线|午夜福利AA毛

  • <dd id="gf5jf"><th id="gf5jf"></th></dd>

    <cite id="gf5jf"><label id="gf5jf"></label></cite>
  • <div id="gf5jf"><listing id="gf5jf"></listing></div>
    學(xué)習(xí)啦>學(xué)習(xí)電腦>操作系統(tǒng)>Linux教程>

    linux編譯c語言的命令

    時(shí)間: 佳洲1085 分享

      GCC是一個(gè)源代碼編譯工具,有了它我們可以編譯不同語言的程序。下面由學(xué)習(xí)啦小編為大家整理了linux編譯C語言的命令的相關(guān)知識(shí),希望大家喜歡!

      linux下編譯C語言的方法

      在任何linux分支下編譯C/C++代碼,如 Ubuntu ,Red Hat, Fedora ,Debian 以及其他linux分支上,我們需要安裝一下軟件包:

      1.GNU C and C++ compiler collection

      2.Development tools

      3.Development libraries

      4.IDE or text editor to write programs

      第一步:安裝 C/C++ 編譯器和相關(guān)工具包

      如果你是使用Fedora, Red Hat, CentOS, or Scientific Linux,可以使用yum命令快速安裝GNU c/c++ 編譯器:

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

      如果你是使用 Debian , Ubuntu Linux ,則輸入apt-get命令來安裝裝GNU c/c++ 編譯器;

      [dabu@dabu.info ]$ sudo apt-get update

      [dabu@dabu.info ]$ sudo apt-get install build-essential manpages-dev

      第二步:確認(rèn)是否安裝成功

      輸入以下命令,顯示編譯器版本和安裝的文件夾:

      [dabu@dabu.info ]$ whereis gcc

      [dabu@dabu.info ]$ which gcc

      [dabu@dabu.info ]$ gcc --version

      第三步、如何(怎樣)在linux中編譯C/C++代碼

      創(chuàng)建一個(gè)demo.c文件,使用vi ,emacs 或者 joe 將下列c源代碼輸入進(jìn)去:

      #include

      /* demo.c: My first C program on a Linux */

      int main(void)

      {

      printf("Hello! This is a test prgoram.\n");

      return 0;

      }

      接著進(jìn)行編譯:

      編譯的使用語法如下:

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

      或者

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

      解釋:program-source-code.c是C源代碼,executable-file-name是編譯后得到的可執(zhí)行文件

      又或者

      [dabu@dabu.info ]$ make executable-file-name #假設(shè)executable-file-name.c 這個(gè)文件存在 ##

      下面以demo.c舉例來將demo.c編譯成可執(zhí)行文件:

      輸入:

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

      或者

      [dabu@dabu.info ]$ make demo #假設(shè)demo.c在當(dāng)前文件夾下存在

      如果你的C/C++源代碼沒有錯(cuò)誤,編譯器就會(huì)編譯成功同時(shí)在當(dāng)前目錄下創(chuàng)建一個(gè)叫做demo的可執(zhí)行文件。否則,源代碼有錯(cuò)誤,你需要修正后重新編譯。輸入下面的命令來確認(rèn)生成了可執(zhí)行文件:

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

      如何(怎樣)在linux上運(yùn)行或者執(zhí)行這個(gè)叫做demo的可執(zhí)行文件

      輸入以下命令:

      [dabu@dabu.info ]$ ./demo

      或者

      [dabu@dabu.info ]$ /path/to/demo #即demo文件的絕對(duì)路徑

      補(bǔ)充:linux編譯運(yùn)行C++語言的方法

      創(chuàng)建一個(gè)文件名為demo2 ,將下面的代碼保存到該文件中;

      #include "iostream"

      // demo2.C - Sample C++ prgoram

      int main(void)

      {

      std::cout << "Hello! This is a C++ program.\n";

      return 0;

      }

      編譯demo2.c ,輸入:

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

      或者

      [dabu@dabu.info ]$ make demo2

      運(yùn)行得到的可執(zhí)行文件,輸入:

      [dabu@dabu.info ]$ ./demo2

      如何(怎樣)生成為GDB調(diào)試信息和警告信息?

      C 使用以下語法;

      [dabu@dabu.info ]$ cc -g -Wall input.c -o executable

      解釋:-Wall表示生成所有警告信息。-g表示生成調(diào)試信息

      C++使用以下語法:

      [dabu@dabu.info ]$ g++ -g -Wall input.C -o executable

      如何(怎樣)在linux上得到優(yōu)化代碼?

      C 使用以下語法:

      [dabu@dabu.info ]$ cc -O input.c -o executable

      解釋: -O(大寫的O)表示優(yōu)化代碼,編譯后得到的文件比沒優(yōu)化的小些,執(zhí)行可能更快

      C++使用以下語法:

      [dabu@dabu.info ]$ g++ -O -Wall input.C -o executable

      如何(怎樣)使用數(shù)學(xué)函數(shù)的C語言程序呢?

      要增加 -lm參數(shù)讓gcc與數(shù)學(xué)函數(shù)庫連接:

      [dabu@dabu.info ]$ cc myth1.c -o executable -lm

      解釋:-l參數(shù)就是用來指定程序要鏈接的庫,-l參數(shù)緊接著就是庫名,比如數(shù)學(xué)庫,他的庫名是m,他的庫文件名是libm.so,很容易看出,把庫文件名的頭lib和尾.so去掉就是庫名。

      如何(怎樣)編譯一個(gè)使用Xlib 圖形函數(shù)的C++程序?

      [dabu@dabu.info ]$ g++ fireworks.C -o executable -lX11

      解釋:X11表示Xlib庫

      如何(怎樣)編譯多個(gè)源文件生成可執(zhí)行程序 ?

      假設(shè)要同時(shí)編譯light.c, sky.c, fireworks.c 這3個(gè)C語言源文件,輸入:

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

      C++同時(shí)編譯c.C bc.C file3.C 3個(gè)源代碼文件,輸入:

      [dabu@dabu.info ]$ g++ ac.C bc.C file3.C -o my-program-name

    3610396