所以我有以下代码:
#include <stdio.h>
#define MAX(a,b) (a) > (b) ? a : b
int abs(int a)
{
if (a > 0)
return a;
else
return a*-1;
}
inline int avg(int a, int b)
{
return (a+b)/2;
}
int math_max(int a, int b)
{
return (avg(a,b)+abs(avg(a,b)-b));
}
int main(int argc, char** argv)
{
return 0;
}
编译时:gcc -g test.c -o test
我得到:
/tmp/ccZbHEju.o: In function `math_max':
test.c:(.text+0x33): undefined reference to `avg'
test.c:(.text+0x44): undefined reference to `avg'
collect2: error: ld returned 1 exit status
我可以通过从avg中删除内联符号来解决这个问题,但是我不知道为什么我会得到未定义的引用,如果它是内联的 - 我认为编译器应该处理它
有人在乎解释吗?
** 添加更多信息 **
对于那些无法复制的人,我正在使用:
gcc --version gcc (GCC) 5.3.0 Copyright (C) 2015 Free Software
Foundation, Inc. This is free software; see the source for copying
conditions. There is NO warranty; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.
使用不带链接编译此代码会生成以下内容:
[ishaypeled@arania test]$ gcc -c test.c -o ./test.o
[ishaypeled@arania test]$ readelf -s ./test.o
Symbol table '.symtab' contains 13 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FILE LOCAL DEFAULT ABS test.c
2: 0000000000000000 0 SECTION LOCAL DEFAULT 1
3: 0000000000000000 0 SECTION LOCAL DEFAULT 3
4: 0000000000000000 0 SECTION LOCAL DEFAULT 4
5: 0000000000000000 0 SECTION LOCAL DEFAULT 6
6: 0000000000000000 0 SECTION LOCAL DEFAULT 7
7: 0000000000000000 0 SECTION LOCAL DEFAULT 5
8: 0000000000000000 25 FUNC GLOBAL DEFAULT 1 abs
9: 0000000000000019 43 FUNC GLOBAL DEFAULT 1 max
10: 0000000000000044 69 FUNC GLOBAL DEFAULT 1 math_max
11: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND avg
12: 0000000000000089 18 FUNC GLOBAL DEFAULT 1 main
其中清楚地显示avg为NOTYPE和UND - 这就是链接器失败的原因......我不知道为什么会发生这种情况。我还检查了gcc的预处理器输出:
[ishaypeled @ arania test] $ gcc test.c -E
# 5 "test.c"
int abs(int a)
{
if (a > 0)
return a;
else
return a*-1;
}
int max(int a, int b)
{
return ((a+b)+abs(a-b))/2;
}
inline int avg(int a, int b)
{
return (a+b)/2;
}
int math_max(int a, int b)
{
int avg_calc = avg(a,b);
int distance_from_avg = abs(avg_calc-a);
int res = avg_calc+distance_from_avg;
return res;
}
int main(int argc, char** argv)
{
# 44 "test.c"
return 0;
}
看起来非常好,avg在math_max中使用之前出现。
STDC version: 201112
__GNUC__ defined