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.
Related
#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.
I was testing the cblas ddot, and the code I used is from the link and I fixed it as
#include <stdio.h>
#include <stdlib.h>
#include <cblas.h>
int main()
{
double m[10],n[10];
int i;
int result;
printf("Enter the elements into first vector.\n");
for(i=0;i<10;i++)
scanf("%lf",&m[i]);
printf("Enter the elements into second vector.\n");
for(i=0;i<10;i++)
scanf("%lf",&n[i]);
result = cblas_ddot(10, m, 1, n, 1);
printf("The result is %d\n",result);
return 0;
}
Then when I compiled it, it turned out to be:
/tmp/ccJIpqKH.o: In function `main':
test.c:(.text+0xbc): undefined reference to `cblas_ddot'
collect2: ld returned 1 exit status
I checked the cblas file in /usr/include/cblas.h, and noticed there is
double cblas_ddot(const int N, const double *X, const int incX,
const double *Y, const int incY);
I don't know where it is going wrong. Why does the compiler said the "cblas_ddot" is undefined reference?
You can't just include the header - that only tells the compiler that the functions exist somewhere. You need to tell the linker to link against the cblas library.
Assuming you have a libcblas.a file, you can tell GCC about it with -lcblas.
The web site for GNU Scientific Library tells you how to do this:
2.2 Compiling and Linking
My problem was just solved. The reason is that I made a mistake when inputed the link path. Thanks for Jonathon Reinhart's answers, they are really helpful when learning how to code in linux.
The compile commands are:
gcc -c test.c
gcc -L/usr/lib64 test.o -lgsl -lgslcblas -lm
Where "/usr/lib64" is the correct link path.
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.
I'm don't seem to be able to generate random number in C under Ubuntu 12.04.
I wrote the fallowing code:
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
int main (int argc,char* argv[])
{
int number;
clear();
number = rand() % 2; // want to get only 0 or 1
printf("%d",number);
getch();
return 0;
}
I named the file "test_gcc.c".
After that I compile it with:
$ sudo gcc -o test_gcc test_gcc.c
And i get the following message:
/tmp/ccT0s12v.o: In function `main':
test_gcc.c:(.text+0xa): undefined reference to `stdscr'
test_gcc.c:(.text+0x12): undefined reference to `wclear'
test_gcc.c:(.text+0x44): undefined reference to `stdscr'
test_gcc.c:(.text+0x4c): undefined reference to `wgetch'
collect2: ld returned 1 exit status
Can somebody tell me what did I do wrong?
And also how to generate random number in C on Ubuntu 12.04 using gcc?
Thanks in advance!
This has nothing to do with random numbers. The problem is that you're linking without the curses library.
You need to add -lncurses to your gcc command line:
$ gcc -o test_file test_file.c -lncurses
You didn't seed the random number generator. <-- Not the reason for errors
Use srand(time(0)); before calling rand().
Use srand ( time(NULL) ); before number = rand() % 2; to get different random number every time the executable is ran.
For errors:
remove clear() and use getchar() instead of getch() and then it
should worked fine.
getch() is used in compilers that support un-buffered input, but in
case of gcc it's buffered input so use getchar().
code:
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
int main (int argc,char* argv[])
{
int number;
srand(time(NULL));
number = rand() % 2; // want to get only 0 or 1
printf("%d",number);
getchar();
return 0;
}
Try :
gcc -o test_gcc test_gcc.c -lncurses
I want to make a simple function involving sqrt(), floor() and pow(). So, I included <math.h>. When I try to use my function, my program says that sqrt() and floor() do not exist. I've triple checked my files and rewritten them, but it still gives the same error. Just to check if there was anything wrong with the <math.h> directory, I made another separate file that calculated the same thing and it worked. I am clueless right now. What am I doing wrong?
The code of the non functioning program:
#include <math.h>
#include "sumofsquares.h"
int sumofsquares(int x){
int counter = 0;
int temp = x;
while(temp != 0){
temp = temp - (int)pow(floor(sqrt(temp)), 2);
counter ++;
}
return counter;
}
The working test file:
#include <stdio.h>
#include <math.h>
int main(void){
printf("%d", (int)pow(floor(sqrt(3)), 2));
}
the error is this
/tmp/ccm0CMTL.o: In function sumofsquares':
/home/cs136/cs136Assignments/a04/sumofsquares.c:9: undefined reference
to sqrt' /home/cs136/cs136Assignments/a04/sumofsquares.c:9: undefined
reference to floor' collect2: ld returned 1 exit status`
I am using runC on a virtual Ubuntu OS to compile
You're probably missing the -lm argument to gcc, required to link the math library. Try:
gcc ... <stuff> ... -lm
There are at least two C FAQs relevant to your problem:
14.3
13.26