-3

So I have a problem where when I try to compile a c++ file in vscode. I would do clang Watermelon.cpp in the terminal. This would work fine with the following code

#include <iostream>

using namespace std;

int main() {
    int x;
    
    return 0;
}

But once I add cin and cout it throws an error.

#include <iostream>

using namespace std;

int main() {
    int x;
    cin >> x;
    return 0;
}
Error: Undefined symbols for architecture arm64:
  "std::__1::basic_istream<char, std::__1::char_traits<char> >::operator>>(int&)", referenced from:
      _main in Watermelon-42c4ad.o
  "std::__1::cin", referenced from:
      _main in Watermelon-42c4`your text`ad.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Running it using VScodes debugger works, but I would like to figure out why this happens when I switch from my school Windows mingGW to clang on my macbook.

273K
  • 29,503
  • 10
  • 41
  • 64
Mallhw
  • 15
  • 5

1 Answers1

1
clang Watermelon.cpp

This invokes the C compiler front-end. It will correctly figure out that it's C++ code and compile it as such, but it will still link with the C standard library instead of the C++ standard library which is why it can't find the definition of the referenced function.

You should use the C++ compiler front-end instead:

clang++ Watermelon.cpp
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108