2

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??

Abdo MAD
  • 19
  • 1
  • 3

1 Answers1

2

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".

Jona
  • 1,218
  • 1
  • 10
  • 20
  • I use it in cs50 sandbox but i want to use it in VS code – Abdo MAD Aug 07 '20 at 07:47
  • You can try to put `cs50.c` in the same folder as `0.c` and execute `gcc 0.c cs50.c -o 0`. A more clean way is to first compile `cs50.c` to an object `cs50.o` and use that object in the compilation of `0.c`. – Jona Aug 07 '20 at 07:56
  • i did so and it worked for GetInt what does this mean ? i mean what is wrong with me linking the lib or whatever is called? also, is there a way to not put cs50.c in every folder I want to work in?((edit* i included cs50.c and removed it from the folder and it worked too , thanks)) and lastly if you can help me use get_int in vscode that would be great – Abdo MAD Aug 07 '20 at 08:10
  • Don't do `#include "cs50.c"` (you said "i included cs50.c"). It can give you bad surprises afterwards if you don't understand correctly what you are doing. – Jona Aug 07 '20 at 08:52
  • Frankly I don't understand . So is there an alternative for having to put in in the same folder and then compiling using it ?? – Abdo MAD Aug 07 '20 at 09:09
  • Instead of `gcc 0.c cs50.c -o 0`, you can execute `gcc 0.c //cs50.c -o 0`. If you want to do it in a better way, you should look for `Makefile`. – Jona Aug 07 '20 at 12:11