gcc,g++ common compilation options in Linux
-X language filename
Set the language used by the file, so that the suffix name is invalid for later multiple valid. That is, according to the Convention, the C language suffix name is. c, and the C + + suffix name is. C or. cpp, if you are very personality, decide the suffix name of your C code file is. Pig Haha, then you have to use this parameter, this parameter to the file name behind him will work, unless the next parameter to use. The parameters that can be used are the following:
' C ', ' objective-c ', ' c-header ', ' C + + ', ' cpp-output ', ' assembler ', and ' a
Ssembler-with-cpp '.
It should be understandable to see English.
Example usage: CD.
Gcc-x C Hello.pig
-X None filename
Turn off the previous option, which is to have GCC automatically identify file types based on the filename suffix
Example usage:
Gcc-x c Hello.pig-x None hello2.c
-C
Only activates preprocessing, compilation, and assembly, that is, he only makes the program obj file
Example usage:
Gcc-c hello.c
He will generate the. O obj file
-S
Only pre-processing and compilation are activated, which means compiling the files into assembly code.
Example usage
Gcc-s hello.c
He will generate the assembly code for. S, which you can view with a text editor
-E
Just activate the preprocessing, this does not generate the file, you need to redirect it to an output file inside.
Example usage:
GCC-E hello.c > Pianoapan.txt
GCC-E hello.c | More
Look slowly, a Hello Word will be preprocessed into 800 lines of code
-O
Set the target name, by default, GCC compiled files are a.out, it is difficult to listen, if you and I have the same feeling, to change it, haha
Example usage
Gcc-o hello.exe hello.c (Oh, Windows is used to it)
Gcc-o hello.asm-s hello.c
-pipe
Using pipelines instead of the temporary files in the compilation, there may be some problems when using non-GNU assembler tools
Gcc-pipe-o Hello.exe hello.c
-ansi
Turn off the features in GNU C that are incompatible with ANSI C and activate the proprietary features of ANSI C (including the prohibition of some ASM inline typeof keywords, as well as preprocessing macros such as Unix,vax
/* Infrequently used **************************************************** in comments
-fno-asm
This option implements part of the functionality of the ANSI option, which disables the use of Asm,inline and typeof as keywords.
-fno-strict-prototype
Only works with g++, and with this option, g++ will assume that there are no explicit arguments to the number and type of parameters, not parameters, for functions without parameters.
and GCC, regardless of whether or not this parameter is used, will be to a function without parameters, that there is no explicit description of the type
-fthis-is-varialble
is to be in line with traditional C + +, you can use this when general variables are used.
-fcond-mismatch
Allow the second and third parameter types of a conditional expression to be mismatched, and the value of the expression will be void type
-funsigned-char
-fno-signed-char
-fsigned-char
-fno-unsigned-char
These four parameters are set on the char type and decide to set the char type to unsigned char (the first two parameters
Number) or signed char (the latter two parameters)
* Note Complete *********************************************/
-include file
Contains a code that, simply, makes it easier for a file to be used when another file is needed.
function is equivalent to using #include<filename> in the code
Example usage:
GCC Hello.c-include/root/pianopan.h
-imacros file
Extend the file macro to the gcc/g++ input file, and the macro definition itself does not appear in the input file
-dmacro
Equivalent to # define macro in C language
-dmacro=defn
The equivalent of # define MACRO=DEFN in the C language
-umacro
Equivalent to #undef macro in C language
-undef
To cancel the definition of any non-standard macro
-idir
When you use #i nclude "file", gcc/g++ will first find the header file you made in the current directory, such as
The result is not found, he returns to the default header file directory to find, if using-I developed a directory, he
Go back to the directory you have set up, and then follow the usual sequence to find it.
For #i nclude<file>,gcc/g++ will be made to-I directory lookup, not found, and then to the system's missing
Province's header file directory lookup
-i-
is to cancel the function of the previous parameter, so it is generally used after-idir
-idirafter dir
Look for a failure in the-I directory, refer to this directory to find.
-iprefix Prefix
-iwithprefix dir
Generally used together, when-I directory lookup failed, will be found under Prefix+dir
-nostdinc
Make the compiler no longer the system default header file directory inside the your change file, general and-I joint use, explicitly qualified header
The location of the file
-nostdin C + +
The rule does not search in the standard path specified by g++, but is still searched in other paths. This option is in the Genesis libg++ Library
Use
-C
In the preprocessing, do not delete the comment information, General and e-use, and sometimes analytical procedures, with this very convenient
-M
Generates information about the file association. Contains all the source code that the target file depends on you can use Gcc-m hello.c
To test it, it's simple.
-mm
Same as above, but it ignores dependencies caused by #include<file>.
-md
Same as-M, but the output will be imported into the. d file
-mmd
Same as-mm, but the output will be imported into the. d file.
-wa,option
This option passes option to the assembler; if option has commas in the middle, option is divided into multiple options,
After passing it to the Assembly program
-wl.option
This option passes option to the connector, and if option has commas in the middle, option is divided into multiple options,
After passing it to the connector program.
-llibrary
Create a library to use when compiling
Example usage
Gcc-lcurses hello.c
Compiling a program using the Ncurses Library
-ldir
When compiling, search for the path to the library. such as your own library, you can use it to create a directory, or
The compiler will only look in the catalog of the standard library. This dir is the name of the directory.
-o0
-o1
-o2
-o3
4 levels of the compiler's tuning options,-o0 for no optimizations,-o1 defaults,-o3 optimization level highest
-G
Just the compiler, at compile time, generates debug information.
-gstabs
This option claims debug information in stabs format, but does not include GDB debugging information.
-gstabs+
This option claims debug information in the stabs format and contains additional debugging information that is intended for use by GDB only.
-ggdb
This option will generate the debug information that GDB can use whenever possible.
-static
This option disables the use of dynamic libraries, so the compiled items are generally very large and do not require anything
Dynamic Connection library, you can run it.
-share
This option will use the dynamic library as much as possible, so the resulting file is smaller, but requires the system to be a dynamic library.
-traditional
Trying to get the compiler to support traditional C language features
The following simple application:
We write a simple C program in a text editor (like all books that learn C or C + + will appear)
Code:
# include <stdio.h>
int main ()
{
printf ("hello,world!\n");
return 0;
}
Now save as HELLO.C, open your terminal and enter the file in the current directory:
GCC Hello.c-o Hello
The following warning may appear at compile time: no newline at and of file, it is good to add a new line at the end of the document.
then enter the./hello in the terminal and you will see the results of the program running in the terminal.
The following is how C + + is compiled
Code:
#include <iostream>
using namespace Std;
int main ()
{
cout<< "hello,world!\n" <<endl;
return 0;
}
Save to Hello.cpp
Compile with gcc??? No, here we use g++ to compile C + + programs
g++ Hello.cpp-o Hello
Compile multiple files What do we do??? Look, there's three files out there. Hello.h, Hello.cpp, MyFirst.cpp
Code:
File_no1:hello.h
Class Hello
{
Public
Hello ();
void Display ();
};
File_NO2:Hello.cpp
#include <iostream>
#include "Hello.h"
using namespace Std;
Hello::hello ()
{
}
void Hello::D isplay ()
{
cout<< "hello,world!\n" <<endl;
}
File_NO3:MyFirst.cpp
#include <iostram>
#include "Hello.cpp"
int main ()
{
Hello Thehello;
Thehello.display ();
return 0;
}
In g++, there is a parameter-C can only compile without connection, then we can compile the file in the following order,
Code:
g++-C Hello.cpp-o hello.o
g++-C Myfirst.cpp-o MYFIRST.O
g++ MYFIRST.O Hello.o-o Myfirst
You will ask, if it is a project, there may be hundreds of documents, such a compilation method, the person is not to be exhausted in front of the computer, or until you compile successfully, not the hair is white, hehe, so we have to write the above compilation process into the following text file:
Called Makefile under Linux
#这里可以写一些文件的说明
MYFIRST:MYFIRST.O hello.o
g++ MYFIRST.O Hello.o-o Myfirst
Hello.o:hello.cpp
g++-C Hello.cpp-o hello.o
Myfirst.o:myfirst.cpp
g++-C Myfirst.cpp-o MYFIRST.O
Makefile Writing rules:
(1) Comment on behavior starting with "#"
(2) file dependencies are:
Target:components
Rule
Save As Myfirst, in the terminal input: Make myfist, the program has been wrong but all the programmers common enemy, in writing the program we should try to avoid the error, but when writing, how inevitably there is such a mistake, It's a good idea to do the necessary debugging of the program, so how do we debug the program, see below:
GdB./File name////////////////Here I modify the following to be able to debug, in the compilation above must add parameters g,g++-G hello.cpp-o Hello
The following are the commands you can use in the debug state (you can enter only the word input, such as break to B), and the angle brackets for the description
List < show source code >
Break line number < set breakpoints >
Run < running programs >
Continue < Continue execution from Breakpoint >
Print variables < View values for variables when debugging >
Del Line number < delete breakpoint >
Step < step, trace to function inside >
Next < Single step, non-traceable to function inside >
Quit < exit >
Makefile writing is not an easy task, because the makefile you write may not be common to all Unix/linux-class operating systems. As a result, automake.autoconf or CMake tools are used in many projects.
From: http://xiaoou2002.blog.163.com/blog/static/2158666920098721228307/
GCC g++ compilation Options Makefile