溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

1 C語言 gcc 介紹 C 語言編譯 main接受參數(shù)

發(fā)布時間:2020-07-19 20:18:28 來源:網(wǎng)絡(luò) 閱讀:1382 作者:990487026 欄目:開發(fā)技術(shù)



1         第一個c語言的hello world

1.1      include頭文件包含

頭文件包含,寫法#include<文件名>,

1.2      main函數(shù)

這個就是C語言程序的入口,所有的C程序都是從main開始執(zhí)行,一個C的源程序必須有一個main函數(shù),也只能有一個main函數(shù)

1.3      注釋

//注釋一行

/* */代表塊注釋,可以注釋多行代碼

 

1.4      {}括號和代碼塊

代表一個代碼單元

1.5      聲明

C語言規(guī)定,所有的變量和函數(shù)必須先聲明,然后才能使用.

1.6      C語言自定義名字的要求

可以使用大小寫字母,下劃線,數(shù)字,但第一個字母必須是字母或者下劃線

字母區(qū)分大小寫

變量名最好用英文,而且要有所含義,通過變量的名稱就能猜測變量的意思。

 

1.7      return語句

C語言當中任何函數(shù)遇到return代表這個函數(shù)停止,當main函數(shù)遇到return,代表整個程序退出

return代表函數(shù)的返回值,如果返回類型是void,可以直接寫return,而不需要返回任何值

2         C語言的編譯

2.1      編譯過程

1 C語言  gcc 介紹 C 語言編譯 main接受參數(shù)


2.2      gcc編譯選項

-o代表指定輸出文件名

-E代表預(yù)編譯

預(yù)編譯處理include的本質(zhì)就是簡單的將include中的文件替換到c文件中

如果include包含的頭文件在系統(tǒng)目錄下,那么就用#include <>,如果包含的文件在當前目錄下,那么用#inlclude“”

-S代表匯編

-c代表編譯

 

 

 

Linux C 學(xué)習(xí)

 

1,編寫一個helloworld 程序

vim  hello.c 
#include "stdio.h"
int main(){
printf("Hello World!\n\n");
return 0;
}


 

一步到位編譯執(zhí)行

chunli@pc0003:/tmp/C$ gcc helloworld.C
chunli@pc0003:/tmp/C$ ./a.out 
Hello World!


 

 

 

 

 

C編譯過程

chunli@pc0003:/tmp/C$ gcc --help
Usage: gcc [options] file...
Options:
 -pass-exit-codes         Exit withhighest error code from a phase
  --help                   Display this information
  --target-help            Display target specific commandline options
 --help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...]
                          Display specific types of command line options
  (Use '-v --help' todisplay command line options of sub-processes)
  --version                Display compiler version information
  -dumpspecs               Display all of the built in specstrings
  -dumpversion             Display the version of thecompiler
  -dumpmachine             Display the compiler's targetprocessor
  -print-search-dirs       Display the directories in thecompiler's search path
 -print-libgcc-file-name  Displaythe name of the compiler's companion library
 -print-file-name=<lib>  Display the full path to library <lib>
 -print-prog-name=<prog> Display the full path to compiler component <prog>
 -print-multiarch         Displaythe target's normalized GNU triplet, used as
                          a component in the library path
 -print-multi-directory   Displaythe root directory for versions of libgcc
  -print-multi-lib         Display the mapping between commandline options and
                          multiple library search directories
 -print-multi-os-directory Display the relative path to OS libraries
  -print-sysroot           Display the target librariesdirectory
 -print-sysroot-headers-suffix Display the sysroot suffix used to findheaders
 -Wa,<options>           Pass comma-separated <options> on to the assembler
 -Wp,<options>           Pass comma-separated <options> on to the preprocessor
 -Wl,<options>           Pass comma-separated <options> on to the linker
  -Xassembler<arg>        Pass <arg> on tothe assembler
  -Xpreprocessor<arg>     Pass <arg> on tothe preprocessor
  -Xlinker<arg>           Pass <arg> onto the linker
  -save-temps              Do not delete intermediate files
 -save-temps=<arg>        Donot delete intermediate files
 -no-canonical-prefixes   Do notcanonicalize paths when building relative
                          prefixes to other gcc components
  -pipe                    Use pipes rather thanintermediate files
  -time                    Time the execution of eachsubprocess
 -specs=<file>           Override built-in specs with the contents of <file>
 -std=<standard>         Assume that the input sources are for <standard>
 --sysroot=<directory>    Use<directory> as the root directory for headers
                          and libraries
  -B<directory>           Add<directory> to the compiler's search paths
  -v                       Display the programsinvoked by the compiler
  -###                     Like -v but options quotedand commands not executed
  -E                       Preprocess only; do notcompile, assemble or link
  -S                       Compile only; do notassemble or link
  -c                       Compile and assemble,but do not link
  -o <file>                Place the output into<file>
  -pie                     Create a positionindependent executable
  -shared                  Create a shared library
  -x <language>            Specify the language of thefollowing input files
                          Permissible languages include: c c++ assembler none
                          'none' means revert to the default behavior of
                          guessing the language based on the file's extension
 
Options starting with -g, -f, -m, -O, -W, or --param areautomatically
 passed on to thevarious sub-processes invoked by gcc.  Inorder to pass
 other options on tothese processes the -W<letter> options must be used.
 
For bug reporting instructions, please see:
<file:///usr/share/doc/gcc-4.8/README.Bugs>.


 

 

 

 

C代碼程序 hello.c

第一步:預(yù)編譯,把include文件的內(nèi)容原封不動的放到源代碼中   gcc -o hello.i  hello.c

第二步:匯編,把預(yù)編譯的結(jié)果變成匯編代碼

第三步:編譯,把匯編的結(jié)果變成二進制文件

第四步: 鏈接,把編譯的二進制文件與系統(tǒng)庫連接起來

chunli@pc0003:/tmp/C$ gcc -o hello.i -E hello.c 
chunli@pc0003:/tmp/C$ gcc -o hello.s -S hello.c
chunli@pc0003:/tmp/C$ gcc -o hello.o -c hello.s 
chunli@pc0003:/tmp/C$ gcc -o hello      hello.o
chunli@pc0003:/tmp/C$ ./hello 
Hello World!


 

查看鏈接的庫

chunli@pc0003:/tmp/C$ ldd hello
     linux-vdso.so.1=>  (0x00007fff217f8000)
     libc.so.6 =>/lib/x86_64-linux-gnu/libc.so.6 (0x00007f5340914000)
     /lib64/ld-linux-x86-64.so.2(0x00005654d9706000)


    

 

    

調(diào)用系統(tǒng)的程序

vim hello.c
#include "stdio.h"
#include "stdlib.h"
int main(){
system("cat hello.c");
printf("Hello World!\n\n");
return 0;
}


 

編譯 gcc hello.c

執(zhí)行 ./a.out

輸出:

#include "stdio.h"
#include "stdlib.h"
int main(){
system("cat hello.c");
printf("Hello World!\n\n");
return 0;
}
Hello World!


 

 

 

    

    

 

2.3  printf執(zhí)行原理

1 C語言  gcc 介紹 C 語言編譯 main接受參數(shù)


向屏幕輸出的其他方式:

chunli@pc0003:/tmp/C$ cat my_printf.c 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int i = 99;
printf("i=%d \n",i/0x10);
fwrite("abc\n",1,2,stdout);
//write("abc\n",4,STDOUT_FILENO,"abc");
return ;
}


 


 

 

 

C 語言版計算器

#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *args[])
{
   if(argc <3 )
       printf("請輸入兩個整數(shù)!\n");
   else
   {
      int a = atoi(args[1]);
      int b = atoi(args[2]);
      int c = a+b;
      printf("兩個數(shù)的和是 %d \n",c);
   }
 
return 0;
}


 

使用方法

chunli@pc0003:/tmp/C$ !gcc
gcc calc.c 
chunli@pc0003:/tmp/C$ ./a.out  3 3
兩個數(shù)的和是 6 
chunli@pc0003:/t


 


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI