Compiling error when -std=gnu99 and inline function is used

后端 未结 4 1470
轻奢々
轻奢々 2021-02-19 06:43

The following code:

#include 
inline int myfunc (int x) {
    return x+3;
}

int main () {

    printf(\"%d\", myfunc(2));
    return 0;
}
         


        
4条回答
  •  旧时难觅i
    2021-02-19 07:05

    You should declare 'myfunc' before define it. For example this code can be compiled with -std=gnu99 option:

    #include 
    
    int myfunc(int);
    
    inline int myfunc (int x) {
        return x+3;
    }
    
    int main () {
    
        printf("%d", myfunc(2));
        return 0;
    }
    

    UPDATED

    Actually, regarding to the C standard inline keyword is just a suggestion to the C compiler. But compiler can choose not to inline. So it can do its own way, therefore.

    In your example you can use function declaration as I showed above - or you can add optimization flag '-O3' (tested on linux gcc) and above - in this case your source code will compiled without extra-declaration.

    UPDATED

    Here you can find deeper explanation: https://blogs.oracle.com/dew/entry/c99_inline_function

提交回复
热议问题