首页 > 解决方案 > C++ files stopped compiling - keep getting error: linker command failed with exit code 1 (use -v to see invocation)

问题描述

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.

标签: c++visual-studio-codeg++

解决方案


发生这种情况是因为我在编译之前没有保存文件,因此编译器找不到它。


推荐阅读