0

I've had no problems compiling in the past, and code that I wrote previously is compiling fine, but it is now giving this error when I try to compile a very simple C++ file using Visual Studio Code on my M1 Mac:

Code:

#include<iostream>
using namespace std;

int main() {
    cout << "Hello World" << endl;
    return 0;
}

The terminal commands I've tried (all give same error):

g++ -std=c++14 test.cpp -o test
g++ -std=c++11 test.cpp -o test
g++ test.cpp -o test

Error:

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I've tried to delete and re-create the file with different names, restarting VSC, using different output file names and nothing changes this.

askman
  • 447
  • 4
  • 14
  • 1
    It looks like your `gcc`'s linker is installed for `x86_64` architecture, but the compiler does something different because M1 is not `x86_64`. I'd suspect some virtualization mismatch. – yeputons Jan 25 '21 at 12:12
  • I've not installed anything prior to this happening - I haven't used the laptop in a couple of weeks. Doing `g++ --version` returns (amongst other things) `Apple clang version 12.0.0 (clang-1200.0.32.28) Target: x86_64-apple-darwin20.1.0 ` I'm very new to this and very confused why it's randomly started happening. All I did before the issue was create a new file in the directory using the echo command but this is happening with files created via the GUI of VSC too – askman Jan 25 '21 at 12:29
  • What you should be seeing as target is [arm64-apple-darwin20.1.0](https://build2.org/blog/apple-m1-compilation.xhtml) –  Jan 25 '21 at 12:36
  • I get that when I run clang / g++ --version in the terminal, but not when I do it in VSC – askman Jan 25 '21 at 12:45
  • for crying out loud, I hadn't turned on auto-save on and wasn't saving it myself so it couldn't find the file – askman Jan 25 '21 at 12:53
  • Does this answer your question? [Why don't other programs see the changes I made to a file in VS Code until I save those changes?](https://stackoverflow.com/questions/76984829/why-dont-other-programs-see-the-changes-i-made-to-a-file-in-vs-code-until-i-sav) – starball Aug 27 '23 at 06:33

1 Answers1

1

This was occurring as I hadn't saved the file prior to compiling and therefore the compiler couldn't find it.

askman
  • 447
  • 4
  • 14
  • 1
    Welcome to the club! There are many reasons to use an IDE instead of a text editor and the command line and that is one of them... – U. W. Jan 25 '21 at 14:12
  • I was using VSC for this so have even fewer excuses! – askman Jan 26 '21 at 15:04
  • An IDE should warn you when that happens. Or save everything automatically when you start to build. Changing files on disk is no problem anymore since the invention of version control systems... – U. W. Jan 26 '21 at 16:25