User Tools

Site Tools


guides:software:gcc:start

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
guides:software:gcc:start [2020/07/19 13:46] – [Some Useful Arguments] wikiadminguides:software:gcc:start [2022/08/02 11:59] (current) – external edit 127.0.0.1
Line 160: Line 160:
 ==== Some Useful Arguments ==== ==== Some Useful Arguments ====
  
-  * //--version// will display the version of g++ you are using.+  * //- -version// will display the version of g++ you are using.
     * <code>     * <code>
 $ g++ --version $ g++ --version
Line 216: Line 216:
 ==== 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.    By turning on these warnings you are more likely to write code that conforms to the standard and less likely to have common runtime errors in  your code.
  
 +Your instructor may require one or more of the following flags when compiling code:
  
-===== Multiple File Compilation ======+  * //-Wpedantic// 
 +    * 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 
 +  * //-Wmisleading-indentation// 
 +    * Warn if the indentation of the code does not reflect the block structure. 
 +  * //-Wunused//  
 +    * Warn if different declared items are not used 
 +  * //-Wuninitialized// 
 +    * Warn about uninitialized variables. 
 +    * This flag requires //-O**n**// for some variables. 
 +      * See below. 
 +  * //-Wshadow// 
 +    * Warn when shadow variables are declared in a scope 
 +  * //-Wconversion//  
 +    * Warn when an implicit conversion may change a value. 
 + 
 +Note:  There are **many** other warning producing flags. 
 + 
 +For the following code: 
 +<code c++> 
 +#include <iostream> 
 + 
 +using namespace std; 
 + 
 +int main() { 
 +    int a,b; 
 +    int c; 
 + 
 +    if (a b) 
 +       cout << " a is 4" << endl; 
 +       5; 
 + 
 +    return 3.14; 
 +
 +</code> 
 + 
 +Compiling without additional flags produces no warnings.  But compiling with the listed flags produces 
 +<code> 
 +$ 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:11: warning: suggest parentheses around assignment used as truth value [-Wparentheses] 
 +   10 |     if (a b) 
 +      |         ~~^~~ 
 +bad.cpp:10:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation] 
 +   10 |     if (a b) 
 +      |     ^~ 
 +bad.cpp:12:8: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if' 
 +   12 |        a 5; 
 +      |        ^ 
 +bad.cpp:14:12: warning: conversion from 'double' to 'int' changes value from '3.1400000000000001e+0' to '3' [-Wfloat-conversion] 
 +   14 |     return 3.14; 
 +      |            ^~~~ 
 +bad.cpp:8:9: warning: unused variable 'c' [-Wunused-variable] 
 +    8 |     int c; 
 +      |         ^ 
 +bad.cpp:10:11: warning: 'b' is used uninitialized in this function [-Wuninitialized] 
 +   10 |     if (a b) 
 +      |         ~~^~~ 
 +</code> 
 + 
 +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 useful when using a //symbolic debugger// such as //gdb//.
 +      * 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.1595166396.txt.gz · Last modified: 2022/08/02 11:59 (external edit)