0

Part of the source code:

double _Complex z = 1.0 + 1.0*I;
printf("%f\n", cabs(z));

My development environment: Ubuntu16.04LTS, Clion IDE with GCC version 5.4.0, C11 standard.

As I run the code, an error occurs with message

undefined reference to `cabs'

The IDE tells me that the function cabs is declared in the header file cmathcalls.h, so I try to:

#include<cmathcalls.h>

But the IDE warns me that the file cannot be found, so again I try:

#include<bits/cmathcalls>

The I run the code, however it still doesn't work.

I want to know how can I get the abs value of the complex z with the function cabs?

Wolf
  • 9,679
  • 7
  • 62
  • 108
  • try `gcc file.c -lm` – Uprooted Feb 07 '18 at 09:05
  • You write: _"As I run the code..."_. Do you rather mean _"when I compile the code..."_? – Jabberwocky Feb 07 '18 at 09:23
  • You should really have a closer look on the messages of compiler, linker etc. that your IDE hopefully displays somewhere. Here you can see *what exactly* fails. Simply hitting [Run] sometimes includes a build of the program. – Wolf Feb 07 '18 at 09:29
  • did you add the library to `CMakeLists.txt`? `target_link_libraries( m)` – uta Feb 20 '18 at 14:16

1 Answers1

6

For cabs you just need <complex.h>. Where it is actually declared is just an implementation detail.

And then link with -lm to actually link the math library in.