在终端(macOS)中编译hello world.cpp

时间:2019-05-12 06:46:37

标签: c++ macos gcc g++

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

#include <iostream>
using namespace std;

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

我进入包含代码的目录。我设法用命令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”中,     引用自:           c_helloworld-a3d3b8.o中的std :: __ 1 :: basic_ios> :: widen(char)const“ std :: __ 1 :: basic_string,     std :: __ 1 :: allocator> :: __ init(unsigned long,char)“,已引用     从:           std :: __ 1 :: basic_string,std :: __ 1 :: allocator> :: basic_string(unsigned long,char)在     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)     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 *,无符号长)在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()”已引用     从:           c_helloworld-a3d3b8.o中的std :: __ 1 :: basic_ios> :: widen(char)const“ 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 :: __ 1 :: ios_base :: setstate(unsigned int)“ std :: terminate()”,引用自:           在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中           在c_helloworld-a3d3b8.o“ ___cxa_call_unexpected”中,___ clang_call_terminate,引用自:           c_helloworld-a3d3b8.o中的std :: __ 1 :: ostreambuf_iterator> :: ostreambuf_iterator(std :: __ 1 :: basic_ostream>&)
    从以下位置引用的“ ___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)     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中的矮人异常展开信息(__eh_frame)ld:找不到架构x86_64的符号     clang:错误:链接器命令失败,退出代码为1(使用-v查看     调用)

  

2 个答案:

答案 0 :(得分:1)

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

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

在命令行中

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

答案 1 :(得分:1)

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

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

gcc -o hello c_helloworld.cpp -lstdc++

PS。我建议您在询问问题之前先搜索网站,对此已经有了答案。

Compiling a C++ program with gcc