How to use cs50.h in my local VSCode environment?
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?
Include the flag -lcs50 when you compile the code.
Include the flag
-lcs50when you compile the code.
error is cannot find -lcs50
I have added both cs50.c and cs50.h into the include folder of gcc.
Try the troubleshooting steps mentioned in the README file on the master branch.
you need to include cs50.c in your folder and compile both files together on command line gcc -o out appname.c cs50.c
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
- run your code
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.02b. Still in terminal after changing directory, then run the command:
sudo make install3a. 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
- 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 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!
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?
- Copy both
cs50.candcs50.hin the same folder with the file you want to compile *** or you can just copy the full path ofcs50.hand write it in#include "full path" - write
#include "cs50.h"at the beginning of the file - Compile with
gcc -o out yourfile.c cs50.c*** or you can just use full path ofcs50.c, likegcc -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.
OK guys, finally I solve it totally!
To give you motivation first:

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

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 needcs50.handcs50.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 installedgccandgdb(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++ extensionby Microsoft (search on in VSCode store) - Install
Mingw64and set PATH (Please read other tutorials)After finishing installation, you need to type in
Powershellwith commandgccto test if GCC Compiler is ready. - Open a
Workspacein VSCodeIn fact,
Workspaceis just a folder. But this is very important and it's a must-master skill to use VSCode. To open aWorkspace, clickFile->Open a folderat the top menu bar - Create a
hello_world.cfile from left file tree panelYou must first create a *.c file to enable highlight and intelligent. Have a good habit like this.
- Save and open
Terminal. Usegcc -o hello_world hello_world.cto build thehello_world.exeTo 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_worldinTerminal) 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 tolibcs50/src/, you can getcs50.handcs50.c. - Copy
cs50.handcs50.cin theWorkspace - Create and save a C file which uses cs50 libraries like
string typeandget_string()in theWorkspace. We call itstring.c.string.cshould be withcs50.handcs50.cin theWorkspace. To usecs50.hin your file, please write#include "cs50.h"(pay attention to the quote mark). - Use
gcc -o string string.c cs50.cto compile - Run
string.exe(.\stringinTerminal) 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.cfrom left file tree panel - Press
F5on your keyboard - VSCode will guide you to choose: you should choose
GCCorGDBkeyword twice - Now you have two files under your
Workspacenamed as.vscode folder -
hello_world.exeshould run properly inTerminalautomatically by VSCode
Then we need change a little bit something to default settings of C/C++ extension:
-
Open
task.jsonfile from VSCode's left file tree panel -
Edit
argslike below (//is my comment)Every keyword in
argswill 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.jsonfile -
Open
string.cfrom left file tree panel -
Press
F5on your keyboardF5will save the file automatically, so you don't need pressCtrl+Sany 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
Workspacepath doesn't have any space. For example,D:\my codeshould beD:\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
*.cfile,cs50.h,cs50.cat 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 , this answer is so good, I propose we add this somewhere in the README.md. Absolutely fantastic job! This is really interesting.
"args" : // end of the file expected error
You saved me a lot. Thank you very much🙏
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
@timeneverdie
I took the exact steps and successfully built hello.c but string.c is giving me an error:
undefinedreference 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
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?
@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."
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...
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.02b. Still in terminal after changing directory, then run the command:
sudo make install3a. 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")
- run your code Gotta give explicit direction, when the reader dont have any basic knowledge about CLI operations.
quoted reply to https://github.com/cs50/libcs50/issues/189#issuecomment-693022677
@LBY89 yeah, what do u want to say??
3. or you can just use full path of
cs50.c, likegcc -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
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"
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.
I found solution on Stackoverflow - @lby89
link?
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.
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
I found this, hopefully it helps. https://www.youtube.com/watch?v=9yzQCgIdL-Y
Cannot seem to find a solution
@Jlietz93 follow the link given at this comment : https://github.com/cs50/libcs50/issues/189#issuecomment-992849821
OK guys, finally I solve it totally!
To give you motivation first:
You can have highlight and intelligent code completion when you are coding. (Notice
stringandget_string()are highlighted)
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 needcs50.handcs50.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 installedgccandgdb(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++ extensionby Microsoft (search on in VSCode store)- Install
Mingw64and set PATH (Please read other tutorials)After finishing installation, you need to type in
Powershellwith commandgccto test if GCC Compiler is ready.- Open a
Workspacein VSCodeIn fact,
Workspaceis just a folder. But this is very important and it's a must-master skill to use VSCode. To open aWorkspace, clickFile->Open a folderat the top menu bar- Create a
hello_world.cfile from left file tree panelYou must first create a *.c file to enable highlight and intelligent. Have a good habit like this.
- Save and open
Terminal. Usegcc -o hello_world hello_world.cto build thehello_world.exeTo 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_worldinTerminal) to make sure it workNow you have a very healthy environment for C on your Windows. Next, we focus how to use
cs50.handcs50.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 tolibcs50/src/, you can getcs50.handcs50.c.- Copy
cs50.handcs50.cin theWorkspace- Create and save a C file which uses cs50 libraries like
string typeandget_string()in theWorkspace. We call itstring.c.
string.cshould be withcs50.handcs50.cin theWorkspace. To usecs50.hin your file, please write#include "cs50.h"(pay attention to the quote mark).- Use
gcc -o string string.c cs50.cto compile- Run
string.exe(.\stringinTerminal) to make sure it workNext, 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.cfrom left file tree panel- Press
F5on your keyboard- VSCode will guide you to choose: you should choose
GCCorGDBkeyword twice- Now you have two files under your
Workspacenamed as.vscode folderhello_world.exeshould run properly inTerminalautomatically by VSCodeThen we need change a little bit something to default settings of
C/C++ extension:
Open
task.jsonfile from VSCode's left file tree panelEdit
argslike below (//is my comment)Every keyword in
argswill 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.jsonfileOpen
string.cfrom left file tree panelPress
F5on your keyboard
F5will save the file automatically, so you don't need pressCtrl+Sany more.Now the
F5should work properly to build withcs50.c(cs50.hincluded). If not, please opentask.jsonfile again and then edittypekeyword:"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
Workspacepath doesn't have any space. For example,D:\my codeshould beD:\my_code.Final Tip: If you are tired copying
cs50.handcs50.cevery 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
*.cfile,cs50.h,cs50.cat the same directory.When you want to include the head file, use
#include "D:\my_code\cs50.h". When you want to debug, change theargsintask.jsonfile, 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!
Spent 3 days looking for a solution that would work for me. thank you!!!!!