1

I'm trying to use the function casin from the complex.h library, but an error ocurres. This is the code:

#include <stdio.h>
#include <complex.h>
#include <math.h>

int main() {

  double complex z = 1.0 - 1.0 * I;
  double complex arcz = casin(z);
  return 0;
}

I'm getting an "Undefined reference to casin"

CodeSniffer
  • 83
  • 1
  • 10

1 Answers1

2

You lacked #include <complex.h>, and need to specify the math library while compile, e.g.,

gcc -std=c11 -Wall test.c -o test -lm
artm
  • 17,291
  • 6
  • 38
  • 54
  • The error persists. Note that using a real argument like casin(-2) everything goes well, but the signature of the function says "double complex casin(double complex z)" so it should work properly with complex numbers. More info about the function http://pubs.opengroup.org/onlinepubs/009695399/functions/casin.html – CodeSniffer Jan 09 '17 at 11:26
  • your original error is `"Undefined reference to casin"` - do you mean it still persists? – artm Jan 09 '17 at 11:28
  • Yes, same error. But this error doesn't appear using a real argument with the function. – CodeSniffer Jan 09 '17 at 11:32
  • what compiler are you using? – artm Jan 09 '17 at 11:33
  • gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4 – CodeSniffer Jan 09 '17 at 11:34
  • I'm using `gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)` - there is no such error with the command given in the answer – artm Jan 09 '17 at 12:08
  • The above command line should not contain the `-c` option, as that will just compile "test.c" to a relocatable object file, not an executable. The `-o test` option will set the name of the object file to "test" instead of "test.o", which will add to the confusion. – Ian Abbott Jan 09 '17 at 13:25
  • The problem is not in the compilation flags. The function already works with real arguments, however the signature says that should also work with complex numbers. So, if you put a real number no error occurs but if you use a complex as an argument then you got the "Undefined reference to casin". – CodeSniffer Jan 09 '17 at 13:32
  • @IanAbbott that's correct, thanks (edited) – artm Jan 09 '17 at 13:34
  • @CodeSniffer my bad, you should use `-lm` at the end (see edited) – artm Jan 09 '17 at 13:34
  • @CodeSniffer refer to this as to why library should be placed at the end http://stackoverflow.com/questions/9417169/why-does-the-library-linker-flag-sometimes-have-to-go-at-the-end-using-gcc – artm Jan 09 '17 at 13:37
  • The -lm flag does not matter at all, since the math.h library is not used in casin function. The library used is complex.h. So -lm flag does not change the error output. – CodeSniffer Jan 09 '17 at 13:38
  • @CodeSniffer I believe `` is not the same as math library. The math library contains both `` and `` - you need to specify the library even if `` is not used in this case – artm Jan 09 '17 at 13:46
  • note: a library can contain many headers – artm Jan 09 '17 at 13:47
  • Solved. It works. – CodeSniffer Jan 09 '17 at 13:50