gpt4 book ai didi

C++ Hello World 体系结构的 undefined symbol x86_64 :

转载 作者:太空狗 更新时间:2023-10-29 20:15:26 25 4
gpt4 key购买 nike

应该很简单,但是当我编译 C++ Hello World 代码时返回一堆 undefined symbol 错误。

// my first program in C++
#include <iostream>
using namespace std;

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

我是这样编译的:打开终端,cd到目录,gcc hello.cpp

然后我得到了错误。有什么想法吗?我觉得我可能已经破坏了某些东西……或者我只是遗漏了一些非常明显的东西。非常感谢任何帮助!

这里是错误:

Undefined symbols for architecture x86_64:
"std::cout", referenced from:
_main in ccfUAf5i.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 ccfUAf5i.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccfUAf5i.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccfUAf5i.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

最佳答案

你需要使用g++来编译和链接C++代码:

g++ hello.cpp -o hello

或者完全符合 Stack Overflow 标准:

g++ -W -Wall -Werror -pedantic -o hello hello.cpp

关于C++ Hello World 体系结构的 undefined symbol x86_64 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13223992/

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