0

Just for context I am an absolute beginner related to any coding. Currently I'm trying to run the code from the cs-50 video, but I get the same error every time.

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

int main (void)
{
        int x = get_int("x: ");
        int y = get_int("y: ");
        printf("%i\n", x+y );
}

Commands used for compiling:

cc     calculator.c   -o calculator
Undefined symbols for architecture arm64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [calculator] Error 1
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 1
    Are you sure the .c file you show in the question is the `calculator.c` file you compile? Because the code you show in the question is correct. – Jabberwocky Jul 19 '23 at 14:57
  • What platform are you building on? And targeting? It looks like you don't have the ARM64 toolchain fully installed. – Toby Speight Jul 19 '23 at 14:57
  • 3
    Did you save the file `calculator.c` before running `cc calculator.c -o calculator`? – Andreas Wenzel Jul 19 '23 at 14:59
  • 2
    Instead of typing `cc calculator.c -o calculator`, type `cat calculator.c` and tell us what happens – Jabberwocky Jul 19 '23 at 15:00
  • 2
    Side-note which is unrelated to your problem: I recommend that you use `cc calculator.c -o calculator -Wall -Wextra -pedantic -Wshadow -Werror` instead of `cc calculator.c -o calculator` for compiling your code, for the reasons stated here: [Why should I always enable compiler warnings?](https://stackoverflow.com/q/57842756/12149471) You can use a [makefile](https://en.wikipedia.org/wiki/Make_(software)#Makefile) so that you don't have to type this long command every time you compile. This is also what the CS50 IDE does. – Andreas Wenzel Jul 19 '23 at 15:11
  • This is what came out after I wrote cat calculator.c #include "cs50.h" #include int main (void) { int x = get_int("x: "); int y = get_int("y: "); printf("%i\n", x+y ); } – Codenoob Jul 19 '23 at 17:17
  • @Codenoob: That response to `cat calculator.c` looks correct. Does `cc calculator.c -o calculator` still give you the same error message, if you run it immediately afterwards from the same directory? – Andreas Wenzel Jul 19 '23 at 17:51
  • 1
    When you call `cc calculator.c -o calculator`, you must also link to the CS50 library, by adding `-lcs50`, like this: `cc calculator.c -o calculator -lcs50`. Note that this requires that the CS50 library has been built and that it is in the location expected by the compiler. Alternatively, you if you did not install the library, you can put the file `cs50.c` from the CS50 library into your current working directory and write `cc calculator.c cs50.c -o calculator` However, I am not sure if the missing CS50 library can be the cause of the error message that you posted. – Andreas Wenzel Jul 19 '23 at 18:28
  • See [this link](https://github.com/cs50/libcs50) for more information on how to install the CS50 library. – Andreas Wenzel Jul 19 '23 at 18:45
  • Finally, this code has finally worked, thank you very much ==> cc calculator.c cs50.c -o calculator – Codenoob Jul 20 '23 at 21:11

0 Answers0