使用内联函数乘以定义的链接器错误 [英] Multiply defined linker error using inlined functions

查看:24
本文介绍了使用内联函数乘以定义的链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接器报告内联函数的多重定义错误.

The linker is reporting multiply defined errors for an inline function.

我在头文件中有以下代码:

I have the following code in a header file:

struct Port_Pin
{
    volatile uint32_t *     port_addr_set_value;    //!< Writing the pin value here sets the pin to high.
    volatile uint32_t *     port_addr_clr_value;    //!< Writing the pin value to this port clears the pin to low.
    volatile uint32_t *     port_addr_read_value;   //!< Address to read pin value.
    volatile uint32_t *     port_addr_enable;       //!< Writing the pin value here enables the pin (for reading or writing).
    volatile uint32_t *     port_addr_disable;      //!< Writing the pin value here disables the pin.
    volatile uint32_t *     port_addr_dir_output;   //!< Writing the pin value here sets the pin as an output.
    volatile uint32_t *     port_addr_dir_input;    //!< Writing the pin value here sets the pin as an input.
    unsigned int            pin_bit_position;       //!< Zero based, where position zero is first bit position.
};

inline void
Write_Port_Pin(const struct Port_Pin *  p_port,
               uint8_t                  bit)
{
    volatile uint32_t * port_addr = 0;
    port_addr = ((bit & 1) == 0) ? p_port->port_addr_clr_value
        : p_port->port_addr_set_value;
    *port_addr = 1 << p_port->pin_bit_position;
    return;
}

我将头文件包含在多个源 (.c) 文件中.

I include the header file in more than one source (.c) file.

我希望将上述函数内联粘贴,无论它被调用.
是否有一种技术可以在包含的每个源文件中没有函数的多个定义? 如果有,请提供示例.

I would like to have the above function pasted inline wherever it is called.
Is there a technique for this without have multiple definitions of the function in each source file that is included? If yes, please provide example.

我需要针对嵌入式平台进行性能优化.
编译器或链接器是否足够聪明,可以在其他翻译单元中定义函数时内联函数?

I need the performance optimization for an embedded platform.
Are compilers or linkers smart enough to inline functions when they are defined in other translation units?

我在嵌入式 ARM9 平台上使用 Green Hills 编译器 4.2.4.假设 2000 年之前的 C 语言标准.这是 C 代码而不是 C++.

推荐答案

inline 只是一个建议,而不是命令.但是,通常编译器足够聪明,可以做正确的事情(就优化而言,Green Hills 享有良好的声誉).

inline is just a suggestion, not a command. However, in general compilers are smart enough to do the right thing ( and Green Hills has a good reputation in so far as optimizations go ).

将函数设为静态内联",这将阻止编译器使符号可导出.那应该可以解决您的多个定义链接错误...链接器抱怨从多个源模块导出了相同的函数.

Make the function 'static inline', which will prevent the compiler from making the symbol exportable. That should fix your multiple definition link errors... the linker is complaining that the same function is exported from several source modules.

这篇关于使用内联函数乘以定义的链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆