不同gcc编译内联函数的差异

inline reference:
https://blog.csdn.net/zqixiao_09/article/details/50877383

gcc8.3 必须声明,不然报错, undefined reference to `xxx’

$ make test && ./test 
cc     test.c   -o test
/tmp/ccLJDAHG.o: In function `main':
test.c:(.text+0x25): undefined reference to `dbtest'
collect2: error: ld returned 1 exit status
make: *** [<builtin>: test] Error 1
[yumwang@hz72-12-rhel8-oms test]$ gcc --version
gcc (GCC) 8.3.1 20191121 (Red Hat 8.3.1-5)
Copyright (C) 2018 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.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


//函数定义为inline即:内联函数  
//char* dbtest(int a);//必须声明,不然报错
inline char* dbtest(int a) 
{  
	return (a % 2 > 0) ? "奇" : "偶";  
}   


int main(int argc, char* argv[])
{
	int i = 0;  
	for (i=1; i < 100; i++) 
	{  
		printf("i:%d    奇偶性:%s \n", i, dbtest(i));      
	}  

    return 0;
}

gcc4.8不用声明可直接编译

gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
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.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
inline char* dbtest(int a)
{
        return (a % 2 > 0) ? "奇" : "偶";
}


int main(int argc, char* argv[])
{
        int i = 0;
        for (i=1; i < 100; i++)
        {
                printf("i:%d    奇偶性:%s \n", i, dbtest(i));
        }



    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值