search
HomeSystem TutorialLINUXGCC Commands: Unlocking the Endless Possibilities of Linux Programming
GCC Commands: Unlocking the Endless Possibilities of Linux ProgrammingFeb 10, 2024 pm 06:54 PM
linuxlinux tutoriallinux systemlinux commandshell scriptoverflowembeddedlinuxGetting started with linuxlinux learning

As a popular open source compiler, GCC (GNU Compiler Collection) has become one of the standard features for Linux software development. If you want to enter the world of Linux programming, mastering GCC commands is essential. Whether you are a beginner or a professional developer, GCC opens up endless possibilities for you.

The gcc compiler provides an almost endless list of command line options. Of course, no one will ever use or be proficient in all its command-line options, but there are some command-line options that every gcc user should know - if not be required to know. Some of them are commonly used and others are less commonly used, but just because they are less commonly used does not mean that they are less useful than the former.

In this series of articles, we focus on some uncommon but useful gcc command line options. Several such command line options have been mentioned in the first section.

I don’t know if you can recall that at the beginning of the first part of this series of tutorials, I briefly mentioned the -Wall option that developers usually use to generate warnings, and does not include some special warnings. If you don’t know about these special warnings and don’t know how to generate them, don’t worry, I will explain all the details about them in this article.

In addition to this, this article will also cover gcc warning options related to floating point values, and how to better manage the gcc command line option list when it becomes large.

Before continuing, please remember that all examples, commands and instructions in this tutorial have been tested on Ubuntu 16.04 LTS operating system and gcc 5.4.0.

GCC Commands: Unlocking the Endless Possibilities of Linux Programming

Generate warnings when -Wall option is not included

Although the -Wall option of the gcc compiler covers most warning flags, there are still some warnings that cannot be generated. To generate them, use the -Wextra option.

For example, the following code:

#include 
#include 
int main()
{
    int i=0;
    /* ...
       some code here 
       ...
    */

    if(i);
        return 1;
     return 0; 
}

I accidentally put an extra semicolon after the if condition. Now, if you compile using the following gcc command, no warnings will be generated.
gcc -Wall test.c -o test

But if you use the -Wextra option at the same time to compile: gcc -Wall -Wextra test.c -o test
A warning like the following will be generated:

test.c: In function ‘main’:
test.c:10:8: warning: suggest braces around empty body in an ‘if’ statement
[-Wempty-body] if(i);

It is clear from the above warning that the -Wextra option enables the -Wempty-body option from the inside, so that suspicious code can be detected and warnings generated. Below are all the warning flags enabled by this option.

-Wclobbered
-Wempty-body
-Wignored-qualifiers
-Wmissing-field-initializers
-Wmissing-parameter-type(仅针对 C 语言)
-Wold-style-declaration(仅针对 C 语言)
-Woverride-init
-Wsign-compare
-Wtype-limits
-Wuninitialized
-Wunused-parameter(只有和-Wunused 或 -Wall 选项使用时才会启用)
-Wunused-but-set-parameter (只有和-Wunused或-Wall 选项使用时才会生成)

If you want to know more about the tags mentioned above, please check the gcc manual.

In addition, the -Wextra option will also generate warnings when encountering the following situations:

一个指针和整数 0 进行 , 或 >= 比较
(仅 C++)一个枚举类型和一个非枚举类型同时出现在一个条件表达式中
(仅 C++)有歧义的虚拟基底
(仅 C++)寄存器类型的数组加下标
(仅 C++)对寄存器类型的变量进行取址
(仅 C++)基类没有在派生类的复制构建函数中进行初始化

Generate a warning when comparing floating point values ​​for equality

You may already know that floating point values ​​cannot be compared for exact equality (if not, please read the FAQ related to floating point value comparison). But if you accidentally do this, will the gcc compiler issue an error or warning? Let’s test it out:

The following is a piece of code that uses the == operator to compare floating point values:

#include

void compare(float x, float y)
{
    if(x == y)
    {
        printf("/n EQUAL /n");
    }
}

int main(void)
{
    compare(1.234, 1.56789);

    return 0; 
}

Use the following gcc command (including the -Wall and -Wextra options) to compile this code:

gcc -Wall -Wextra test.c -o test

Unfortunately, the above command does not generate any warnings related to floating point value comparisons. A quick look at the gcc manual shows that there is a dedicated -Wfloat-equal option that can be used in this situation.
Here is the command with this option:

gcc -Wall -Wextra -Wfloat-equal test.c -o test

The following is the output produced by this command:

test.c: In function ‘compare’:
test.c:5:10: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]
 if(x == y)

As you can see in the output above, the -Wfloat-equal option forces the gcc compiler to generate a warning related to comparisons of floating point values.

Here is the gcc manual description of this option:

The idea behind this is that sometimes it is convenient for programmers to think of floating point values ​​as approximately infinitely precise real numbers. If you do this, then you need to figure out, by profiling the code, or otherwise, the maximum or possible maximum error introduced by this calculation, and then allow for this when doing the comparison (and when producing the output, but that's a different question) error. In particular, you should not check for equality, but you should check whether the two values ​​may have overlapping ranges; this is done using relational operators, so the equality comparison may be a mistake.

How to better manage gcc command line options

如果在你使用的 gcc 命令中,命令行选项列表变得很大而且很难管理,那么你可以把它放在一个文本文件中,然后把文件名作为 gcc 命令的一个参数。之后,你必须使用@file 命令行选项。

比如,下面这行是你的 gcc 命令:
gcc -Wall -Wextra -Wfloat-equal test.c -o test

然后你可以把这三个和警告相关的选项放到一个文件里,文件名叫做 gcc-options:
$ cat gcc-options
-Wall -Wextra -Wfloat-equal
这样,你的 gcc 命令会变得更加简洁并且易于管理:
gcc @gcc-options test.c -o test

下面是 gcc 手册关于 @file 的说明:

从文件中读取命令行选项。读取到的选项随之被插入到原始 @file 选项所在的位置。如果文件不存在或者无法读取,那么这个选项就会被当成文字处理,而不会被删除。

文件中的选项以空格分隔。选项中包含空白字符的话,可以用一个由单引号或双引号包围完整选项。任何字符(包括反斜杠: ‘/’)均可能通过一个 ‘/’ 前缀而包含在一个选项中。如果该文件本身包含额外的 @file选项,那么它将会被递归处理。

结论

在这个系列的教程中,我们一共讲解了 5 个不常见但是很有用的 gcc 命令行选项:

-Save-temps
-g
-Wextra
-Wfloat-equal

@file

记得花时间练习使用每一个选项,同时不要忘了浏览 gcc 手册上面所提供的关于它们的全部细节。

因此,无论你是正在学习Linux编程,还是已经成为一名经验丰富的开发者,掌握GCC命令将会让你事半功倍。尝试使用GCC命令去编译各种不同的程序,挑战自己的极限,让你的Linux编程之路越来越精彩

The above is the detailed content of GCC Commands: Unlocking the Endless Possibilities of Linux Programming. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
什么是linux设备节点什么是linux设备节点Apr 18, 2022 pm 08:10 PM

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

Linux中open和fopen的区别有哪些Linux中open和fopen的区别有哪些Apr 29, 2022 pm 06:57 PM

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

linux中什么叫端口映射linux中什么叫端口映射May 09, 2022 pm 01:49 PM

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

什么是linux交叉编译什么是linux交叉编译Apr 29, 2022 pm 06:47 PM

在linux中,交叉编译是指在一个平台上生成另一个平台上的可执行代码,即编译源代码的平台和执行源代码编译后程序的平台是两个不同的平台。使用交叉编译的原因:1、目标系统没有能力在其上进行本地编译;2、有能力进行源代码编译的平台与目标平台不同。

linux中eof是什么linux中eof是什么May 07, 2022 pm 04:26 PM

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

linux怎么判断pcre是否安装linux怎么判断pcre是否安装May 09, 2022 pm 04:14 PM

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux怎么查询mac地址linux怎么查询mac地址Apr 24, 2022 pm 08:01 PM

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

linux中rpc是什么意思linux中rpc是什么意思May 07, 2022 pm 04:48 PM

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!