gpt4 book ai didi

C++ Hello World : Undefined symbols for architecture x86_64

转载 作者:行者123 更新时间:2023-11-28 02:31:50 25 4
gpt4 key购买 nike

当我尝试编译一个简单的 hello world 代码时,我从 gcc 得到一些架构错误

$ vim 测试.cpp

#include <iostream>

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

$ gcc 测试.cpp -o 测试.o

Undefined symbols for architecture x86_64:
"std::cout", referenced from:
_main in ccyEpFri.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 ccyEpFri.o
"std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
_main in ccyEpFri.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
_main in ccyEpFri.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccyEpFri.o
"std::ios_base::Init::~Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccyEpFri.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

我的 gcc 在这里:/usr/local/bin/gcc

$ gcc --version

gcc (GCC) 4.9.2 20141029 (prerelease)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

系统设置:Mac OSX yosemit 10.10.2来自 http://hpc.sourceforge.net/ 的海湾合作委员会

我原来的 gcc 4.2(Mac 上的默认设置)给我 couldn't understand kern.osversion '14.1.0' 这就是我选择 http://hpc.sourceforge.net/ 的原因

最佳答案

使用 g++ 而不是 gcc。

g++ 是一个基于 *nix 的 C++ 编译器。 gcc 是 C、C++、Objective-C、Fortran、Java、Ada 和 Go 的编译器。使用的库不同。您的代码将适用于 g++ 而不是 gcc。

关于C++ Hello World : Undefined symbols for architecture x86_64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28773453/

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