Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Inline procedure defined in the same module isn't found by the linker #20982

Open Quuxplusone opened 10 years ago

Quuxplusone commented 10 years ago
Bugzilla Link PR20983
Status REOPENED
Importance P release blocker
Reported by Yuri (yuri@tsoft.com)
Reported on 2014-09-17 15:31:05 -0700
Last modified on 2014-09-18 22:16:15 -0700
Version trunk
Hardware PC FreeBSD
CC llvm-bugs@lists.llvm.org, rnk@google.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
Function setRGB is first declared as inline, then defined as inline, and then
not found by the linker.

$ clang -I/usr/local/include -lm -L/usr/local/lib -lpng -o makePNG makePNG.c
/tmp/makePNG-3638e2.o: In function `writeImage':
makePNG.c:(.text+0x6e8): undefined reference to `setRGB'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Removing 'inline' attribute fixes the compile.
This problem is particularly bad due to the simplicity of this situation.

The testcase is from this book:
http://www.labbookpages.co.uk/software/imgProc/libPNG.html
Particular failing program:
http://www.labbookpages.co.uk/software/imgProc/files/libPNG/makePNG.c
(The only change I made is replaced malloc.h -> stdlib.h)

gcc compiles it successfully.

Observed in rev.217271 on FreeBSD 10
Quuxplusone commented 10 years ago
Clang defaults to C99, and gcc to C89 with gnu inline rules, which are
incompatible.

The program as written is ill-formed in C99 because it doesn't provide a strong
definition for setRGB. Simply add 'static' if setRGB is local to the TU or
redeclare it without inline after the definition if it is not.

This is a FAQ:
http://clang.llvm.org/compatibility.html#inline
Quuxplusone commented 10 years ago

I would like to suggest that compiler issues a warning (or an error) in such case.

It sees the function is declared inline, defined inline, and used, it knows that this function is essentially unusable under the C99 standard, so it should say:

module.c:NN: warning: Function 'setRGB' lacks the strong definition, required by C99 standard.

This situation may and will cause user questions, because the linker error isn't descriptive enough.