EDIT My problem is solved Thanks all very much
the problem was that I only included cs50.h but didn't include cs50.c and that the library I had was an old one containing only GetInt but not get_int
when I downloaded the new library everything worked
I'm taking CS50x course and I want to use get_int function which is included in cs50 library on VS code ...
I downloaded cs50 library and copied cs50.h and cs50.c to d:\MinGW\bin
my code is
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int age = get_int("Age?");
int days=age*365;
printf("Your age is %i which means that you are %i days old", age, days);
}
when I try to compile it using
gcc 0.c -o 0
it writes
0.c: In function 'main':
0.c:7:15: warning: implicit declaration of
function 'get_int' [-Wimplicit-function-declaration]
7 | int age = get_int("Age?");
| ^~~~~~~
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\AbdoMAD\AppData\Local\Temp\ccTefKbe.o:0.c:(.text+0x15): undefined reference to `get_int'
collect2.exe: error: ld returned 1 exit status
the auto complete of vs code doesn't have get_int but it has GetInt
But when I use it and the code is
#include <stdio.h>
#include <cs50.h>
int main(void)
{
printf("Age?")
int age = GetInt();
int days=age*365;
printf("Your age is %i which means that you are %i days old", age, days);
}
it returns
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\AbdoMAD\AppData\Local\Temp\cc3NVsiz.o:1.c:(.text+0x1a): undefined reference to `GetInt'
collect2.exe: error: ld returned 1 exit status
What should I do to use get_int or at least GetInt in VS code??
If you want get_int, don't write it GetInt.
If you use https://sandbox.cs50.io, you can do:
gcc 0.c -lcs50 -o 0
and you first code will work.
To get more information, try googling "c link to library".
Related
Code:
#include <stdio.h>
#include "cs50.h"
int main(void)
{
string answer = get_string("What's your name? ");
printf("Hello, %s\n", answer);
}
I'm doing a course on edX called CS50's Introduction to Computer Science and in week 1 lecture 1 I have to write some code that asks the user what his/her name is and says "Hello" with the name after it, I've been fixing this for over 2 hours now wasting away time. I downloaded the cs50 library and put it into my vscode.
VScode files
Here's the syntax error too.
PS C:\Users\Felip\Documents\Microsoft VS Code\New Code> cd "c:\Users\Felip\Documents\Microsoft VS Code\New Code\CS50IntroClanguage\" ; if ($?) { gcc name.c -o name } ; if ($?) { .\name }
C:\Users\Felip\AppData\Local\Temp\ccYgzUcR.o:name.c:(.text+0x1e): undefined reference to `get_string'
collect2.exe: error: ld returned 1 exit status
PS C:\Users\Felip\Documents\Microsoft VS Code\New Code\CS50IntroClanguage>
You need #include <cs50.h> instead of #include "cs50.h". "" means that the file you're including is in the same folder as your code. <> Means that it's installed on the system.
#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 am trying to compile a c program with a static library and its not working .
This is the error :
undefined reference to `calculatearea'
collect2.exe: error: ld returned 1 exit status .
The static files were made with the gcc / g++ compilers .
This is the main code :
#include <stdio.h>
#include <stdint.h>
int calculatearea(int a , int b);
int main()
{
int c = calculatearea(2,4);
printf("%d",c);
getchar();
return 0;
}
edit :
: screenshot of compiler error
From the above code we can see that you have declared the function int calculatearea(int a , int b); but have not written any definition for the same. and you are calling this function in the main. compiler is not finding the definition for the function calculatearea and giving error.
To solve this:
1) Write the definition for function calculatearea in the same file.
2) Make use of extern specifier with this function declaration and make sure that definition is present with the link library at the time of compilation.
3) As mentioned in the picture if the area.o have the definition of function calculatearea, then compile as below, this will generate a.out in linux:
gcc filename.c area.o
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
When I compile this code, I get the following gcc errors:
/tmp/ccUigsI6.o: In function `main':
/home/matt/Dropbox/school/2011/cs3210/test/sizeterm.c:9: undefined reference to `setupterm'
/home/matt/Dropbox/school/2011/cs3210/test/sizeterm.c:10: undefined reference to `tigetnum'
/home/matt/Dropbox/school/2011/cs3210/test/sizeterm.c:11: undefined reference to `tigetnum'
collect2: ld returned 1 exit status
make: *** [sizeterm] Error 1
Here's the code:
#include <stdio.h>
#include <term.h>
#include <curses.h>
#include <stdlib.h>
int main()
{
int nrows, ncolumns;
setupterm(NULL, fileno(stdout), (int *)0);
nrows = tigetnum("lines");
ncolumns = tigetnum("cols");
printf("This terminal has %d columns and %d rows\n", ncolumns, nrows);
exit(0);
}
Libncurses is installed correctly on my machine. I get the same results from my Arch linux laptop, and the Ubuntu server installed at my school. This particular piece of code is taken directly out of the book. Am I doing something wrong? I've done some googling and it looks as though people have had this problem before, but I can't narrow down a solution.
You forgot to actually link against ncurses. Add -lcurses to the gcc command line.
This is exactly what you find in the same book as where you found this code:
$ cc -o badterm badterm.c -lncurses
Beginning linux programming 4th edition, chapter 5: Terminals, page 196.