Search code examples
gcclinker-errorsexterngcc-warninglinker-flags

extern variable not define but no error from compiler


I am compiling my .so library code and came across a weird problem. In one the the header file i have decleared a variable extern that is included in other .c file and not defined any where. Code compiles fine and while runtime it gives error. Why no error or warning at compile time ? I am using gcc 9.


Solution

  • Use -Wl,--no-undefined and specify the libraries where your functions come from on the command line. For example, if library x uses functions defined in library y:

    gcc -shared -o libx.so x.c                        # OK
    gcc -shared -o libx.so x.c -Wl,--no-undefined     # undefined symbols from lib y
    gcc -shared -o libx.so x.c -ly -Wl,--no-undefined # OK again