1

This is straight from the GeeksForGeeks' tutorial on C page: https://www.geeksforgeeks.org/interesting-facts-preprocessors-c/

#include <stdio.h>
inline int square(int x) { return x*x; }
int main()
{
int x = 36/square(6);
printf("%d", x);
return 0;
}
// Output: 1

I get the error: /tmp/ccSHJFrH.o: In function main': a54a7786ee0a4f730407c05eb1147f5f.c:(.text+0xe): undefined reference tosquare' collect2: error: ld returned 1 exit status

For some reason this compiles when compiling with C++, but with C, I get the error message above. I am running this on the IDE on the GeeksForGeeks website.

https://ide.geeksforgeeks.org/index.php

Tommy Saechao
  • 1,099
  • 4
  • 17
  • 28
  • That code is undefined behaviour in ISO C. Remove `inline` to make it work – M.M May 16 '18 at 03:48
  • On the other hand, perfectly valid in contemporary C standards. Turn up the compiler flags. – DeiDei May 16 '18 at 03:48
  • @M.M I misread as "ansi C", apologies. What's wrong with `inline` in C then? It's been used since C99. – DeiDei May 16 '18 at 03:51
  • 1
    @DeiDei I've linked another question. Nothing's wrong with `inline` per se, just this code uses it incorrectly – M.M May 16 '18 at 03:52
  • Alternatively, add `static` to the function declaration to make it work. – John Bollinger May 16 '18 at 03:53
  • @JohnBollinger `inline` affects the linkage as described in the linked question – M.M May 16 '18 at 04:00
  • And note that although the `inline` specifier requests that calls to the so-marked function be as fast as possible, implementations are not required to honor such requests in any way, and those that do are not required to do so by actually inlining the called function body into its caller. – John Bollinger May 16 '18 at 04:04

0 Answers0