【问题标题】:Compiling hello world.cpp in terminal (macOS) [duplicate]在终端(macOS)中编译 hello world.cpp [重复]
【发布时间】:2019-09-29 11:56:55
【问题描述】:

我正在尝试在终端中编译一个 C++ helloWorld。

#include <iostream>
using namespace std;

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

我 cd 到包含代码的目录。我设法使用命令 g++ -o hello c_helloworld.cpp 进行编译。但是当我使用命令 gcc -o hello c_helloworld.cpp 我得到以下错误。

架构 x86_64 的未定义符号:“std::__1::locale::use_facet(std::__1::locale::id&) const”,引用 从: std::__1::ctype const& std::__1::use_facet >(std::__1::locale const&) 在 c_helloworld-a3d3b8.o "std::__1::ios_base::getloc() const", 参考自: std::__1::basic_ios >::widen(char) const in c_helloworld-a3d3b8.o "std::__1::basic_string, std::__1::allocator >::__init(unsigned long, char)",引用 从: std::__1::basic_string, std::__1::allocator >::basic_string(unsigned long, char) in c_helloworld-a3d3b8.o "std::__1::basic_string, std::__1::allocator ::~basic_string()",引用自: std::__1::ostreambuf_iterator > std::__1::__pad_and_output >(std::__1::ostreambuf_iterator >, char const*, char const*, char const*, std::__1::ios_base&, char) in c_helloworld-a3d3b8.o "std::__1::basic_ostream >::put(char)",引用自: std::__1::basic_ostream >& std::__1::endl (std::__1::basic_ostream >&) 在 c_helloworld-a3d3b8.o "std::__1::basic_ostream >::flush()",引用自: std::__1::basic_ostream >& std::__1::endl (std::__1::basic_ostream >&) 在 c_helloworld-a3d3b8.o "std::__1::basic_ostream ::sentry::sentry(std::__1::basic_ostream >&)”,引用自: std::__1::basic_ostream >& std::__1::__put_character_sequence (std::__1::basic_ostream >&, char const*, unsigned long) 在 c_helloworld-a3d3b8.o
“std::__1::basic_ostream ::sentry::~sentry()”,引用自: std::__1::basic_ostream >& std::__1::__put_character_sequence (std::__1::basic_ostream >&, char const*, unsigned long) 在 c_helloworld-a3d3b8.o "std::__1::cout", 参考自: _main in c_helloworld-a3d3b8.o "std::__1::ctype::id",引用自: std::__1::ctype const& std::__1::use_facet >(std::__1::locale const&) 在 c_helloworld-a3d3b8.o "std::__1::locale::~locale()" 中,引用 从: std::__1::basic_ios >::widen(char) const in c_helloworld-a3d3b8.o "std::__1::ios_base::__set_badbit_and_consider_rethrow()",引用 从: std::__1::basic_ostream >& std::__1::__put_character_sequence (std::__1::basic_ostream >&, char const*, unsigned long) 在 c_helloworld-a3d3b8.o
“std::__1::ios_base::clear(unsigned int)”,引用自: c_helloworld-a3d3b8.o "std::terminate()" 中的 std::__1::ios_base::setstate(unsigned int),引用自: c_helloworld-a3d3b8.o "___cxa_begin_catch" 中的___clang_call_terminate,引用自: std::__1::basic_ostream >& std::__1::__put_character_sequence (std::__1::basic_ostream >&, char const*, unsigned long) 在 c_helloworld-a3d3b8.o ___clang_call_terminate in c_helloworld-a3d3b8.o "___cxa_call_unexpected",引用自: std::__1::ostreambuf_iterator >::ostreambuf_iterator(std::__1::basic_ostream >&) 在 c_helloworld-a3d3b8.o
“___cxa_end_catch”,引用自: std::__1::basic_ostream >& std::__1::__put_character_sequence (std::__1::basic_ostream >&, char const*, unsigned long) 在 c_helloworld-a3d3b8.o
“___gxx_personality_v0”,引用自: std::__1::basic_ostream >& std::__1::__put_character_sequence (std::__1::basic_ostream >&, char const*, unsigned long) 在 c_helloworld-a3d3b8.o std::__1::ostreambuf_iterator > std::__1::__pad_and_output >(std::__1::ostreambuf_iterator >, char const*, char const*, char const*, std::__1::ios_base&, char) in c_helloworld-a3d3b8.o c_helloworld-a3d3b8.o 中的 std::__1::ostreambuf_iterator >::ostreambuf_iterator(std::__1::basic_ostream >&) c_helloworld-a3d3b8.o 中的 std::__1::basic_ios >::widen(char) const c_helloworld-a3d3b8.o ld 中的 Dwarf 异常展开信息 (__eh_frame):未找到架构 x86_64 的符号 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看 调用)

【问题讨论】:

    标签: c++ macos gcc g++


    【解决方案1】:

    gcc 用于编译 C 程序(默认),g++ 用于 C++。 因此,该行为是预期的。

    默认情况下,gcc 链接到标准 C 库。 如果要编译 C++ 程序,可以通过添加以下选项链接到标准 C++ 库:

    gcc -o hello c_helloworld.cpp -lstdc++
    

    PS。我建议您在提问之前搜索该网站,已经有答案了。

    Compiling a C++ program with gcc

    【讨论】:

      【解决方案2】:

      使用 g++ 更容易编译 .cpp 文件。通过在目录中运行./outputfile 来执行编译后的代码。所以基本上它会运行

      g++ file.cpp -o file && ./file
      

      在命令行中

      编辑:我的错误.. gcc 也可以在这里使用,但使用 g++ 更简单

      【讨论】:

      猜你喜欢
      • 2023-01-11
      • 2021-12-18
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多