libcs50
libcs50 copied to clipboard
Mac M1: Undefined symbols for architecture arm64 when trying to use cs50.h
@zizlog @HoonyHoney-91: as follow-up to:
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.
Originally posted by @zizlog in https://github.com/cs50/libcs50/issues/212#issuecomment-1516497958
On my Mac M1 I got a similar error message:
rob@TJYV4J34RJ ~ % make hello
cc hello.c -o hello
Undefined symbols for architecture arm64:
"_get_string", referenced from:
_main in hello-f3db2a.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [hello] Error 1
The mention of clang made me suspicious, as I was under the impression that gcc was being used. When running gcc indeed a reference to clang comes up:
rob@TJYV4J34RJ ~ % gcc
clang: error: no input files
Digging a bit led me to https://stackoverflow.com/questions/64992467/mac-clang-installation-seems-to-override-gcc-install. The comments in there made me check the version of make I was using:
rob@TJYV4J34RJ ~ % /usr/bin/make -v
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
That seemed pretty ancient, checking the versions on https://ftp.gnu.org/gnu/make/. After running "brew install make", I now got:
rob@TJYV4J34RJ ~ % make -v
GNU Make 4.4.1
Built for x86_64-apple-darwin23.4.0
Copyright (C) 1988-2023 Free Software Foundation, Inc.
Seeing this caveat made me run "export PATH="/usr/local/opt/make/libexec/gnubin:$PATH"":
==> Caveats
GNU "make" has been installed as "gmake".
If you need to use it as "make", you can add a "gnubin" directory
to your PATH from your bashrc like:
PATH="/usr/local/opt/make/libexec/gnubin:$PATH"
Now, when running "make hello" it works, without having to add anything special to link cs50 as make now invokes gcc with the right parameters, just like when using a Makefile:
rob@TJYV4J34RJ ~ % make hello
gcc -Wall -Wextra -std=c11 -o hello hello.c -lcs50
rob@TJYV4J34RJ ~ %