Thread: GCC can't find some complex.h functions

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    3

    Question GCC can't find some complex.h functions

    With code like this:
    Code:
    #include <stdio.h>
    #include <complex.h>
    #include <math.h>
    
    
    int main(int argc, char *argv[]) {
    
    
        double complex z1 = 0.0 + 1.0 * I;
        double a = csin(z1);
        printf("%lf", a);
    
    
        return 0;
    }
    I get - undefined reference to `csin' and several other complex functions
    But some functions work eg creal and cimag but others do no.
    This is with gcc compiler, through Netbeans, on Debian Mint.
    The compiler standard is set to C99.
    TIA

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Link with the math library.
    Code:
    $ gcc foo.c
    /tmp/ccm0ndwS.o: In function `main':
    foo.c:(.text+0x3c): undefined reference to `csin'
    collect2: error: ld returned 1 exit status
    $ gcc foo.c -lm
    $
    Quote Originally Posted by man page
    NAME
    csin, csinf, csinl - complex sine function

    SYNOPSIS
    #include <complex.h>

    double complex csin(double complex z);
    float complex csinf(float complex z);
    long double complex csinl(long double complex z);

    Link with -lm.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    3
    OK thanks this works. Can you explain why this is needed? Is it because gcc treats these as 'built-in' functions?

    And why it does not work from Netbeans, with command line options set to:
    -m64 -lm -g -Wall -std=c99
    ?
    Last edited by WMilner; 04-13-2020 at 03:40 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > Can you explain why this is needed?
    Because the manual says so.

    Everything in the C standard library except math is resolved by libc, and that is linked by the compiler by default.
    The math library is separate, because of history.


    > -m64 -lm -g -Wall -std=c99?
    Code:
    $ gcc foo.c -lm
    $ gcc -lm foo.c
    /tmp/ccIluqrt.o: In function `main':
    foo.c:(.text+0x3c): undefined reference to `csin'
    collect2: error: ld returned 1 exit status
    Some things are order dependent.

    The linker needs to know that 'csin' is undefined as a result of compiling your code before it read the definition of 'csin' from the library.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    3
    But functions in math.h eg exp do not require the -lm flag (even though the manual says they do) - either in gcc at the command line or from Netbeans

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Sometimes you need -lm, and other times you don't.
    Code:
    $ cat foo.c
    #include <stdio.h>
    #include <math.h>
    
    int main(){
      double x = 10.0;
      double y = exp(x);
      printf("%lf = %lf\n", x, y);
      return 0;
    }
    $ gcc foo.c
    /tmp/cceZwDmN.o: In function `main':
    foo.c:(.text+0x23): undefined reference to `exp'
    collect2: error: ld returned 1 exit status
    
    $ gcc -O2 foo.c
    Other Builtins (Using the GNU Compiler Collection (GCC))
    Many of these functions are only optimized in certain cases; if they are not optimized in a particular case, a call to the library function is emitted.
    So, depending on too many factors to possibly list, any given call to a math function may or may not necessitate the addition of the -lm linker option.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-09-2012, 01:51 PM
  2. Replies: 15
    Last Post: 12-01-2012, 11:15 AM
  3. Replies: 3
    Last Post: 10-19-2009, 10:54 PM
  4. about complex functions in C
    By MyRedz in forum C Programming
    Replies: 10
    Last Post: 10-12-2008, 02:26 PM
  5. How to implement Complex exponential functions in C ???
    By Clueless@work in forum C Programming
    Replies: 10
    Last Post: 04-25-2007, 05:51 PM

Tags for this Thread