jdm
jdm

Reputation: 10050

Why is "symbol lookup error" not caught at compile-time?

I'm updating some old code, and accidentally left a macro inside that's no longer defined:

// "DETAIL" is not defined anywhere
if (DETAIL ("notebook")) {
    // ...
}

The code still compiles and links (its a .so library), but when it is loaded I get:

gtk3-widget-factory: symbol lookup error: /usr/lib/gtk-3.0/3.0.0/
theming-engines/libmurrine.so: undefined symbol: DETAIL

Why isn't this caught at compile time? I guess C sees DETAIL without any definition and assumes it is a function int -> int , right? Is there any way to make the compiler (gcc) be more strict and complain at compile time? I don't think I'm directly using any symbols without definition in my source or header files.

Upvotes: 2

Views: 600

Answers (1)

NPE
NPE

Reputation: 500317

Your analysis is correct: DETAIL is treated as an implicitly-defined function.

From the gcc manual:

-Wimplicit-function-declaration (C and Objective-C only)

Give a warning whenever a function is used before being declared. In C99 mode (-std=c99 or -std=gnu99), this warning is enabled by default and it is made into an error by -pedantic-errors. This warning is also enabled by -Wall.

Additionally, as @Jonathan Leffler points out in the comments:

Also, in some versions of GCC, it's an error with -Werror=implicit-function-declaration (and similarly for any other explicit warning option). All warnings can be converted to errors with -Werror.

Upvotes: 4

Related Questions