cs50 / libcs50

This is CS50's Library for C.
https://cs50.readthedocs.io/libraries/cs50/c/
GNU General Public License v3.0
1.74k stars 853 forks source link

Undefined symbols for architecture x86_64 #212

Closed nixoletas closed 4 years ago

nixoletas commented 4 years ago

Installed the library and wanted to use it with VSCODE, i'm running on a macbook pro 2015. I get this error when trying to compile with "make hello":

MacBook-Pro-de-Nick:CS50 nixoletas$ make hello
cc     hello.c   -o hello
Undefined symbols for architecture x86_64:
  "_get_int", referenced from:
      _main in hello-58690a.o
  "_get_string", referenced from:
      _main in hello-58690a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [hello] Error 1

The library only works when compiling with "clang -lcs50 hello.c":

MacBook-Pro-de-Nick:CS50 nixoletas$ clang -lcs50 hello.c
MacBook-Pro-de-Nick:CS50 nixoletas$ ls
a.out           credit.c        hello.c
MacBook-Pro-de-Nick:CS50 nixoletas$ 

After that, i tried to compile my other project named "credit" and ran into this issue:

MacBook-Pro-de-Nick:CS50 nixoletas$ clang -lcs50 credit.c
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
MacBook-Pro-de-Nick:CS50 nixoletas$ 
nixoletas commented 4 years ago

After installing the library through the "sudo make install" command in the downloaded cs50 library folder...

// go to /usr/local/lib directory in Terminal 
// list the contents of lib/ directory 
   // notice that 'libcs50.dylib' file is pointing to 'libcs50-9.dylib' file (this file does not exist) 
// change the symbolic link so that 'libcs50.dylib' points to 'libcs50-9.0.1.dylib' 
   // type 'sudo ln -sf libcs50-9.0.1.dylib /usr/local/lib/libcs50.dylib' command and press enter 
   // enter macOS password 
// check by listing the contents of lib/ directory if the symbolic link has changed
// go to the directory where the source code file requiring cs50 library is saved 
// run 'clang -lcs50 program-to-be-compiled.c' command 
   // if the syntax is correct; it will create 'a.out' file 
// run './a.out' command to see the output

Configure make command to be used with -lcs50 command. It can be done by creating a file called "Makefile" in the directory where the source code file is saved.

We can add the following code in the Makefile to configure make utility to work with -lcs50 command.

LDLIBS += -lcs50
CC := clang

full blog post from Krishan:

g471000 commented 2 years ago

I got the same issue, and resolved after nixoletas did! Create a file name Makefile, and added like him. Thank you, nixoletas!

sozoalvin commented 2 years ago

I don't know if the comments in nixoletas were necessary but they sure confused me a little. However what worked was the 2nd part of the same post.

  1. Create a file called "Makefile" in the same directory
  2. put the following two lines of codes in the Makefile and then save the file
    LDLIBS += -lcs50
    CC := clang

Thereafter your make command should run just fine. also if your vs code has been opened before; you should also restart your vscode.

LBY89 commented 2 years ago

Don't have to be so hard. In your terminal, put "clang -lcs50 program-to-be-compiled.c" before, put "make program-to-be-compiled". It works for me. But you ofc have to install that cs50 library first. Dam it, why it has to be so hard for non-harved learner to do anything.

purart commented 2 years ago

Just follow the course instructions and use this online environment by the conductor: https://cs50.harvard.edu/college/2021/fall/psets/1/hello/

kashiouris commented 2 years ago

LDLIBS += -lcs50 CC := clang

Thank you!

jargoman commented 2 years ago

The makefile helped me.

Adding the following to .bashrc may have also helped. export LIBRARY_PATH=/usr/local/lib

There's some discrepancy between zsh and bash.

HoonyHoney-91 commented 2 years ago

Still having problems with Undefined symbols for architecture arm64: "_get_string", referenced from: _main in week1-18fd68.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation).

I followed and switched the libcs50.dylib -> libcs50-11.0.1.dylib
Screen Shot 2022-09-05 at 11 21 04 PM
And also added LDLIBS += -lcs50 CC := clang Screen Shot 2022-09-05 at 11 21 46 PM

to Makefile and still not showing the way it is shown on the lecture screen.

can anyone help me? Screen Shot 2022-09-05 at 11 22 40 PM

Acovid commented 1 year ago

@HoonyHoney-91 this worked for me:

clang -lcs50 -o <programname> <programname>.c

David explains it in lecture 2.

zizlog commented 1 year ago

I don't think it solves the issue. Although it works using your suggestion, it fails using make, giving the error @HoonyHoney-91 points out. I'm having the same issue as him.

yonaSeo commented 1 year ago

Just like @sozoalvin said,

touch Makefile

in your current working directory and write in Makefile,

LDLIBS += -lcs50
CC := clang

and command,

make <filename>

and it will be compiled well.

pranavkotwal commented 1 year ago

Simply create a Makefile file

enter:

LDLIBS += -lcs50
CC := clang

and just do

make hello.c
Screenshot 2023-08-05 at 2 10 43 PM