博客
关于我
《C Primer Plus中文版》第五版--学习记录3
阅读量:211 次
发布时间:2019-02-28

本文共 2488 字,大约阅读时间需要 8 分钟。

Mac OS 下的 GCC 编译问题及解决方案

1. 常见编译问题及解决方案

在 Mac OS 环境下,使用 GCC 编译时,可能会遇到一些常见问题。以下是一些常见问题及其解决方法:

1.1 链接错误

如果你在编译过程中遇到 ld: Undefined symbols 或 Undefined symbols for architecture x86_64 的错误,可能是因为缺少了必要的库文件。检查项目是否有依赖项,确保所有必要的库文件已经包含在编译命令中。

1.2 包含多个参数的格式化警告

在 print1.c 中,编译器会提示 %d 格式化符使用了多于数据的参数。例如:

printf("%d minus %d is %d\n", ten, 2, ten - two);

这里缺少了两个参数,导致警告。正确的格式化方式是:

printf("%d minus %d is %d\n", ten, 2, ten - 2);

1.3 浮点数格式化问题

在 showf_pt.c 中,使用 %f 或 %e 格式化符时,可能会遇到类型不匹配的问题。例如:

printf("%f can be written %e\n", dip, dip);

这里的 dip 是 long double 类型,而 %f 和 %e 的默认类型是 double,因此需要使用 %Lf 和 %Le 格式化符:

printf("%Lf can be written %Le\n", dip, dip);

1.4 类型大小获取错误

在 typesize.c 中,printf 函数使用 %u 格式化符来显示类型的大小。然而,sizeof 函数返回的值是 size_t 类型,而 %u 格式化符可以正确显示 size_t 的值。因此,不需要额外修改代码。

1.5 转义字符问题

在 escape.c 中,scanf 函数使用的格式化符 %f 与 double 类型不匹配,应改为 %lf:

scanf("%lf", &salary);

此外,printf 函数的格式化字符串中,$ 和 \b 组合会导致显示异常,建议使用 \n 和 \t 结合。

2. 浮点数显示的最佳实践

在 showf_pt.c 中,正确的浮点数显示方式是使用特定的格式化符来显示不同精度的浮点数。例如:

float aboat = 32000.0; // 用 `%f` 格式化double abet = 2.14e9;  // 用 `%e` 格式化long double dip = 5.32e-5; // 用 `%Lf` 和 `%Le` 格式化

正确的代码示例:

int main(void) {    float aboat = 32000.0;    double abet = 2.14e9;    long double dip = 5.32e-5;    printf("%f can be written %e\n", aboat, aboat);    printf("%f can be written %e\n", abet, abet);    printf("%Lf can be written %Le\n", dip, dip);    return 0;}

编译并运行后,输出结果为:

32000.000000 can be written 3.200000e+041400000000.000000 can be written 1.400000e+090.000053 can be written 5.320000e-05

3. 可移植的整数类型名称

在 altnames.c 中,int16_t 是一个可移植的 16 位有符号整数类型。为了正确显示其值,可以使用 inttypes.h 中的宏定义:

#include 
int main(void) { int16_t me16 = 4593; printf("First, assume int16_t is short: me16 = %hd\n", me16); printf("Next, use a \"macro\" from inttypes.h: me16 = %" PRId16 "\n", me16); return 0;}

编译结果与预期一致,输出为:

First, assume int16_t is short: me16 = 4593Next, let's not make any assumptions. instead, use a "macro" from inttypes.h: me16 = 4593

4. 转义字符的正确使用

在 escape.c 中,scanf 函数应使用 %lf 格式化符读取浮点数:

int main(void) {    float salary;    printf("\aEnter your desired monthly salary: ");    printf(" $_______\b\b\b\b\b\b\b");    scanf("%lf", &salary);    printf("\n\t$%.2f a month is $%.2f a year.", salary, salary * 12.0);    printf("\rGee!\n");    return 0;}

编译并运行后,输出结果为:

Enter your desired monthly salary: 1234567.00$1234567.00 a month is $14814804.00 a year.Gee!

5. 总结

通过以上优化和修改,可以解决 Mac OS 环境下 GCC 编译的各种问题。关键在于正确使用格式化符,避免类型不匹配和参数数量不符的问题,同时熟悉可移植类型和转义字符的使用方法,可以显著提高代码的可读性和正确性。

转载地址:http://atzi.baihongyu.com/

你可能感兴趣的文章
python变量的详细教程_Python零基础入门教程之语法入门[变量](第二期)
查看>>
Python变量命名方法-ChatGPT4o作答
查看>>
Python变量与运算符
查看>>
python单元测试之unittest
查看>>
Python单元测试unittest实战
查看>>
python协程实时输出_Python 协程以及事件循环的问题,怎么知道 IO 读取结束了呢?...
查看>>
Python化身“安全老中医”:一键搞定工控系统漏洞,从零基础到精通,收藏这篇就够了!
查看>>
Python加速运行技巧
查看>>
python办公自动化基础遍历文件夹
查看>>
python办公自动化基础输出文件和文件夹
查看>>
python办公自动化基础搜索文件
查看>>
python前端之css
查看>>
Python制作进度条,原来有这么多方法
查看>>
Python制作简单的学生成绩管理系统
查看>>
python制作甘特图的基本知识(附Demo)
查看>>
python制作一个简单的服务器,【Python】 做一个简单的 http 服务器
查看>>
Python到底能做什么?它的优点在哪?
查看>>
python利用pytorch库导出图像分割算子
查看>>
python利用pyshark监听网卡来抓包其中pyshark中摸索的一些可用参数
查看>>
python利用excel分析过杀漏失
查看>>