Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to `get_string' compiling CS50 code

Tags:

c

cs50

I'm a complete beginner and have no prior coding experince. and most of the answer to this problem are in technical words, but I'm not able to understand them. Please help in more begginer friendly language.

My code

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

int main(void)
{
    string answer = get_string("What is your name");
    printf("Hello, %s\n", answer);
}

And after running it, got this.

> Executing task: C/C++: gcc.exe build active file <

Starting build...
C:\msys64\mingw64\bin\gcc.exe -fdiagnostics-color=always -g "C:\Users\MaNaS\Desktop\CS50x\C Code\0002.c" -o "C:\Users\MaNaS\Desktop\CS50x\C Code\0002.exe"
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\MaNaS\AppData\Local\Temp\ccIpKhYX.o: in function `main':
C:\Users\MaNaS\Desktop\CS50x\C Code/0002.c:6: undefined reference to `get_string'
collect2.exe: error: ld returned 1 exit status

Build finished with error(s).

Terminal will be reused by tasks, press any key to close it.

I'm using VS code and Mingw64. I had downloaded CS50 library, and copied cs50.c & cs50.h to C:\msys64\mingw64\include

like image 628
Manas Deo Avatar asked Jun 10 '26 21:06

Manas Deo


2 Answers

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:

 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

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
like image 84
Denilson Martins Avatar answered Jun 13 '26 14:06

Denilson Martins


I feel like a few people trying to help you don't understand the CS50x course. Your code matches mine, but looking at your output, it appears you are not running your project in Codespaces. Per the project specifications, your work should be done in Codespaces. Read through the beginning of the problem for instructions on how to set up your Codespace. Don't worry about everyone talking about how string is char* and that get_string does not exist, when you run your code in Codespaces, your line #include <cs50.h> will fix all of this.

like image 33
Frankie Griffin Avatar answered Jun 13 '26 15:06

Frankie Griffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!