This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Support for inline functions in C mode

When trying to compile the following file using arm-none-eabi-gcc-7.3.1 I get a linker error.

inline int test()
{
	return 0;
}

int main(int argc, char* argv[])
{
	test();
	return 0;
}

Command line: arm-none-eabi-gcc.exe test.c --specs=nosys.specs

Output:

ccc547p6.o: In function `main':
test.c:(.text+0x14): undefined reference to `test'

Analysis of intermediate assembly output reveals that compiler treats test() function as inline and does not generate a body for it, but then, when it generates a code for main function, it generates a regular, non-inline call to test() function instead of inlining it, which, of course, upsets the linker later.

Probing the compiler for __STDC_VERSION__ reveals that it is set to 201112L, pointing to C11 compliance. Support for inline functions in C mode was introduced in C99, so it should be handled correctly by C11-compliant compiler.

Of course, inline function specifier is merely a hint (according to the standard), so implementations may elect to not inline functions at all, but they can't do it half-way by first not generating a function body, but then calling functions using regular calls - this will lead to inability to link.

By the way, none of this happens in C++ mode, arm-none-eabi-g++ compiles and links the same file without an error.

Am I doing something wrong?

Parents
  • Hi,

    You can fix the error a couple different ways. The first one is to add static.

    static inline int test()
    {
    	return 0;
    }
    
    int main(int argc, char* argv[])
    {
            test();
    	return 0;
    }
    

    The second option is to add extern because inline by itself creates external linkage.

    extern int test();
    
    inline int test()
    {
    	return 0;
    }
    
    int main(int argc, char* argv[])
    {
            test();
    	return 0;
    }
    

    Thanks,

    Jason

Reply
  • Hi,

    You can fix the error a couple different ways. The first one is to add static.

    static inline int test()
    {
    	return 0;
    }
    
    int main(int argc, char* argv[])
    {
            test();
    	return 0;
    }
    

    The second option is to add extern because inline by itself creates external linkage.

    extern int test();
    
    inline int test()
    {
    	return 0;
    }
    
    int main(int argc, char* argv[])
    {
            test();
    	return 0;
    }
    

    Thanks,

    Jason

Children
No data