Built-in Multiple Definition Function

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 only source2.cwith gcc source2.c, it compiles. However, when I try to compile with gcc source1.c source2.c, I get 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 tried to find this and found a similar question to several definitions of an inline function . However, in his case, the error is caused by overriding its built-in function. In my case, I am not redefining it (or at least not explicitly ...).

+4
source share
2 answers

source1.c source1.o, func1. , source2.c source2.o, func1. , source1.o source2.o, .

, source1.c source2.c . .

, :

int func1();

.

inline. static, .

+11

, static:

static inline int func1() {
    return 1;
}

, (), .

, gcc :

, ; , , . .

+6

Source: https://habr.com/ru/post/1625873/


All Articles