我正在学习 2023 年春季的操作系统课程,并尝试使用他们的说明设置环境,以便我可以完成课程作业。这些指令的一部分是构建修改后的 gcc 4.1.2。
我在 RHEL 上使用 gcc 11.3.1,在构建有关
toplev.h
和 toplev.c
中的 excact_log2 和 Floor_log2 函数的 gcc 时遇到多个定义错误。这是一些示例输出:
/usr/bin/ld: attribs.o: in function `floor_log2':
/home/ec2-user/gcc-4.1.2+os161-2.0/host-x86_64-unknown-linux-gnu/gcc/../.././gcc/toplev.h:177: multiple definition of `floor_log2'; c-lang.o:/home/ec2-user/gcc-4.1.2+os161-2.0/host-x86_64-unknown-linux-gnu/gcc/../.././gcc/toplev.h:177: first defined here
/usr/bin/ld: attribs.o: in function `exact_log2':
/home/ec2-user/gcc-4.1.2+os161-2.0/host-x86_64-unknown-linux-gnu/gcc/../.././gcc/toplev.h:183: multiple definition of `exact_log2'; c-lang.o:/home/ec2-user/gcc-4.1.2+os161-2.0/host-x86_64-unknown-linux-gnu/gcc/../.././gcc/toplev.h:183: first defined here
/usr/bin/ld: c-errors.o: in function `floor_log2':
/home/ec2-user/gcc-4.1.2+os161-2.0/host-x86_64-unknown-linux-gnu/gcc/../.././gcc/toplev.h:177: multiple definition of `floor_log2'; c-lang.o:/home/ec2-user/gcc-4.1.2+os161-2.0/host-x86_64-unknown-linux-gnu/gcc/../.././gcc/toplev.h:177: first defined here
/usr/bin/ld: c-errors.o: in function `exact_log2':
/home/ec2-user/gcc-4.1.2+os161-2.0/host-x86_64-unknown-linux-gnu/gcc/../.././gcc/toplev.h:183: multiple definition of `exact_log2'; c-lang.o:/home/ec2-user/gcc-4.1.2+os161-2.0/host-x86_64-unknown-linux-gnu/gcc/../.././gcc/toplev.h:183: first defined here
这似乎与 gcc 现在默认使用 -fno-common 标志有关,但我尝试将 -fcommon 添加到我的 Makefile CFLAGS 中,但没有成功。
在toplev.h代码中有一个#ifdef块,其中包含exact_log2和floor_log2的内联函数:
/* Inline versions of the above for speed. */
#if GCC_VERSION >= 3004
extern inline int
exact_log2 (unsigned HOST_WIDE_INT x)
{
return x == (x & -x) && x ? (int) CTZ_HWI (x) : -1;
}
#endif /* GCC_VERSION >= 3004 */
多重定义是由内联代码引起的。一些解决方案: