Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline function multiple definition

Tags:

c

gcc

inline

I have the following three files:

inline_header.h

#ifndef INLINE_HEADER_H
#define INLINE_HEADER_H

inline int func1() {
    return 1;
}

#endif

source1.c

#include "inline_header.h"

source2.c

#include "inline_header.h"

int main() {
    func1();
}

When I compile just source2.c with gcc source2.c it compiles. However, when I attempt to compile with gcc source1.c source2.c I get the a multiple definition error as follows:

/tmp/cchsOaHF.o: In function `func1':
source2.c:(.text+0x0): multiple definition of `func1'
/tmp/ccEyUW0T.o:source1.c:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status

I am compiling with gcc 4.8.4 on Ubuntu 14.04.

I've tried looking this up and found a similar question multiple definition of inline function. However in his case, the error is caused by a redefinition of his inline function. In my case, I am not redefining it (or at least not explicitly...).

like image 980
kevinAlbs Avatar asked Jan 25 '16 16:01

kevinAlbs


People also ask

What is inline function Short answer?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

Why is debugging faster when the inline function is used?

For inline functions, the debugger displays local variables, but not parameters. When code gets optimized, it is transformed to run faster and use less memory.

What is inline function explain with example?

The inline keyword tells the compiler to substitute the code within the function definition for every instance of a function call. Using inline functions can make your program faster because they eliminate the overhead associated with function calls.

What is the properties of inline function?

Inline functions provide following advantages: 1) Function call overhead doesn't occur. 2) It also saves the overhead of push/pop variables on the stack when function is called. 3) It also saves overhead of a return call from a function.


2 Answers

When you compile source1.c into source1.o, it contains a definition of func1. Similarly, when you compile source2.c into source2.o, it also contains a definition of func1. So when you link source1.o and source2.o, you get a multiple definition error.

The reason the include guards don't prevent this is because source1.c and source2.c are each compiled separately. Include guards only help within a single compilation unit.

If this were not an inline function, you'd put a declaration in the header file:

int func1();

Then put the definition in exactly one source file.

However, you're defining the function as inline. So you need to also declare it as static so that each compilation unit gets its own copy of the function.

EDIT:

The multiple definition error is happening because you're compiling in C89 mode by default, and inline isn't part of that version of the standard. As such, it seems that gcc is basically ignoring that keyword.

If you compile in C99 or C11 mode using -std=c99 or =std=c11 with this code, you'll actually get an "undefined reference" error. Section 6.7.4p7 of the C standard states the following:

Any function with internal linkage can be an inline function. For a function with external linkage, the following restrictions apply: If a function is declared with an inline function specifier, then it shall also be defined in the same translation unit. If all of the file scope declarations for a function in a translation unit include the inline function specifier without extern, then the definition in that translation unit is an inline definition. An inline definition does not provide an external definition for the function,and does not forbid an external definition in another translation unit. An inline definition provides an alternative to an external definition, which a translator may use to implement any call to the function in the same translation unit. It is unspecified whether a call to the function uses the inline definition or the external definition

What this means is that a function with only inline doesn't actually provide a definition of a function that can be called. In your case, you want to add the static storage class specifier to force a local definition in each file.

Interestingly, if you compile this code as is with -O1 and -std=c99, gcc will physically inline the function and it will compile and run cleanly.

like image 167
dbush Avatar answered Oct 08 '22 04:10

dbush


If you wish to place this sort of function in a header, it must also be static:

static inline int func1() {
    return 1;
}

This will cause the symbol to be local to each compilation unit (file), avoiding any linker errors.

Also, from the gcc manual:

When an inline function is not static, then the compiler must assume that there may be calls from other source files; since a global symbol can be defined only once in any program, the function must not be defined in the other source files, so the calls therein cannot be integrated. Therefore, a non-static inline function is always compiled on its own in the usual fashion.

like image 34
FatalError Avatar answered Oct 08 '22 05:10

FatalError