I'm new to programming and a bit confused with arrays, what seems to be wrong in this code, as eclipse output console is saying ** Build of configuration Debug for project Project **
Internal Builder is used for build **
gcc -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ..\main.c
gcc -oProject.exe main.o
C:...\Documents\eclipse\mingw\bin..\lib\gcc\mingw32\3.4.5........\mingw32\bin\ld.exe: cannot open output file Project.exe: Permission denied
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 472 ms.
I will really appreciate your help...
int main()
{
int box[2][2], rows, cols, x = 1;
for (rows=0; rows < 2; rows++)
{
for (cols=0; cols < 2; cols++)
{
box[rows][cols] = x++;
printf("%d", box[rows][cols]);
}
}
fflush(stdout);
getch();
return 0;
}
x++ is post increment so the value of x is used and then incremented and hence box[0][0] is 1
Related
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.
I have already taken a look at the question :Undefined reference to `pow' and `floor' but I'd like to know if there is anyway I could fix this permanently in my IDE which is Jet brains C Lion. I am using it on Pop OS. And I am very new to C as well as programming in general, it'd be better if I could just build and compile the program from my IDE rather than having to go to terminal every time.
Edit #1 : This is the code I am trying to compile rn. Its trying to find the reverse of a number rn.
#include <stdio.h>
#include <math.h>
int how_long(int x)
{
int count = 0;
while (x>0)
{
x /= 10;
count++;
}
return count;
}
int main()
{
int n = 1;
int num ;
int reverse_num = 0;
printf("Enter a number : ");
scanf("%i",&num);
int j = how_long(num);
while (num>0)
{
reverse_num += (num % 10) * (int)pow(10,j) ;
num -= (num%10) * (int)pow(10,n-1);
++n;
--j;
}
printf("%i",reverse_num);
return 0;
}
And the error I get on C Lion is :
[1/1] Linking C executable 5_2
FAILED: 5_2
: && /usr/bin/cc -g CMakeFiles/5_2.dir/main.c.o -o 5_2 && :
/usr/bin/ld: CMakeFiles/5_2.dir/main.c.o: in function `main':
/home/koustubhjain/CLionProjects/Assignments/5-2/main.c:30: undefined reference to `pow'
/usr/bin/ld: /home/koustubhjain/CLionProjects/Assignments/5-2/main.c:31: undefined reference to `pow'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
Edit #2 :
I found this old forum post on C Lion's website : https://intellij-support.jetbrains.com/hc/en-us/community/posts/206607085-CLion-Enabling-math-h-for-C-projects
but idk if it still works, and if it works, how do I add that to my CMake file ?
Here is my CMakeLists.txt file for reference :
cmake_minimum_required(VERSION 3.21)
project(5_2 C)
set(CMAKE_C_STANDARD 99)
add_executable(5_2 main.c)
Edit #3 : Edit #2 seems to have fixed it
#include<stdio.h>
int main()
{
int a , b;
a = 2;
b = 3;
printf("%d , %d", a , b);
return 0;
}
The output comes out to be ->
PS E:\Coding\C\C C++> cd "e:\Coding\C\C C++" ; if ($?) { gcc hello.c -o hello } ; if ($?) { .\hello }
4214884
Why is this happening? Is there something wrong with the compiler?
The code seems okay, it should give the correct output.
After saving the code, compile the program again.
gcc file_name_with_extension
It'll create an execitable file. Then run that file by simply giving the name of the executable file without any extension.
Having a Major issue with trying to do a multi-threading project for school
I'm just trying to run sample code to see how pthreads work
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int sum;
void *runner(void *param);
int main(int argc, char *argv[]) {
pthread_t tid;
pthread_attr_t attr;
if (argc !=2){
fprintf(stderr, "usage: a.out <interger value>\n");
return -1;
}
if (atoi(argv[1]) < 0) {
fprintf(stderr, "%d must be >= 0\n",atoi(argv[1]));
return -1;
}
pthread_attr_init(&attr);//get default attributes
pthread_create(&tid,&attr,runner,argv[1]);//create the thread
pthread_join(tid,NULL);//wait for the thread to exit
printf("sum = %d\n",sum);
}
void *runner(void *param) {
int i, upper = atoi(param);
sum = 0;
for (i =1; i <= upper; i++) {
sum += i;
}
pthread_exit(0);
}
And no matter what I do I can't get the code to compile. I get the following error:
Running "/home/ubuntu/workspace/primefactor/assn3.c"
/tmp/cc58AE5c.o: In function 'main':
assn3.c:(.text+0xc4): undefined reference to 'pthread_create'
assn3.c:(.text+0xd5): undefined reference to 'pthread_join'
collect2: error: ld returned 1 exit status
Process exited with code: 1
I've tried writing my own makefile with the -pthread flag; didn't work....
I've tried running
sudo apt-get install libpthread-stubs0-dev
to install the right library; didn't work...
I'm at my wits end to try and get pthreads_create() and pthreads_join() to work and they're the main requirement for my school project. I've already gotten the project working with a normal C program (take numbers from the command line, get the prime factors for each arg, store them in an array, then pass that array and display the results. ex: ./prime.c.o {1..100} would be run from BASH and it outs 1:1 2: 2 3: 3 4: 2 2 etc...
so that's what the output is supposed to be and I'm supposed to do that with threading, but I can't do that if I can't even compile sample code to see how threads work.
I really have already spent several hours searching, trying, but no luck getting rid of these undefined reference errors for pthread_create() and pthread_join() . Any help would be greatly appreciated in figuring out why I get the above errors.
Edit:
Thanks to those that tried answering for me. I don't know why I got the down vote; I'm not very active here and I do try to be polite when asking for help. Anyways I finally figured it out.
If I am using the make file it had to look like this:
assn3:
gcc -g -Wall -pthread assn3.c -o assn3
Then from the command line: make assn3
It appears you haven't specified to the linker about thread library. For multithreading applications, the usual option for gcc is follows (assume your program file name thread1.c):
gcc -D_REENTRANT -lpthread thread1.c -o thread1
It's also recommended to use -Wall -O3 -pedantic-errors to be pedantic about compiler warnings etc. So this should do:
gcc -Wall -O3 -pedantic-errors -D_REENTRANT -lpthread thread1.c -o thread1
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.