I am try to go through the cs50 course and I found out that they use
#include <cs50.h>
The only problem is that i now get
cc hello.c -o hello
/usr/bin/ld: /tmp/ccMzSNr5.o: in function `main':
hello.c:(.text 0x1a): undefined reference to `get_string'
collect2: error: ld returned 1 exit status
make: *** [<builtin>: hello] Error 1
shell returned 2
Press ENTER or type command to continue
Sorry if this is not very specific or anything, I am just getting started and if you need any extra info I'm more than happy to help.(Also I use vim explaining the error syntax)
I tried to get it to work, I installed all the things on https://github.com/cs50/libcs50/releases and extracted, and installed everything from https://www.youtube.com/watch?v=Vs910D5f_wE
CodePudding user response:
The CS50 library is split into two parts:
- The header file which you need to include;
- And the
cs50.c
source file which you need to build with.
The source file contains the definitions (implementations) of all the support functions while the header file only contains the declarations of the functions.
To build with the source file just add it to the command:
cc hello.c cs50.c -o hello
If you read the documentation in the release archive (the README.md
file) then it shows you how to build an actual library to be installed and linked with.
If you create such a library then you need to link with it instead of building with the source file:
cc hello.c -o hello -L/usr/local/lib -lcs50
CodePudding user response:
For those still having trouble, I would recommend you following the steps on another post.(https://stackoverflow.com/a/73215253/20464086). Here is the solution verbatim from the original post just in case they edit the answer(I added some extra notes here and there). I had the same problem and performed the following steps to be able to compile my code on Linux:
1 - Install CS50 Library for C:(Here is a little note you can really skip this step if you already did the steps of downloading it, although this may go without saying)
curl -s https://packagecloud.io/install/repositories/cs50/repo/script.deb.sh | sudo bash
sudo apt install libcs50
2 - Add the following variables to your .bashrc file:
How to Set Environment Variables in Linux
export CC="clang"
export CFLAGS="-fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow"
export LDLIBS="-lcrypt -lcs50 -lm"
Obs: To access the .bashrc file type nano ~/.bashrc in your terminal and type the code above in the last line of the file and save(I didn't have to do this and it shouldn't be necessary because chromebooks run off gentoo and not obs)
3 - Restart bash:
source ~/.bashrc
4 - Install clang:
sudo apt-get install clang
5 - Compile your source code:
make code_name
6 - Run your machine code:
./code_name