3

I've recently run into a problem with compiling a piece of code after upgrading debian testing, getting the gcc 6.2.1 compiler. I've boiled it down to this simple example:

inline int func(void) {
    return 0;
}

int main (int argc, char **argv) {
    func();
}

The code does not compile with the following:

gcc -o exec code.c # gcc 6.2.1

It fails with:

undefined reference to 'func'

I have also tried and failed with gcc 4.8, 4.9 and 5 on the same host. It does compile if I add:

gcc -o exec code.c -O2 # gcc 6.2.1

I'm really curious as to why it works with the -O2 flag but not without, I'd expect this to work?

  • 6
    What do you mean "[t]he code does *not* compile"? What errors do you get? Please edit your question to include the full, complete and unedited output from the compiler. – Some programmer dude Dec 19 '16 at 07:52
  • 1
    How many times do we have to repeat what `inline` does... Seriously... – DeiDei Dec 19 '16 at 07:52
  • 2
    Your program doesn't compile because there is no `return` statement int the `func`. function. The error message from the compiler is pretty clear. – Jabberwocky Dec 19 '16 at 08:03
  • 1
    http://stackoverflow.com/questions/16245521/c99-inline-function-in-c-file/16245669#16245669 – dbrank0 Dec 19 '16 at 08:05
  • A function accepting no arguments is `(void)` in C. Not just a pair of empty parentheses. – unwind Dec 19 '16 at 10:21
  • Added what you've commented about. – Oscar Andreasson Dec 19 '16 at 11:10
  • 1
    I guess the question has been answered in other questions already so you can remove it if you wish, however, the response below by Sumit is better than most of the other responses I've seen. – Oscar Andreasson Dec 19 '16 at 11:11
  • Possible duplicate of [C99 inline function in .c file](https://stackoverflow.com/questions/16245521/c99-inline-function-in-c-file) – E_net4 Nov 07 '18 at 12:00

1 Answers1

10

Adding the "-O" option to your compiler command. Inlining is turned on only when optimization is enabled.

C99 inline functions

By default, Clang builds C code in GNU C11 mode, so it uses standard C99 semantics for the inline keyword. These semantics are different from those in GNU C89 mode, which is the default mode in versions of GCC prior to 5.0. For example, consider the following code:

inline int add(int i, int j) { return i + j; }

int main() {
  int i = add(4, 5);
  return i;
}

In C99, inline means that a function's definition is provided only for inlining, and that there is another definition (without inline) somewhere else in the program. That means that this program is incomplete, because if add isn't inlined (for example, when compiling without optimization), then main will have an unresolved reference to that other definition. Therefore we'll get a (correct) link-time error like this:

Undefined symbols:
  "_add", referenced from:
      _main in cc-y1jXIr.o

By contrast, GNU C89 mode (used by default in older versions of GCC) is the C89 standard plus a lot of extensions. C89 doesn't have an inline keyword, but GCC recognizes it as an extension and just treats it as a hint to the optimizer.

There are several ways to fix this problem:

  1. Change add to a static inline function. This is usually the right solution if only one translation unit needs to use the function. static inline functions are always resolved within the translation unit, so you won't have to add a non-inline definition of the function elsewhere in your program.
  2. Remove the inline keyword from this definition of add. The inline keyword is not required for a function to be inlined, nor does it guarantee that it will be. Some compilers ignore it completely. Clang treats it as a mild suggestion from the programmer.
  3. Provide an external (non-inline) definition of add somewhere else in your program. The two definitions must be equivalent!
  4. Compile in the GNU C89 dialect by adding -std=gnu89 to the set of Clang options. This option is only recommended if the program source cannot be changed or if the program also relies on additional C89-specific behavior that cannot be changed.

All of this only applies to C code; the meaning of inline in C++ is very different from its meaning in either GNU89 or C99.

Sumit Gemini
  • 1,836
  • 1
  • 15
  • 19
  • 1
    OP is compiling with gcc, not clang. – frasnian Dec 19 '16 at 08:52
  • I thank you for the explanations :). I failed to note that inline was not available standalone in c99 and higher. How would you provide the external non-inline without adding the "add" function which will clash with the "inline add" function? – Oscar Andreasson Dec 19 '16 at 11:05