0

I had a compiling error when coding in C on VSCode 2 on MacOS. The program is to calculate the factorial of a natural number. Here is my .cpp

#include <stdio.h>
#include <ncurses.h>

int gthua(int);
int main()
 {
    char c = 'N';
    int N  = 1;
    int kq;
    do {
        printf("\n Nhap mot so nguyen duong: ");
        scanf("%d", &N);
        kq = gthua(N);
        printf("\n Giai thua cua so %d la %d",N, kq);
        printf("\n Ban co muon tiep tuc khong? Y/N");
        c = getch();

    } while (c=='y' || c=='Y');
    return 0;
}
int gthua(int n) {
    int kq = 1;
    while (n > 1) 
        kq*= n--;
    return kq;
}

When I tried running the code it returned this:

Undefined symbols for architecture x86_64:
  "_stdscr", referenced from:
      _main in 1-cabd12.o
  "_wgetch", referenced from:
      _main in 1-cabd12.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

What should I do?

6
  • That's not a compile error. It's a linker error. Please search this site for the exact phrase [c][macos] Undefined symbols for architecture x86_64. This question has been asked and answered here many times before - surely, one of those existing answers will help you resolve the issue without needing to create yet another duplicate. Commented Sep 22, 2024 at 14:03
  • 1
    You have to link the ncurses library. Commented Sep 22, 2024 at 14:15
  • 1
    @KenWhite Since linking is normally done automatically when you compile, the distinction is generally irrelevant. Commented Sep 22, 2024 at 14:24
  • 1
    @Barmar: That may be true, but it seems important to me that new coders learn the difference between compiling and linking - they are two separate and distinct operations, despite the fact that they're now mostly done automatically. After all, it's possible to compile without linking - I do it frequently to check my syntax and typing when I'm coding. Commented Sep 22, 2024 at 17:29
  • @KenWhite With the advent of IDEs that automatically compile and execute, they can barely tell the difference between those! Commented Sep 23, 2024 at 15:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.