libcs50 icon indicating copy to clipboard operation
libcs50 copied to clipboard

How to use cs50.h in my local VSCode environment?

Open timeneverdie opened this issue 6 years ago • 43 comments

Okay, I am a fresh to learn C language.

I install gcc and can run *.c file successfully on my computer.

But I find I need cs50 library to let get_string work. I try to extract the single file cs50.h to the code folder and write #include "cs50.h" in the file but the get_string still doesn't work.

Bug information is: undefined reference to get_string collect2.exe: error: ld returned 1 exit status

Must I follow the whole steps to install cs50 library into my system? Any green way to do that? Like Python can import *.py file directly, C language doesn't support this way?

timeneverdie avatar Nov 04 '19 08:11 timeneverdie

Include the flag -lcs50 when you compile the code.

e-eight avatar Nov 06 '19 21:11 e-eight

Include the flag -lcs50 when you compile the code.

error is cannot find -lcs50

I have added both cs50.c and cs50.h into the include folder of gcc.

timeneverdie avatar Nov 07 '19 05:11 timeneverdie

Try the troubleshooting steps mentioned in the README file on the master branch.

e-eight avatar Nov 08 '19 14:11 e-eight

you need to include cs50.c in your folder and compile both files together on command line gcc -o out appname.c cs50.c

devahmedmagid avatar Jul 13 '20 16:07 devahmedmagid

I was able to get mine working on my Mac with Visual Studio Code as follow: Assuming you have Visual Studio already installed and with all C/C++ extensions required for compiling and running C codes,
1a. Download the CS50 Library from https://github.com/cs50/libcs50/releases

1b. Extract/unzip the downloaded zipped folder

2a. Open Terminal and change directory to the unzipped CS50 library folder. For example if folder is on desktop and the folder is named libcs50-10.1.0 , then terminal command should be: cd desktop/libcs50-10.1.0

2b. Still in terminal after changing directory, then run the command: sudo make install

3a. After installation, close terminal, go to visual studio and write your C program #include <cs50.h>

3b. Link cs50 when compiling your code with clang. Example if your file name is hello.c clang hello.c -o hello -lcs50

  1. run your code

JeromeP93 avatar Sep 15 '20 22:09 JeromeP93

I was able to get mine working on my Mac with Visual Studio Code as follow: Assuming you have Visual Studio already installed and with all C/C++ extensions required for compiling and running C codes, 1a. Download the CS50 Library from https://github.com/cs50/libcs50/releases

1b. Extract/unzip the downloaded zipped folder

2a. Open Terminal and change directory to the unzipped CS50 library folder. For example if folder is on desktop and the folder is named libcs50-10.1.0 , then terminal command should be: cd desktop/libcs50-10.1.0

2b. Still in terminal after changing directory, then run the command: sudo make install

3a. After installation, close terminal, go to visual studio and write your C program #include <cs50.h>

3b. Link cs50 when compiling your code with clang. Example if your file name is hello.c clang hello.c -o hello -lcs50

  1. run your code

that is great but i am running windows is there any way i can add cs50 library in windows visual studio code

pnaskardev avatar Sep 27 '20 12:09 pnaskardev

@pnaskardev you need to link against the library single time. I assume you're using the integrated terminal within the Visual Studio Code.

For that, you need to make sure you have gcc, g++, or clang setup on your Windows machine. Please look into MinGW for that. Also, do setup the cs50 properly as given in the instructions. Then, just as the comment you are referring to, change directory to where you have the .c file that you want to compile against, and run gcc/g++/clang my_file.c -o my_file -lcs50.

Remember to close this issue if this works. Best of luck!

Rubix982 avatar Sep 29 '20 06:09 Rubix982

you need to include cs50.c in your folder and compile both files together on command line gcc -o out appname.c cs50.c

On windows, maybe you are the only right answer! It worked.

However, do you know how to let it work with C/C++ extension in VSCode? So I can use debugger.

I guess there is a general solution to add head files as default like stdio.h, but I don't know how to do it, especially on Windows.

TIP: how to use that way?

  1. Copy both cs50.c and cs50.h in the same folder with the file you want to compile *** or you can just copy the full path of cs50.h and write it in #include "full path"
  2. write #include "cs50.h" at the beginning of the file
  3. Compile with gcc -o out yourfile.c cs50.c *** or you can just use full path of cs50.c, like gcc -o out yourfile.c <full path>

It works fine as the old school way: compile it and then run it. The only inconvenience is debugging. I would like to use online cs50 IDE to debug, or use the old school way of printf().

We can edit the json file of cpptool extension to let it work to automatically compile and debug, but I don't know how to edit the json file.

timeneverdie avatar Nov 23 '20 10:11 timeneverdie

OK guys, finally I solve it totally!

To give you motivation first:

image

You can have highlight and intelligent code completion when you are coding. (Notice string and get_string() are highlighted)

image

And also, you can easily debug just with one F5 key!

Most important thing is that as beginners, we should code by Text Editors. Though IDEs are convenient and industry-friendly, but in many situations Text Editors are better.

The popular Text Editor powered by Microsoft is Visual Studio Code (VSCode), it can be enhanced by extensions to become an IDE. And also, it supports many programming languages throughout CS50 course and your further career. A very good tool to learn and use!


Please follow every step to make it work on Windows:

If you are on Linux or Mac, you can check README.md (https://github.com/cs50/libcs50/blob/develop/README.md) to get installation guide. If you want to know the generally manually installing method on Linux, the usage is the same as on Windows. You just need cs50.h and cs50.c, no need to install any other libraries, even though installation script on Linux is easier than on Windows. If you try to test my method on Linux manually, make sure you have installed gcc and gdb (sudo apt install gcc gdb -y)

First, you need to successfully build standard C libraries on your Windows. To do so, you need:

  • Install VSCode
  • Install C/C++ extension by Microsoft (search on in VSCode store)
  • Install Mingw64 and set PATH (Please read other tutorials)

    After finishing installation, you need to type in Powershell with command gcc to test if GCC Compiler is ready.

  • Open a Workspace in VSCode

    In fact, Workspace is just a folder. But this is very important and it's a must-master skill to use VSCode. To open a Workspace, click File -> Open a folder at the top menu bar

  • Create a hello_world.c file from left file tree panel

    You must first create a *.c file to enable highlight and intelligent. Have a good habit like this.

  • Save and open Terminal. Use gcc -o hello_world hello_world.c to build the hello_world.exe

    To open Terminal, you can use shorts Ctrl+~, or just move your cursor at the bottom of VSCode and then click and drag upwards to pull the Terminal out. Please notice that Terminal has many tabs.

  • run hello_world.exe ( .\hello_world in Terminal) to make sure it work

Now you have a very healthy environment for C on your Windows. Next, we focus how to use cs50.h and cs50.c.

First, we need to test if cs50.h and cs50.c work properly:

  • Download the zip from Github Release (https://github.com/cs50/libcs50/releases). Unzip it, locate to libcs50/src/, you can get cs50.h and cs50.c.
  • Copy cs50.h and cs50.c in the Workspace
  • Create and save a C file which uses cs50 libraries like string type and get_string() in the Workspace. We call it string.c.

    string.c should be with cs50.h and cs50.c in the Workspace. To use cs50.h in your file, please write #include "cs50.h" (pay attention to the quote mark).

  • Use gcc -o string string.c cs50.c to compile
  • Run string.exe (.\string in Terminal) to make sure it work

Next, we should make this work automatically and active debugger:

First, let's test if standard C libraries compiling and debugging will work:

  • Open hello_wolrd.c from left file tree panel
  • Press F5 on your keyboard
  • VSCode will guide you to choose: you should choose GCC or GDB keyword twice
  • Now you have two files under your Workspace named as .vscode folder
  • hello_world.exe should run properly in Terminal automatically by VSCode

Then we need change a little bit something to default settings of C/C++ extension:

  • Open task.json file from VSCode's left file tree panel

  • Edit args like below (// is my comment)

    Every keyword in args will work the same like gcc -o string string.c cs50.c (recall Command-line Argument), so we just edit keyword to let it work as we type in the command line.

          "args": [
              "-g",
              "${file}",
              "${fileDirname}\\cs50.c",    // add this line. Must use "\\" to have correct path. Must add ',' mark in the end of line.
              "-o",
              "${fileDirname}\\${fileBasenameNoExtension}.exe"
          ],
    
  • Save the task.json file

  • Open string.c from left file tree panel

  • Press F5 on your keyboard

    F5 will save the file automatically, so you don't need press Ctrl + S any more.

Now the F5 should work properly to build with cs50.c (cs50.h included). If not, please open task.json file again and then edit type keyword:

         "type": "cppbuild"    =>    "type":"shell"

I don't know if this is a bug, but "cppbuild" really occurs error in my experience even though your args keywords are correct. The other useful suggestion is to make sure your Workspace path doesn't have any space. For example, D:\my code should be D:\my_code.

Final Tip: If you are tired copying cs50.h and cs50.c every time between folders when you create many projects, you can consider full path of these two files.

Because the tutorial above only works when your *.c file, cs50.h, cs50.c at the same directory.

When you want to include the head file, use #include "D:\my_code\cs50.h". When you want to debug, change the args in task.json file, but you should notice that the path use double , not single. For example, "D:\\my_code\\cs50.c".

Enjoy it and enjoy CS50!

Not only useful in CS50. You can use the same method to build multiple C files wrote by you in the future. So it's a good choice to spend your time learning it!

timeneverdie avatar Dec 02 '20 11:12 timeneverdie

@timeneverdie , this answer is so good, I propose we add this somewhere in the README.md. Absolutely fantastic job! This is really interesting.

Rubix982 avatar Dec 02 '20 13:12 Rubix982

"args" : // end of the file expected error

thecrazydevil avatar May 14 '21 04:05 thecrazydevil

You saved me a lot. Thank you very much🙏

Gideon00 avatar May 25 '21 00:05 Gideon00

I took the exact steps and successfully built hello.c but string.c is giving me an error: undefined reference to 'get_string'. Can someone please help

Haidarahmd avatar Jul 10 '21 00:07 Haidarahmd

@timeneverdie

Haidarahmd avatar Jul 10 '21 00:07 Haidarahmd

I took the exact steps and successfully built hello.c but string.c is giving me an error: undefined reference to 'get_string'. Can someone please help

Take it easy. I recommend you keep going the course and you will learn what is command line argument and what is scanf(), pointer etc. And then you will find you can config your VS Code by yourself much easier than before.

timeneverdie avatar Jul 10 '21 04:07 timeneverdie

@timeneverdie

same problem : undefined reference to get_int' undefined reference to get_string' collect2.exe: error: ld returned 1 exit status

would you please suggest how to solve this issue?

ES208 avatar Aug 18 '21 09:08 ES208

@timeneverdie it seems pressing F5 to run the code (without using code runner extension) is working well.

Would you please advise how to do the below? "If you are tired copying cs50.h and cs50.c every time between folders when you create many projects, you can consider full path of these two files."

ES208 avatar Aug 18 '21 10:08 ES208

moved my input to the other issue https://github.com/cs50/libcs50/issues/193#issuecomment-992856890 as that one is more generic which's what my input was about as well. although, as i said, my solution can be used with any IDE (and i use it in vscodium itself) but the last steps in that solution can be changed and made much easier in vscode/vscodium i think. so...

goyalyashpal avatar Dec 13 '21 20:12 goyalyashpal

I was able to get mine working on my Mac with Visual Studio Code as follow: Assuming you have Visual Studio already installed and with all C/C++ extensions required for compiling and running C codes, 1a. Download the CS50 Library from https://github.com/cs50/libcs50/releases

1b. Extract/unzip the downloaded zipped folder

2a. Open Terminal and change directory to the unzipped CS50 library folder. For example if folder is on desktop and the folder is named libcs50-10.1.0 , then terminal command should be: cd desktop/libcs50-10.1.0

2b. Still in terminal after changing directory, then run the command: sudo make install

3a. After installation, close terminal, go to visual studio and write your C program #include <cs50.h>

3b. Link cs50 when compiling your code with clang. Example if your file name is hello.c clang hello.c -o hello -lcs50 (he means, before put "make hello" in your terminal, put "clang hello.c -o hello -lcs50")

  1. run your code Gotta give explicit direction, when the reader dont have any basic knowledge about CLI operations.

LBY89 avatar Dec 25 '21 10:12 LBY89

quoted reply to https://github.com/cs50/libcs50/issues/189#issuecomment-693022677

@LBY89 yeah, what do u want to say??

goyalyashpal avatar Dec 25 '21 11:12 goyalyashpal

3. or you can just use full path of cs50.c, like gcc -o out yourfile.c <full path> - @timeneverdie at https://github.com/cs50/libcs50/issues/189#issuecomment-732068690

that fullpath method doesnt work for me, even tried with quotes, and both relative/absolute addresses

this method worked only when the both those files are in exact same directory as the program source files. ~~which also has to be the current directory one~~ this last condition not needed

goyalyashpal avatar Dec 25 '21 20:12 goyalyashpal

How to set clang link to cs50 as default guys? that would be an ultimate solve. Otherwise, everytime we compile a code, we have to add that "clang hello.c -o hello -lcs50"

LBY89 avatar Dec 26 '21 07:12 LBY89

How to set clang link to cs50 as default guys? that would be an ultimate solve. Otherwise, everytime we compile a code, we have to add that "clang hello.c -o hello -lcs50"

I found solution on Stackoverflow: "to create a Makefile that would automatically link cs50 library from now on. to do that make a new file with no extensions and call it Makefile; in the Makefile simply write down this line: LDLIBS += -lcs50. further more, when you need to link more libraries in the future , do the same, for example LDLIBS += -lm to add the math library ,etc. I hope I could help you with this" This WORKS, now I can practice just like that teacher.

LBY89 avatar Dec 26 '21 07:12 LBY89

I found solution on Stackoverflow - @lby89

link?

goyalyashpal avatar Dec 26 '21 08:12 goyalyashpal

How to set clang link to cs50 as default guys? that would be an ultimate solve. Otherwise, everytime we compile a code, we have to add that "clang hello.c -o hello -lcs50"

I found solution on Stackoverflow: "to create a Makefile that would automatically link cs50 library from now on. to do that make a new file with no extensions and call it Makefile; in the Makefile simply write down this line: LDLIBS += -lcs50. further more, when you need to link more libraries in the future , do the same, for example LDLIBS += -lm to add the math library ,etc. I hope I could help you with this" This WORKS, now I can practice just like that teacher.

It works just for Mac users. @timeneverdie process works for Windows users. Although I made a bit of modification to it as it didn't fully work for me.

I'll be dropping it soon.

MADEPADIO avatar Jan 03 '22 02:01 MADEPADIO

This does not work for me in vscode. When I run the gcc line in the terminal on a .c file with the #include "cs50.h" header, with both cs50.c and cs50.h in the same directory, i get this message in the terminal "error: ld returned 1 exit status"

I have code runner, gcc, gbd, mingw, mysys2 mysys2 and all that has been installed and updated. Cannot seem to find a solution

Jlietz93 avatar Feb 21 '22 04:02 Jlietz93

I found this, hopefully it helps. https://www.youtube.com/watch?v=9yzQCgIdL-Y

Jlietz93 avatar Feb 21 '22 05:02 Jlietz93

Cannot seem to find a solution

@Jlietz93 follow the link given at this comment : https://github.com/cs50/libcs50/issues/189#issuecomment-992849821

goyalyashpal avatar Feb 22 '22 02:02 goyalyashpal

OK guys, finally I solve it totally!

To give you motivation first:

image

You can have highlight and intelligent code completion when you are coding. (Notice string and get_string() are highlighted)

image

And also, you can easily debug just with one F5 key!

Most important thing is that as beginners, we should code by Text Editors. Though IDEs are convenient and industry-friendly, but in many situations Text Editors are better.

The popular Text Editor powered by Microsoft is Visual Studio Code (VSCode), it can be enhanced by extensions to become an IDE. And also, it supports many programming languages throughout CS50 course and your further career. A very good tool to learn and use!

Please follow every step to make it work on Windows:

If you are on Linux or Mac, you can check README.md (https://github.com/cs50/libcs50/blob/develop/README.md) to get installation guide. If you want to know the generally manually installing method on Linux, the usage is the same as on Windows. You just need cs50.h and cs50.c, no need to install any other libraries, even though installation script on Linux is easier than on Windows. If you try to test my method on Linux manually, make sure you have installed gcc and gdb (sudo apt install gcc gdb -y)

First, you need to successfully build standard C libraries on your Windows. To do so, you need:

  • Install VSCode
  • Install C/C++ extension by Microsoft (search on in VSCode store)
  • Install Mingw64 and set PATH (Please read other tutorials)

    After finishing installation, you need to type in Powershell with command gcc to test if GCC Compiler is ready.

  • Open a Workspace in VSCode

    In fact, Workspace is just a folder. But this is very important and it's a must-master skill to use VSCode. To open a Workspace, click File -> Open a folder at the top menu bar

  • Create a hello_world.c file from left file tree panel

    You must first create a *.c file to enable highlight and intelligent. Have a good habit like this.

  • Save and open Terminal. Use gcc -o hello_world hello_world.c to build the hello_world.exe

    To open Terminal, you can use shorts Ctrl+~, or just move your cursor at the bottom of VSCode and then click and drag upwards to pull the Terminal out. Please notice that Terminal has many tabs.

  • run hello_world.exe ( .\hello_world in Terminal) to make sure it work

Now you have a very healthy environment for C on your Windows. Next, we focus how to use cs50.h and cs50.c.

First, we need to test if cs50.h and cs50.c work properly:

  • Download the zip from Github Release (https://github.com/cs50/libcs50/releases). Unzip it, locate to libcs50/src/, you can get cs50.h and cs50.c.
  • Copy cs50.h and cs50.c in the Workspace
  • Create and save a C file which uses cs50 libraries like string type and get_string() in the Workspace. We call it string.c.

    string.c should be with cs50.h and cs50.c in the Workspace. To use cs50.h in your file, please write #include "cs50.h" (pay attention to the quote mark).

  • Use gcc -o string string.c cs50.c to compile
  • Run string.exe (.\string in Terminal) to make sure it work

Next, we should make this work automatically and active debugger:

First, let's test if standard C libraries compiling and debugging will work:

  • Open hello_wolrd.c from left file tree panel
  • Press F5 on your keyboard
  • VSCode will guide you to choose: you should choose GCC or GDB keyword twice
  • Now you have two files under your Workspace named as .vscode folder
  • hello_world.exe should run properly in Terminal automatically by VSCode

Then we need change a little bit something to default settings of C/C++ extension:

  • Open task.json file from VSCode's left file tree panel

  • Edit args like below (// is my comment)

    Every keyword in args will work the same like gcc -o string string.c cs50.c (recall Command-line Argument), so we just edit keyword to let it work as we type in the command line.

          "args": [
              "-g",
              "${file}",
              "${fileDirname}\\cs50.c",    // add this line. Must use "\\" to have correct path. Must add ',' mark in the end of line.
              "-o",
              "${fileDirname}\\${fileBasenameNoExtension}.exe"
          ],
    
  • Save the task.json file

  • Open string.c from left file tree panel

  • Press F5 on your keyboard

    F5 will save the file automatically, so you don't need press Ctrl + S any more.

Now the F5 should work properly to build with cs50.c (cs50.h included). If not, please open task.json file again and then edit type keyword:

         "type": "cppbuild"    =>    "type":"shell"

I don't know if this is a bug, but "cppbuild" really occurs error in my experience even though your args keywords are correct. The other useful suggestion is to make sure your Workspace path doesn't have any space. For example, D:\my code should be D:\my_code.

Final Tip: If you are tired copying cs50.h and cs50.c every time between folders when you create many projects, you can consider full path of these two files.

Because the tutorial above only works when your *.c file, cs50.h, cs50.c at the same directory.

When you want to include the head file, use #include "D:\my_code\cs50.h". When you want to debug, change the args in task.json file, but you should notice that the path use double , not single. For example, "D:\\my_code\\cs50.c".

Enjoy it and enjoy CS50!

Not only useful in CS50. You can use the same method to build multiple C files wrote by you in the future. So it's a good choice to spend your time learning it!

malcmakes avatar Mar 17 '22 01:03 malcmakes

Spent 3 days looking for a solution that would work for me. thank you!!!!!

malcmakes avatar Mar 17 '22 01:03 malcmakes