《(5.4.1)--5.4TheUsageofgcc.ppt》由会员分享,可在线阅读,更多相关《(5.4.1)--5.4TheUsageofgcc.ppt(13页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、TheUsageofgccTheUsageofgccgcc v check the version number of gccgcc f1.c o f2(gcc f1.c)Example:#include int main()printf(hello worldn);return 0;The usage of gcchello.cCompile the program of multiple source files by gcc/mul1.c#include#include“my.h”main()printf(hello worldn);f();/mul2.c#include f()prin
2、tf(“in f()n”);/my.hvoid f();laidell lai gcc mul1.c mul2.c o m.outmul1.c/mul2.c/my.hCompile the program of multiple source files by gcc/mul1.c#include#include“my.h”main()printf(hello worldn);f();/mul2.c#include f()printf(“in f()n”);/my.hvoid f();laidell lai gcc mul1.c mul2.c o m.outThe defect of taki
3、ng gcc to compile the program of multiple source files:Every file must be recompiled.gcc contains complete functions of error checking and warning prompt to help programmers find error codes as soon as possible.gcc includes more than 30 warnings and three warning levels.Warning prompt of gccA bad ex
4、ampleThe main function doesnt use the return statement to return an int type value.Variable var is defined but not used.#include void main(void)long long int var=3;printf(this is not a standard c program!n);bad.cgcc-Wallbad.cWarning options of gccoptioinsoptioinsfunctionsfunctions-Wcomment-WcommentW
5、arnWarningingifcommentnestingoccurs(/*appearsafter/*)ifcommentnestingoccurs(/*appearsafter/*).-Wformat-WformatWarnWarningingiftheiftheparameterparameterpassedtopassedto printfdoesnprintfdoesn tmatchthespecifiedformattmatchthespecifiedformat.-Wmain-WmainWarnWarningingifthereturnifthereturntypetype of
6、mainisnotofmainisnotintintortheparametersareincorrectwhencallingmainortheparametersareincorrectwhencallingmain.-Wparentheses-WparenthesesBasedonthecontextinference,Basedonthecontextinference,warningwarningifif(n=10)n=10)iswritteniswrittenasas(n=10)(n=10).-Wswitch-WswitchWarnWarningingifoneormorecase
7、branchesaremissingfromtheswitch(onlyapplicabletoenum)ifoneormorecasebranchesaremissingfromtheswitch(onlyapplicabletoenum).-Wunused-WunusedWarningiftWarningifthevariableisdeclaredbutnotused,orthestatictypefunctionisnotcalledhevariableisdeclaredbutnotused,orthestatictypefunctionisnotcalled.-Wuninitial
8、ized-WuninitializedWarningiftheautomaticvariableusedisnotinitializedWarningiftheautomaticvariableusedisnotinitialized.-Wundef-WundefWarnWarningingifundefinedvariablesareusedinthe#ififundefinedvariablesareusedinthe#ifmacromacrofordecision.fordecision.-Winline-WinlineWarningifthefunctioncannotbeinline
9、dWarningifthefunctioncannotbeinlined.-Wmissing-declarations-Wmissing-declarationsWarnWarningingifaglobalfunctionisdefinedbutnotdeclaredinanyheaderfileifaglobalfunctionisdefinedbutnotdeclaredinanyheaderfile.-Wlong-long-Wlong-longWarningiflonglongtypeisusedWarningiflonglongtypeisused.-Werror-WerrorTur
10、nallwarningsintoerrorsTurnallwarningsintoerrors.Function library:header file(.h)+library file(.so,.a)The default paths that can be identified by the system:header file:/usr/includelibrary file:/usr/libAccessing a file in a non-default path must specify the path while compiling.header file-Ilibrary f
11、ile-LThe library dependency of gccgcc k.c I /home/lai/include o kgcc k.c L /home/lai/lib lkk o kAccessing a file in a non-default path must specify the path while compiling.header file-Ilibrary file-L,the parameter after-l indicates that the library libkk.so is to be found.The library dependency of
12、gccCode optimization refers to that the compiler can find out the part which is not fully optimized by analyzing the source code and recombine the code to improve the execution performance of the program.gcc implements code optimization by option-On.-O,-O1:they refer to reducing the length and execu
13、tion time of code at the same time.-O2:it contains all the functions of-O and can perform the work of instruction scheduling.-O3:it contains all the functions of-O2 and can perform some optimization work of loop unrolling.Optimization options of gccA code optimization example#includeintmain()doublec
14、ounter;doubleresult;doubletemp;for(counter=0;for(counter=0;counter400counter4000 0.0*4000.0*4000.0/20.0+2030;.0*4000.0*4000.0/20.0+2030;counter+=(5-3+2+1)/4counter+=(5-3+2+1)/4)temp=counter/1239;temp=counter/1239;result=counter;result=counter;printf(theresultis%1fn,result);return0;optimeze.cUse the following options to compile the program.Execute the program,find its execution time,and observe the difference.time./a.outExample:optimize.cgcc optimize.c gcc O optimize.c EmbeddedSoftwareDesignEmbeddedSoftwareDesign