-2

I am new to C++, and trying to use get_string, but I am not sure what I writing wrong that is creating an error.

The code I have is the following:

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

int main(void)
{
   string name = get_string("What's your name? ");
   printf("hello, %s\n", name);
}

and it keeps saying the following error:

Undefined symbols for architecture arm64:
  "_get_string", referenced from:
      _main in hello-890d43.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

Does any one know what I am doing wrong?

I expected the code take an input and print out hello, (your input).

toasted
  • 7
  • 1
  • 1
    Please search this site for `c++ Undefined symbols for architecture arm64:`. This question has been asked and answered here several times before. Please **always** do a thorough search of the site before posting a new question. – Ken White Nov 17 '22 at 01:33
  • 2
    Isn't cs50 C rather than C++? – Avi Berger Nov 17 '22 at 01:41

3 Answers3

1

Undefined symbols for architecture arm64: "_get_string", referenced from:

Assuming you installed the latest CS50 Library. If not, download it, open the terminal, type cd, then hit space, and drag the folder onto the terminal. You should have a file path displaying. Run the "sudo make install" command, type in your Mac password, and the file should install.

Open VSCode and type clang hello.c -o hello -lcs50. Once you hit enter type ./hello.

The code should now run without any errors!

cconsta1
  • 737
  • 1
  • 6
  • 20
ZERO
  • 11
  • 1
0

The same thing happened to me when trying to use the cs50 C library on my local Mac with M1 chip.

Even after installing the library as described here: https://cs50.readthedocs.io/libraries/cs50/c/ It still didn't work.

The problem is coming from the fact that the make command here doesn't include the link command by default.

Assuming your source file is named hello.c instead of just doing:

make hello

You have to enter the following in your terminal in order to compile:

clang hello.c -o hello -lcs50

Afterwards, when running the executable, it works as expected:

./hello
0
make hello LDLIBS="-lcs50"

LDLIBS to include the cs50 library

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 27 '23 at 14:32