架构 x86_64 的 C++ Hello World 未定义符号: [英] C++ Hello World Undefined symbols for architecture x86_64:

查看:25
本文介绍了架构 x86_64 的 C++ Hello World 未定义符号:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该很简单,但是当我编译 C++ Hello World 代码时,会返回一堆未定义的符号错误.

Should be straightforward, but when I compile the C++ Hello World code returns a bunch of undefined symbol errors.

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

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

以下是我的编译方式:打开终端,cd 到目录,gcc hello.cpp

Heres how I compile: Open terminal, cd to the directory, gcc hello.cpp

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

Then I get the errors. Any thoughts? I feel like I may have broken somehting... or I am just missing something really obvious. Any help is greatly appreciated!

错误如下:

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++代码:

You need to use g++ to compile and link C++ code:

g++ hello.cpp -o hello

或者完全符合 Stack Overflow:

Or to be fully Stack Overflow compliant:

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

这篇关于架构 x86_64 的 C++ Hello World 未定义符号:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆