gcc compilation options - include and - Wall

One option - include

1 eye

Header files can also be included on the gcc command line. A lot of open source software is like this. Sometimes the code "including < xxx.h >" cannot be found in the source code of open source software, and the content of xxx.h is indeed referenced. That's what - include did. Protect xxx.h with - include at gcc compile time.

2. Usage

gcc [srcfile] -include [headfile]

3 actual combat

1 directory structure

[root@localhost 2.14]# vi inc/test.h
[root@localhost 2.14]# tree
.
├── inc
│   └── test.h
└── test
    └── test.cpp

2 test.h content

#define ZWW 9

3 test.cpp content

#include <stdio.h>
// Note: this file does not contain test.h
int main()
{
    bool b = false;
    printf("hello, boy:%d\n",ZWW);
    return 0;
}

4. Operation results

[root@localhost test]# gcc test.cpp -include /root/C++/ch02/2.14/inc/test.h -o test
[root@localhost test]# ./test
hello, boy:9

Two options - Wall

1 eye

Option - Wall displays all warning messages. Warn all to display all warnings.

2 code

#include <stdio.h>
int main()
{
        bool b = false;  //b not used
        int i;
        printf("hello, boy:%d\n",i); //i open without assignment
        return 0;
}

3. Use the option - Wall to compile the alarm, but it can also compile successfully

[root@localhost test]# gcc test.cpp -Wall -o test
test.cpp: In function 'int main()':
test.cpp:5:14: warning: unused variable 'b' [-Wunused-variable]
         bool b = false;
              ^
test.cpp:7:36: warning: 'i' is used uninitialized in this function [-Wuninitialized]
         printf("hello, boy:%d\n",i); 
                                    ^
[root@localhost test]# ll
total 16
-rwxr-xr-x. 1 root root 8520 Mar 10 19:28 test
-rw-r--r--. 1 root root  161 Mar 10 08:07 test.cpp
[root@localhost test]# ./test
hello, boy:0

4. Compile and run without the option - Wall

[root@localhost test]# gcc test.cpp -o test
[root@localhost test]# ./test
hello, boy:0

 

Posted by joma on Sat, 30 Nov 2019 05:01:07 -0800