Simple C inline linker error

前端 未结 3 541
轮回少年
轮回少年 2020-12-06 12:28

simple problem:

given the following program:

#include 

inline void addEmUp(int a, int b, int * result)
{
    if (result) {
        *r         


        
3条回答
  •  -上瘾入骨i
    2020-12-06 13:15

    The program causes undefined behaviour (no diagnostic required) due to 6.9/5, sometimes informally called the "one definition rule":

    If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof or _Alignof operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one.

    Your program uses the identifier addEmUp while providing no external definition for it. (As mentioned already, "An inline definition does not provide an external definition for the function").

    We do not need to start talking about which definition a function call calls etc. etc. The reason for ODR violations being undefined with no diagnostic required is to make it easier on the compiler writer; if this code required a diagnostic then the compiler would have to do a pass with inline optimization disabled to check for the existence of the external definition, which is a waste of time really.

提交回复
热议问题