Even though this might look like duplication, I'd claim that it isn't.
Why does a compiler make the Foo function, which is defined in foo.c as inline double Foo() { return 1.2; }, a global function if the foo.c file also has the extern double Foo(); line anywhere?
Please see the example with foo.c and main.c:
// foo.c
#if defined(WITH_DECL)
extern double Foo();
#endif
inline double Foo() {
return 1.2;
}
double Bar() {
printf("%lf\n", Foo());
return Foo() + Foo();
}
And,here's main.c:
double Foo();
double Bar();
int main() {
printf("Bar + Foo = %lf + %lf\n", Bar(), Foo());
return 0;
}
Here's how I built:
clang -O2 main.c foo.c
clang -O2 main.c foo.c -DWITH_DECL
The first command failed. The second command silently succeeded.
I understand why the first failed. As expected it's a linker error:
clang -g3 -O0 -std=c11 foo.c main.c -Wall -Werror -O2
/usr/bin/x86_64-pc-linux-gnu-ld.bfd: /usr/bin/x86_64-pc-linux-gnu-ld.bfd: DWARF error: mangled line number section (bad file number)
/tmp/main-10e175.o: in function `main':
/home/aion1223/workspace/prac/main.c:7:(.text+0x8): undefined reference to `Foo'
clang: error: linker command failed with exit code 1 (use -v to see invocation)`
I do not understand why the second command does not fail.
First, do I expect this to happen for all standard-complying compilers?
Second, why at least some of my compilers silently build the executable when -DWITH_DECL is given?
I'd like to figure out if this is something that I can rely on, or it just happened so but is not guaranteed by C language standard.
The standard I tried is c99, c11, and c17. The compilers are clang 20, gcc14, and gcc12.
-std=. we will know what standard you refer to.c11. Just triedc99andc17. Got the same result.@with commenter name when you answer to that specific comment. otherwise I will not see that you have answered.C99,C11, andC17withclang20andgcc9/12/14. All on Linuxx86_64.inlinekeyword? I think, inClanguage it is not so useful as inC++.