How do I compile a cilk program? - c

I installed Cilk using the instructions from their website.
sudo apt-add-repository ppa:wsmoses/tapir-toolchain
sudo apt-get update
sudo apt-get install tapirclang-5.0 libcilkrts5
I copied the following program from the Cilk documentation.
#include <stdio.h>
#include <stdint.h>
int64_t fib(int64_t n) {
if (n < 2) return n;
int x, y;
x = cilk_spawn fib(n - 1);
y = fib(n - 2);
cilk_sync;
return x + y;
}
int main(){
printf("%ld\n", fib(20));
}
I then compiled using the compiler flag that they specified.
clang-5.0 -fcilkplus Fib.c
Fib.c:7:9: error: use of undeclared identifier 'cilk_spawn'
x = cilk_spawn fib(n - 1);
^
Fib.c:9:5: error: use of undeclared identifier 'cilk_sync'
cilk_sync;
^
The desired output is a working executable that uses Cilk and prints 6765.
What magic incantations are needed to produce this executable?
I am running Ubuntu 18.04 with kernel 4.4.0-45-generic.

Related

C: undefined reference to 'WinMain#16'

I'm very new to coding and I have been trying to write code to adds two integers. But whenever I try to run it using 'gcc addition.c' in the terminal I always reports an error. I tried reinstalling the compiler i.e Mingw several times but the problem does not gets fixed.
(I m currently doing C language on VS CODE software, when you answer to my issue please use layman language)
#include <stdio.h>
int main() {
int x=1;
int y=2;
int z=0;
z=x+y;
printf("%d", z);
return 0;
}
Windows PowerShell
PS D:\C tutorials> gcc addition.c
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to "WinMain#16' collect.exe: error: ld returned 1 exit status
I added a \n to clean up the printf().
#include <stdio.h>
int main(void) {
int x=1;
int y=2;
int z=0;
z=x+y;
printf("%d\n", z);
return 0;
}
% gcc -o addition addition.c -lc ; ./addition
3
You needed to include the C library, represented by the -lc in the gcc line.

Gcc doesn't recognise 'double complex' decleration

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

OSX 10.12 GCC OpenMP Error

Have been trying to compile and run a simple OpenMP program (Hello World) on OSX 10.12. I installed gcc 6 using brew.
I have been building using the '-fopenmp' flag.
The program compiles fine, but when I try to execute the program I get the following:
dyld: lazy symbol binding failed: Symbol not found: ___emutls_get_address
Referenced from: /usr/local/opt/gcc/lib/gcc/6/libgomp.1.dylib (which was built for Mac OS X 10.12)
Expected in: /usr/lib/libSystem.B.dylib
dyld: Symbol not found: ___emutls_get_address
Referenced from: /usr/local/opt/gcc/lib/gcc/6/libgomp.1.dylib (which was built for Mac OS X 10.12)
Expected in: /usr/lib/libSystem.B.dylib
Abort trap: 6
Any ideas?
Here's an example that runs on my iMac with g++-6, compiled as follows:
g++-6 -std=c++11 -fopenmp -O3 demo.cpp -o demo
Run as follows:
./demo
Time: 4.132ms, 1000000 elements.
Code as follows:
#include "omp.h"
#include <iostream>
#include <cmath>
#include <cstdio>
#include <chrono>
int main()
{
const int size = 1000000;
int i;
double sinTable[size];
std::chrono::time_point<std::chrono::high_resolution_clock> start = std::chrono::high_resolution_clock::now();
#pragma omp parallel for
for(int n=0; n<size; ++n)
sinTable[n] = sqrt(std::sin(2 * M_PI * n / size));
std::chrono::time_point<std::chrono::high_resolution_clock> end = std::chrono::high_resolution_clock::now();
std::uint64_t duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count();
float ms=duration/1000000.0;
std::cout << "Time: " << ms << "ms, " << size << " elements." << std::endl;
}

Compile and run a C bit operation file

I am working on this bitwise operators in C program.i need to compile and run the program to check the output is working correct or not . what compiler i need to use to run and execute the program and what is the command to execute this C program:
enter code here
#include <stdio.h>
#include "bitslab.h"
int main()
int bitAnd(int x, int y)
int getByte(int x, int n)
int logicalShift(int x, int n)
{
printf("bitAnd Result: %d\n", bitAnd(15,3));
printf("getByte Result: %d\n", getByte(20,4));
printf("logicalShift Result: %d\n", logicalShift(12,4));
return ~((~x)|(~y));
return (x>>(n<<3))&0xff;
return ((x >> n) & ((1 << ((~n + 1) + 32)) + ~0));
}
On linux flavours use GCC to compile your program.
http://www.wikihow.com/Compile-a-C-Program-Using-the-GNU-Compiler-(GCC)
On windows you can try turbo-C or visual C++.
I guess your program is not complete and I fear it would not run properly.
Enjoy debugging and happy coding.

C programming - "Undefined symbol referenced in file"

I am trying to write a program to approximate pi. It basically takes random points between 0.00 and 1.00 and compares them to the bound of a circle, and the ratio of points inside the circle to total points should approach pi (A very quick explanation, the specification goes in depth much more).
However, I am getting the following error when compiling with gcc:
Undefined first referenced
symbol in file
pow /var/tmp//cc6gSbfE.o
ld: fatal: symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
What is happening with this? I've never seen this error before, and I don't know why it's coming up. Here is my code (though I haven't fully tested it since I can't get past the error):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
float x, y;
float coordSquared;
float coordRoot;
float ratio;
int n;
int count;
int i;
printf("Enter number of points: ");
scanf("%d", &n);
srand(time(0));
for (i = 0; i < n; i++) {
x = rand();
y = rand();
coordSquared = pow(x, 2) + pow(y, 2);
coordRoot = pow(coordSquared, 0.5);
if ((x < coordRoot) && (y < coordRoot)) {
count++;
}
}
ratio = count / n;
ratio = ratio * 4;
printf("Pi is approximately %f", ratio);
return 0;
}
use -lm during compilation(or linking) to include math library.
Like this: gcc yourFile.c -o yourfile -lm
need to Link with -lm.
gcc test.c -o test -lm
The error is produced by the linker, ld. It is telling you that the symbol pow cannot be found (is undefined in all the object files handled by the linker). The solution is to include the library which includes the implementation of the pow() function, libm (m for math). [1] Add the -lm switch to your compiler command line invocation (after all the source file specifications) to do so, e.g.
gcc -o a.out source.c -lm
[1] Alternatively, you could have your own implementation of pow() in a separate translation unit or a library, but you would still have to tell the compiler/linker where to find it.

Resources