使用内联函数多次定义链接错误 [英] Multiply defined linker error using inlined functions

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

问题描述

链接器是报告多次定义错误的内联函数。

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

我有一个头文件如下code:

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?

我用的绿山编译器4.2.4嵌入式ARM9平台上。假设pre-2000 C语言标准。这是C code不是C ++。

推荐答案

直列只是一个建议,不是命令。但是,一般的编译器有足够的智慧做正确的事(和青山在,只要优化走了良好的信誉)。

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 ).

请功能静态内联,这将prevent自作出符号导出的编译器。这应该解决您的多个定义链接错误......连接器是抱怨,同样的功能从多个源模块的输出。

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天全站免登陆