c - 使用内联函数乘以定义的链接器错误

标签 c embedded inline-code multiple-definition-error greenhills

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

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

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) 文件中。

我希望将上述函数内联粘贴到任何被调用的地方。
有没有在包含的每个源文件中对函数进行多个定义的技术? 如果有,请提供示例。

我需要嵌入式平台的性能优化。
当函数在其他翻译单元中定义时,编译器或链接器是否足够智能以内联函数?

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

最佳答案

inline 只是一个建议,不是命令。但是,一般来说,编译器足够聪明,可以做正确的事情(而 Green Hills 在优化方面享有盛誉)。

使函数成为“静态内联”,这将阻止编译器使符号可导出。这应该可以解决您的多重定义链接错误...链接器提示从多个源模块导出了相同的函数。

关于c - 使用内联函数乘以定义的链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3160484/

相关文章:

java - 对象的内联实例化

asp.net - 如何在 ASP.NET 页面内定义命名空间(内联代码方法)?

c++ - 异常与 errno

c++ - 使用 Apache Xalan C++ 库改进 XSL 翻译

c++ - RAII 的有用性无一异常(exception)

c++ - 内存使用检查

c - 运行进程的重载符号(LD_PRELOAD附件)

c - 在C中如何将局部函数变量值传递给另一个函数并无错误地打印值

arm - 如何在列表模式下配置我的 CAN 过滤器?