gpt4 book ai didi

c++ - C++ 的 Hello World 错误

转载 作者:太空狗 更新时间:2023-10-29 23:26:27 24 4
gpt4 key购买 nike

我开始学习 C++,并尝试运行经典的 Hello World 程序:

#include <iostream>

using namespace std;

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

但是当我使用 GCC 在终端中编译它时,我总是会得到这个错误:

Undefined symbols for architecture x86_64:
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const", referenced from:
std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccpe3DPY.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const", referenced from:
std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccpe3DPY.o
"std::cout", referenced from:
_main in ccpe3DPY.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in ccpe3DPY.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccpe3DPY.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccpe3DPY.o
"___gxx_personality_v0", referenced from:
Dwarf Exception Unwind Info (__eh_frame) in ccpe3DPY.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

你能告诉我为什么吗?

最佳答案

您已经使用 gcc 编译了您的程序,它是 C 编译器,而不是 C++ 编译器。使用 g++ 代替;它知道包含 C++ 运行时库,其中定义了那些缺失的符号。

关于c++ - C++ 的 Hello World 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20621915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com