温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

1 C语言 gcc 介绍 C 语言编译 main接受参数

发布时间:2020-07-19 20:18:28 来源:网络 阅读:1412 作者:990487026 栏目:开发技术



1         第一个c语言的hello world

1.1      include头文件包含

头文件包含,写法#include<文件名>,

1.2      main函数

这个就是C语言程序的入口,所有的C程序都是从main开始执行,一个C的源程序必须有一个main函数,也只能有一个main函数

1.3      注释

//注释一行

/* */代表块注释,可以注释多行代码

 

1.4      {}括号和代码块

代表一个代码单元

1.5      声明

C语言规定,所有的变量和函数必须先声明,然后才能使用.

1.6      C语言自定义名字的要求

可以使用大小写字母,下划线,数字,但第一个字母必须是字母或者下划线

字母区分大小写

变量名最好用英文,而且要有所含义,通过变量的名称就能猜测变量的意思。

 

1.7      return语句

C语言当中任何函数遇到return代表这个函数停止,当main函数遇到return,代表整个程序退出

return代表函数的返回值,如果返回类型是void,可以直接写return,而不需要返回任何值

2         C语言的编译

2.1      编译过程

1 C语言 gcc 介绍 C 语言编译 main接受参数


2.2      gcc编译选项

-o代表指定输出文件名

-E代表预编译

预编译处理include的本质就是简单的将include中的文件替换到c文件中

如果include包含的头文件在系统目录下,那么就用#include <>,如果包含的文件在当前目录下,那么用#inlclude“”

-S代表汇编

-c代表编译

 

 

 

Linux C 学习

 

1,编写一个helloworld 程序

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


 

一步到位编译执行

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

第一步:预编译,把include文件的内容原封不动的放到源代码中   gcc -o hello.i  hello.c

第二步:汇编,把预编译的结果变成汇编代码

第三步:编译,把汇编的结果变成二进制文件

第四步: 链接,把编译的二进制文件与系统库连接起来

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)


    

 

    

调用系统的程序

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

执行 ./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执行原理

1 C语言 gcc 介绍 C 语言编译 main接受参数


向屏幕输出的其他方式:

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("请输入两个整数!\n");    else    {       int a = atoi(args[1]);       int b = atoi(args[2]);       int c = a+b;       printf("两个数的和是 %d \n",c);    }   return 0; }


 

使用方法

chunli@pc0003:/tmp/C$ !gcc gcc calc.c  chunli@pc0003:/tmp/C$ ./a.out  3 3 两个数的和是 6  chunli@pc0003:/t


 


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI