2

This is strange to me.

inline complex_t complex_add(complex_t a, complex_t b)
{
    complex_t r = {a.re + b.re, a.im + b.im};
    return r;
}

inline complex_t complex_subtract(complex_t a, complex_t b)
{
    complex_t r = {a.re - b.re, a.im - b.im};
    return r;
}


inline complex_t complex_multiply(complex_t a, complex_t b)
{
    complex_t r = {a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re};
    return r;
}

inline complex_t complex_conjugate(complex_t a)
{
    complex_t r = {a.re, -a.im};
    return r;
}

inline complex_t complex_divide(complex_t a, complex_t b)
{
    float dem = b.im * b.im + b.re * b.re;

    complex_t r = complex_multiply(a, complex_conjugate(b));
    r.re /= dem;
    r.im /= dem;
    return r;
}

int divergence(double x, double y, int iters, complex_t* r)
{
    const complex_t c = {x, y};
    complex_t z = {0, 0};
    int i;
    for(i = 0; i < iters && complex_mag(z) < 2; i++)
    {
        z = complex_add(complex_multiply(z, z), c);
    }
    *r = z;
    return i;
}

When I compile this code I get the following error in the line z = complex_add(complex_multiply(z, z), c)...

home/chase/workspace/MandelbrotSet/Debug/../main.c:62: undefined reference to `complex_multiply'
makefile:44: recipe for target 'MandelbrotSet' failed
/home/chase/workspace/MandelbrotSet/Debug/../main.c:62: undefined reference to `complex_add'
collect2: error: ld returned 1 exit status
make: *** [MandelbrotSet] Error 1

However when I remove inline from the complex number functions the code compiles fine. Any ideas why? How come adding the inline makes it so the compiler can not find the function?

chasep255
  • 11,745
  • 8
  • 58
  • 115
  • 1
    Which compiler flags are you using? (the behaviour of `inline` differs between C90, C99, and GNU89) – M.M Aug 21 '15 at 00:07
  • `inline` is `static` (file scope). – BLUEPIXY Aug 21 '15 at 00:11
  • @BLUEPIXY no it isn't – M.M Aug 21 '15 at 00:11
  • 1
    Also you willl need to indicate where this code fits into your project structure. For example if these function bodies were in a `.c` file and you tried to call them from a different `.c` file you would get undefined reference errors – M.M Aug 21 '15 at 00:12
  • inline doesn't happen at link time. where did you put your inline function? – Jason Hu Aug 21 '15 at 00:15
  • Everything is in the same c file. I just tested it. It works with c90 but not c99. Why? – chasep255 Aug 21 '15 at 00:15
  • @chasep255 As Matt McNabb said, the behaviour of `inline` in gcc varies depending on the language you select. Specifically, there is the GNU behaviour and the C99 behaviour. Consult the gcc manual for more details. – fuz Aug 21 '15 at 00:16
  • try `inline` --> `inline static` – BLUEPIXY Aug 21 '15 at 00:18
  • 1
    C90 did not have `inline`. So it cannot work in C90. – too honest for this site Aug 21 '15 at 00:30
  • @BLUEPIXY: `static` [should preceed](http://port70.net/~nsz/c/c11/n1570.html#6.11.5) the function specifier actually. – too honest for this site Aug 21 '15 at 00:34
  • Do you have any evidence to suggest that `inline`ing these functions will significantly improve the performance of your program? – autistic Aug 21 '15 at 00:47
  • "Everything is in the same c file" - ok, so your code is the same structure as the example in [this question](http://stackoverflow.com/questions/6312597/is-inline-without-static-or-extern-ever-useful-in-c99) which has links to some very detailed explanations – M.M Aug 21 '15 at 02:02

0 Answers0