linux内核函数do_div与undefined reference to `__udivdi3'解决方法

本文详细介绍了在Linux环境下处理64位整数除法时常见的编译错误“undefined reference to '__udivdi3'”,并提供了解决方案。通过使用特定的除法函数如div_u64(),可以避免此类错误。

【问题】

编译Linux下面的代码,经常会遇到这种错误:

undefined reference to `__udivdi3'

【解决过程】

之前遇到过几次了,都是类似的原因导致此问题的。后来才了解,其根本原因:

嵌入式中,32位系统中(目前多数系统都是,比如ARM的片子),对于普通的a除以b(b为32位):

(1)当a为32位,Linux 内核中,常用uint32_t 类型,可以直接写为 a/b

(2)但是,对于a是64位,uint64_t的时候,就要用到专门的除操作相关的函数,linux内核里面一般为

do_div(n, base),注意,此处do_div得到的结果是余数而真正的a/b的结果,是用a来保存的

do_div(n,base)的具体定义,和当前体系结构有关,对于arm平台,在

arch/arm/include\asm\div64.h

其实现很复杂,感兴趣的自己去代码里看吧,这里不多说了。

因此,如果你当前写代码,a/b,如果a是uint64_t类型,那么一定要利用do_div(a,b),而得到结果a,

而不能简单的用a/b,否则编译可以正常编译,但是最后链接最后出错,会提示上面的那个错误:

undefined reference to "__udivdi3"

【解决方法】

知道原因,就好办了。办法就是,去你代码里面找到对应的用到除法的地方,即类似于a/b的地方,其中被除数a为64位,Linux中一般用用uint64_t,将a/b用do_div(a,b)得到的a去代替(注意,不是直接用do_div()得到真正a除b后的结果,因为do_div(a,b)得到的是余数,囧。。。),即可,而具体写其他,就显得很麻烦。此处,我们可以借鉴Linux中\fs\yaffs2\yaffs_fs.c中的宏:

static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
{
uint64_t result = partition_size;
do_div(result, block_size);
return (uint32_t)result;
}

来自己也去封装一个支持64位数的除法的函数,不过,Linux内核就是好,早已经帮我们实现了对应的64位的unsingned和signed两个函数:

static inline u64 div_u64(u64 dividend, u32 divisor);

static inline s64 div_s64(s64 dividend, s32 divisor);
我们可以直接拿过来用了,注意用此函数时,要包含对应头文件:

#include <linux/math64.h>

总结一下就是:

1.先包含头文件:

2.然后用(a,b)得到a/b的结果即可。

【提示】

如果需要在进行64位除数的时候,同时得到余数remainder,可以直接用

static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder);
static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder);

【引用】

1. - 64 bit division in linux

If you've encountered an error message like this

Unknown symbol __udivdi3Unknown symbol __umoddi3Unresolved symbol __udivdi3Unresolved symbol __umoddi3

you most likely want to make a 64 bit division, which is not supported by default in linux kernel space.
To solve this problem, you need to use the do_div macro available in asm/div64.h:

#include <asm/div64.h>unsigned long long x, y, result;unsigned long mod;mod = do_div(x, y);result = x;

If you want to calculate x / y with do_div(x, y), the result of the division is in x, the remainder is returned from the do_div function.
Since do_div is just an asm (assembler) macro, it doesn't break real time determinism, so it's also suitable for use in RTAI classic, RTAI fusion and ADEOS/ADEOS-IPIPE applications.

代码如下: #include <board.h> #include <rtthread.h> #include <rtdevice.h> #include <string.h> #define DBG_TAG "main" #define DBG_LVL DBG_LOG #include <rtdbg.h> #define PWM_DEV_NAME "PWM1" #define CHANNEL1 0 // 通道1(对应PE9) #define CHANNEL2 1 // 通道2(对应PE11) int mian() { rt_device_t pwm_dev; pwm_dev = rt_device_find(PWM_DEV_NAME); if (!pwm_dev) { rt_kprintf("PWM device not found\n"); return; } /* 打开设备 */ if (rt_device_open(pwm_dev, RT_DEVICE_FLAG_RDWR) != RT_EOK) { rt_kprintf("Open device failed\n"); return; } /* 配置通道1(PE9) */ rt_pwm_set(pwm_dev, CHANNEL1, 1000000, 500000); // 周期1ms, 占空比50% rt_pwm_enable(pwm_dev, CHANNEL1); // 启用输出 /* 配置通道2(PE11) */ rt_pwm_set(pwm_dev, CHANNEL2, 2000000, 1500000); // 周期2ms, 占空比75% rt_pwm_enable(pwm_dev, CHANNEL2); // 启用输出 while(1) { } } __WEAK void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 4; RCC_OscInitStruct.PLL.PLLN = 168; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 4; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { Error_Handler(); } } 但是报错: linking... ./rt-thread/src/components.o: In function `main_thread_entry&#39;: F:\Code_Test\keil_test\car\brain\Debug/../rt-thread/src/components.c:198: undefined reference to `main&#39; collect2.exe: error: ld returned 1 exit status make: *** [makefile:85: rtthread.elf] Error 1 "make -j12 all" terminated with exit code 2. Build might be incomplete.
最新发布
09-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值