[cfe-dev] question about function with linkage type AvailableExternallyLinkage

John McCall rjmccall at apple.com
Wed Feb 2 20:00:35 PST 2011


On Feb 2, 2011, at 2:28 PM, Akira Hatanaka wrote:
> I am getting linkage errors when I try to compile foo1.c:
> 
> $: clang foo1.c -o foo1 -O3
> /tmp/cc-VXVfGf.o:(.data+0x0): undefined reference to `f1'
> /tmp/cc-VXVfGf.o:(.data+0x4): undefined reference to `f2'
> clang: error: linker command failed with exit code 1 (use -v to see invocation)
> 
> I inspected the bitcode foo1.ll, and it seems that definitions of f1 and f2 are not emitted because their linkage types are "available_externally".

Clang compiles C files in C99 mode by default.  In C99, 'inline' is a promise that a non-inline declaration exists somewhere else in your program.  Therefore, if the compiler doesn't actually inline the functions, it's supposed to emit an external reference instead of emitting them in this file.

You have five options, which I've listed in descending order of recommendation.
  - Make the functions 'static inline'.  This will cause them to be emitted in this translation unit if they're used but not inlined.  This is the best option if you don't need to call these functions from a different file.
  - Remove 'inline'.  It isn't required in order to get the compiler to inline a function, and while we do honor it as an optimization hint, functions this small will always get inlined anyway.  (Here it's impossible to inline because we can't statically prove what farray[i] will call, in part because farray is mutable.)
  - Make sure there's a different file in your program which provides non-inline definitions of these functions.
  - Compile the file with -std=gnu89, which will cause the functions to be emitted with strong linkage despite the 'inline'.  gcc uses gnu89 by default.
  - Write this in C++ instead of C.  In C++, you can provide definitions of inline functions in multiple translation units and they'll get automatically merged together by the linker.

John.



More information about the cfe-dev mailing list