Sanjeev
Sanjeev

Reputation: 125

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.

Upvotes: 0

Views: 541

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 120069

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

Upvotes: 0

Related Questions