Home > other >  trouble implementing cs50 library for C in vs code
trouble implementing cs50 library for C in vs code

Time:06-22

I am new to programming and have been following the cs50 course. I am using Windows and I am not able to #include "cs50.h". Here's my code:

#include <stdio.h>
#include "cs50.h"

int main(void)
{
    string answer = get_string("What's your name? ");
    printf("hello, answer\n");
}

Compiling gives the error

whatsyourname.c:2:18 fatal error: cs50.h: No such file or directory
 #include "cs50.h"
                  ^

compilation terminated.

I have downloaded the files cs50.h and cs50.c and created a pathway for them. I am using GCC. It now seems that the cs50.h library was recognized in my code as it is now colored. I also have a .sh file from a YouTube video. I am very lost and have no clue what I am doing or what I have already done.

Also, I forgot to add, but this is my.sh file

this

CodePudding user response:

It seems like you didn't install the cs50 library.

Have at a look at the GitHub repository of the cs50 lib. In the src directory, you will find the actual code and header file of the module.

I'm not sure if you can make the automated building process work on Windows, but you can always build the library yourself.

First download the repository, and place both the files under src in the folder which your code resides.

Then, you have to build the module. You can either just compile and use the object file whenever you need it:

gcc -c cs50.c
gcc main.c cs50.o # main.c contains your code

or recompile it each you compile your actual code:

gcc whatsyourname.c cs50.c

CodePudding user response:

The error you are seeing is the compiler telling you that the file you're trying to include doesn't exist, or can't be seen.

Your code says #include "cs50.h" but there is no cs50.h file in your directory. To fix this, add the cs50.h file in the same folder as your code (week 1). You probably need to download this file from the course materials.

EDIT:
Also, make sure that you're not confusing .sh files and .h files.

  • .sh files are shell scripts (as @Gerhardh points out in his comment, yours appears to be to compile the code).
  • .h files are headers for C code.
  • Related