I need a complex library for some stuf in c++ or c.
So I found some useful tooltip in linux.
man complex
documentation have good example like this:
#include <math.h> /* for atan */
#include <stdio.h>
#include <complex.h>
int
main(void)
{
double pi = 4 * atan(1.0);
double complex z = cexp(I * pi);
printf("%f + %f * i\n", creal(z), cimag(z));
}
everything goes well...
But I took error every time which I tried.
> Executing task: /usr/bin/g++ -g '/home/max/Documents/c_expls/test2.cpp' -o '/home/max/Documents/c_expls/test2' <
/home/max/Documents/c_expls/test2.cpp: In function ‘int main()’:
/home/max/Documents/c_expls/test2.cpp:10:17: error: expected initializer before ‘z’
10 | double complex z = cexp(I * pi);
| ^
/home/max/Documents/c_expls/test2.cpp:11:32: error: ‘z’ was not declared in this scope
11 | printf("%f + %f * i\n", creal(z), cimag(z));
| ^
The terminal process "/bin/bash '-c', '/usr/bin/g++ -g '/home/max/Documents/c_expls/test2.cpp' -o '/home/max/Documents/c_expls/test2''" terminated with exit code: 1.
Terminal will be reused by tasks, press any key to close it.
I edited code a little bit, like adding double complex z etc.. but same machine... same error...
I think my gcc installation have lacks component. beause I tried Code::Blocks
Do you have an idea,
why my gcc doesn't know this declaration?
You need to compile with gcc and specify that you want to use the C99 standard or higher -std=c99.
For C++, use std::complex.
You need to link against the math library with the flag -lm
Using exactly your code in a file called test.c I compiled with:
gcc -o test test.c -lm
Running the binary gives the output:
./test
-1.000000 + 0.000000 * i
Related
Well, I think my problem is a little bit interesting and I want to understand what's happening on my Ubuntu box.
I compiled and linked the following useless piece of code with gcc -lm -o useless useless.c:
/* File useless.c */
#include <stdio.h>
#include <math.h>
int main()
{
int sample = (int)(0.75 * 32768.0 * sin(2 * 3.14 * 440 * ((float) 1/44100)));
return(0);
}
So far so good. But when I change to this:
/* File useless.c */
#include <stdio.h>
#include <math.h>
int main()
{
int freq = 440;
int sample = (int)(0.75 * 32768.0 * sin(2 * 3.14 * freq * ((float) 1/44100)));
return(0);
}
And I try to compile using the same command line, and gcc responds:
/tmp/cctM0k56.o: In function `main':
ao_example3.c:(.text+0x29): undefined reference to `sin'
collect2: ld returned 1 exit status
And it stops. What is happening? Why can't I compile that way?
I also tried a sudo ldconfig -v without success.
There are two different things going on here.
For the first example, the compiler doesn't generate a call to sin. It sees that the argument is a constant expression, so it replaces the sin(...) call with the result of the expression, and the math library isn't needed. It will work just as well without the -lm. (But you shouldn't count on that; it's not always obvious when the compiler will perform this kind of optimization and when it won't.)
(If you compile with
gcc -S useless.c
and take a look at useless.s, the generated assembly language listing, you can see that there's no call to sin.)
For the second example, you do need the -lm option -- but it needs to be at the end of the command line, or at least after the file (useless.c) that needs it:
gcc -o useless useless.c -lm
or
gcc useless.c -lm -o useless
The linker processes files in order, keeping track of unresolved symbols for each one (sin, referred to by useless.o), and then resolving them as it sees their definitions. If you put the -lm first, there are no unresolved symbols when it processes the math library; by the time it sees the call to sin in useless.o, it's too late.
Why does this compile and run this w/o the "-lm" switch on gcc:
#include <stdio.h>
#include <math.h>
int main()
{
printf("2 to the 8th power is %f\n",pow(2.0,8.0));
return(0);
}
but this:
#include <stdio.h>
#include <math.h>
int main()
{
float a,b;
a = 2.0;
b = 8.0;
printf("2 to the 8th power is %f\n",pow(a,b));
return(0);
}
gives the error:
undefined reference to `pow'
unless you link the math library with -lm
The behavior is the same if I use doubles rather than floats. Is there some sort of rudimentary pow() function hidden in standard library, or is the linker just resigned to working with idiots and links the math library for really simple cases?
I'm using gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 if that makes any difference. It's not a big deal, just curious why that happens, thanks!
I had myself, a problem of understanding, what is wrong with the above. I got the same behaviour with gcc -O0. Running executable with strace and gdb showed that no function call made at all.
Scratched my head, read some comments and got it. Me alone, would have guessed the answer for hours. All credits goes to commenters
Try to change your code to
....
double t = (int)(2.0 + 1.0) % 2 + 1.0;
double r = pow(2.0+t,8.0);
....
With -O0 flag you should hopefully get undefined reference to 'pow' message.
Well, I think my problem is a little bit interesting and I want to understand what's happening on my Ubuntu box.
I compiled and linked the following useless piece of code with gcc -lm -o useless useless.c:
/* File useless.c */
#include <stdio.h>
#include <math.h>
int main()
{
int sample = (int)(0.75 * 32768.0 * sin(2 * 3.14 * 440 * ((float) 1/44100)));
return(0);
}
So far so good. But when I change to this:
/* File useless.c */
#include <stdio.h>
#include <math.h>
int main()
{
int freq = 440;
int sample = (int)(0.75 * 32768.0 * sin(2 * 3.14 * freq * ((float) 1/44100)));
return(0);
}
And I try to compile using the same command line, and gcc responds:
/tmp/cctM0k56.o: In function `main':
ao_example3.c:(.text+0x29): undefined reference to `sin'
collect2: ld returned 1 exit status
And it stops. What is happening? Why can't I compile that way?
I also tried a sudo ldconfig -v without success.
There are two different things going on here.
For the first example, the compiler doesn't generate a call to sin. It sees that the argument is a constant expression, so it replaces the sin(...) call with the result of the expression, and the math library isn't needed. It will work just as well without the -lm. (But you shouldn't count on that; it's not always obvious when the compiler will perform this kind of optimization and when it won't.)
(If you compile with
gcc -S useless.c
and take a look at useless.s, the generated assembly language listing, you can see that there's no call to sin.)
For the second example, you do need the -lm option -- but it needs to be at the end of the command line, or at least after the file (useless.c) that needs it:
gcc -o useless useless.c -lm
or
gcc useless.c -lm -o useless
The linker processes files in order, keeping track of unresolved symbols for each one (sin, referred to by useless.o), and then resolving them as it sees their definitions. If you put the -lm first, there are no unresolved symbols when it processes the math library; by the time it sees the call to sin in useless.o, it's too late.
#include <stdio.h>
#include <math.h>
int exp_for_level(int n) {
return (int)(100 * pow(n, 2.3));
}
int main(){
int x;
x = exp_for_level(6);
printf("%d", x);
return 0;
}
I receive the following error when I run this code on an online compiler
/tmp/cc28S7ML.o: In function exp_for_level':
main.c:(.text+0x19): undefined reference to `pow'
collect2: error: ld returned 1 exit status
How do I rectify this?
After I couldn't get it to work on the online compiler, I followed advice from some other threads on
The file is stored under a file grades.c on my mac
I've tried entering this
$ gcc - Wall - lm -o grades grade . c
into my terminal and i just get zsh error: command not found
Any ideas on what the issue is here too?
The online compiler I'm using is
https://www.tutorialspoint.com/compile_c_online.php
EDIT: in my post, in main I'd miswritten the function as exp_to_level instead of exp_for_level. Didn't copy paste the entire code as it's too long. I narrowed it down and retyped it to the portion that yields the error.
There are some errors in your code, you have defined a function exp_for_level but you use exp_to_level.
Then your x variable is not defined
If you fix your code like this:
#include <stdio.h>
#include <math.h>
int exp_for_level(int n) {
return (int)(100 * pow(n, 2.3));
}
int main(){
int x = exp_for_level(6);
printf("%d", x);
return 0;
}
and you compile:
gcc -Wall powtest.c -o powtest -lm
it works.
About the error on the online compiler:
The undefined reference error occurs because you are missing -lm linker option.
Edit the online compiler command clicking on Project->Compile Options:
About this problem on your local machine:
After I couldn't get it to work on the online compiler, I followed
advice from some other threads on The file is stored under a file
grades.c on my mac I've tried entering this
$ gcc - Wall - lm -o grades grade . c
into my terminal and i just get zsh error: command not found
you don't have the compiler installed.
You should install clang, Have a look to this question
First of all your function name is wrong in the main take a look here exp_for_level
and in main its exp_to_level change one of them then also add int x in main to solve the issue.
The source code of square.c is:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int square(int *ptr)
{
int a;
a = *ptr;
return a * a;
}
int main(int argc, char **argv)
{
int a, aa;
srandom(time(NULL));
a = random() % 10 + 1;
aa = square(&a);
printf("%d\n", aa);
return 0;
}
The command-line to compile the source code is:
gcc square.c -o square
Is it possible to run the square executable in Linux so that the printed value will not be a square of any integer number?
Any method of running the program is allowed.
Yes. We can override printf.
Write the code in your post into square.c and compile it with gcc square.c
Make this file, fakesquare.c
int printf(char *str,int i)
{
return puts("7");
}
Compile fakesquare.c as a shared library:
gcc -fPIC -o libfakesquare.so -shared fakesquare.c
Run the square program with libfakesquare.so preloaded:
[15:27:27 0 /tmp] $ LD_PRELOAD=./libfakesqare.so ./a.out
7
[15:29:16 0 /tmp] $ LD_PRELOAD=./libfakesqare.so ./a.out
7
[15:29:16 0 /tmp] $ LD_PRELOAD=./libfakesqare.so ./a.out
7
Witout libfakeshared.so preloaded:
[15:29:40 0 /tmp] $ ./a.out
36
[15:29:41 0 /tmp] $ ./a.out
16
[15:29:42 0 /tmp] $ ./a.out
64
You could use this :
Fastest way to determine if an integer's square root is an integer
Their code seems optimized, but whichever is simplest should do the trick for you.
The only dependency at your code is libc. If libc stays unmodified then your code will always work.
Also your program will fail if before running it, all available memory is exhausted. You can always check if ptr!=NULL.
Assuming a standard C environment I don't see a reason why this should fail on a standard platform. The code might fail if printf is not doing what it is inteded to do, but probably this is not what you are asking for. It also might fail on a platform where int is as small as a byte and a byte is only 6 bits wide. In this case your square function might calculate 9*9=81 which will not fit in the result type int (0..63 for 6 bit-byte). But in my opinion this is a quite academic case.