guides:software:gcc:start
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| guides:software:gcc:start [2020/07/19 12:55] – wikiadmin | guides:software:gcc:start [2022/08/02 11:59] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 147: | Line 147: | ||
| </ | </ | ||
| + | g++ supports many flags. | ||
| + | * Language specific flags | ||
| + | * Warning flags | ||
| + | * Debugging flags | ||
| + | * Optimization flags | ||
| + | * Linking flags | ||
| + | * Information flags | ||
| + | * Many others | ||
| + | On most linux systems //man g++// will show documentation for the flags supported by your version of g++. | ||
| + | |||
| + | ==== Some Useful Arguments ==== | ||
| + | |||
| + | * //- -version// will display the version of g++ you are using. | ||
| + | * < | ||
| + | $ g++ --version | ||
| + | g++ (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2) | ||
| + | Copyright (C) 2019 Free Software Foundation, Inc. | ||
| + | This is free software; see the source for copying conditions. | ||
| + | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| + | |||
| + | </ | ||
| + | * In general, you want your version of g++ to be as close to the version of g++ on the homework standard machine specified by your instructor. | ||
| + | * If your version is not the same, make sure that you compile your code on the homework standard machine specified by your instructor **before** you submit any code. | ||
| + | * //-std// controls the version of the c++ language the compiler will use. | ||
| + | * There have been multiple c++ standards over the years, generally named after the year the standard was adopted. | ||
| + | * // | ||
| + | * // | ||
| + | * // | ||
| + | * // | ||
| + | * Consider the following code | ||
| + | * <code c++> | ||
| + | #include < | ||
| + | |||
| + | using namespace std; | ||
| + | |||
| + | int main() { | ||
| + | string greeting=" | ||
| + | |||
| + | for(auto & letter: greeting) { | ||
| + | cout << letter; | ||
| + | } | ||
| + | |||
| + | cout << endl; | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | * It will not compile under c++98 | ||
| + | * < | ||
| + | $ g++ -std=c++98 auto.cpp | ||
| + | auto.cpp: In function 'int main()': | ||
| + | auto.cpp: | ||
| + | 8 | | ||
| + | | ^~~~~~ | ||
| + | auto.cpp: | ||
| + | 8 | | ||
| + | | ^~~~~~~~ | ||
| + | auto.cpp: | ||
| + | </ | ||
| + | * But it will compile under c++14 | ||
| + | * < | ||
| + | $ g++ -std=c++14 auto.cpp | ||
| + | </ | ||
| + | * There are **many** other standards supported. | ||
| + | * You should check with your instructor regarding which version of the compiler you should use. | ||
| + | |||
| ==== Producing Additional Warnings ==== | ==== Producing Additional Warnings ==== | ||
| - | ==== Other Arguments ==== | + | Compilers are also capable of analyzing source code to predict where this code may cause run time errors. |
| - | ===== Multiple File Compilation | + | Your instructor may require one or more of the following flags when compiling code: |
| + | |||
| + | * // | ||
| + | * This requests the compiler to follow the ISO C++ standard, rejecting forbidden and non-standard extensions | ||
| + | * //-Wall// | ||
| + | * Enables MANY different warnings for constructs that are considered questionable. | ||
| + | * //-Wextra// | ||
| + | * Enables many additional warnings | ||
| + | * // | ||
| + | * Warn if the indentation of the code does not reflect the block structure. | ||
| + | * // | ||
| + | * Warn if different declared items are not used | ||
| + | * // | ||
| + | * Warn about uninitialized variables. | ||
| + | * This flag requires //-O**n**// for some variables. | ||
| + | * See below. | ||
| + | * // | ||
| + | * Warn when shadow variables are declared in a scope | ||
| + | * // | ||
| + | * Warn when an implicit conversion may change a value. | ||
| + | |||
| + | Note: There are **many** other warning producing flags. | ||
| + | |||
| + | For the following code: | ||
| + | <code c++> | ||
| + | #include < | ||
| + | |||
| + | using namespace std; | ||
| + | |||
| + | int main() { | ||
| + | int a,b; | ||
| + | int c; | ||
| + | |||
| + | if (a = b) | ||
| + | cout << " a is 4" << endl; | ||
| + | | ||
| + | |||
| + | return 3.14; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Compiling without additional flags produces no warnings. | ||
| + | < | ||
| + | $ g++ bad.cpp | ||
| + | $ g++ -g -O3 -Wpedantic -Wall -Wextra -Wmisleading-indentation -Wunused -Wuninitialized -Wshadow -Wconversion bad.cpp | ||
| + | bad.cpp: In function 'int main()': | ||
| + | bad.cpp: | ||
| + | 10 | if (a = b) | ||
| + | | | ||
| + | bad.cpp: | ||
| + | 10 | if (a = b) | ||
| + | | ^~ | ||
| + | bad.cpp: | ||
| + | 12 | a = 5; | ||
| + | | ^ | ||
| + | bad.cpp: | ||
| + | 14 | | ||
| + | | ^~~~ | ||
| + | bad.cpp: | ||
| + | 8 | int c; | ||
| + | | ^ | ||
| + | bad.cpp: | ||
| + | 10 | if (a = b) | ||
| + | | | ||
| + | </ | ||
| + | |||
| + | You should use all command line flags specified by your instructor. | ||
| + | |||
| + | |||
| + | Note: If you wish to avoid typing command line arguments, you may wish to investigate | ||
| + | * Makefiles | ||
| + | * Command Line History | ||
| + | ==== Other Arguments | ||
| + | Other flags are useful in different situations. | ||
| + | * //-g// tells the compiler to retain debugging information. | ||
| + | * This is probably a good option to have enabled. | ||
| + | * //-O**n**// tells the compiler to optimize the code. | ||
| + | * This may result in a more efficient executable. | ||
| + | * **n** is the level of optimization | ||
| + | * This is currently an integer between 1 and 3. | ||
| + | * Compiling with higher values of n may take longer, but will generally result in a faster executable. | ||
guides/software/gcc/start.1595163330.txt.gz · Last modified: 2022/08/02 11:59 (external edit)