在终端(MACOS)中编译Hello World.cpp



我正在尝试在终端编译C Helloworld。

#include <iostream>
using namespace std;
int main() {
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    return 0;
}

i CD到包含代码的目录。我设法使用命令g -o hello c_helloworld.cpp编译。但是当我使用命令gcc -o hello c_helloworld.cpp时,我会收到以下错误。

架构的未定义符号x86_64:" std :: __ 1 :: locale :: use_facet(std :: __ 1 :: locale :: locale :: id&amp;(const", 从: STD :: __ 1 :: CTYPE CONST&amp;std :: __ 1 :: use_facet>(std :: __ 1 :: locale const&amp;( 在c_hellowlld-a3d3b8.o中 引用以下参考: std :: __ 1 :: basic_ios> :: wide(char(c_helloworld-a3d3b8.o" std :: __ 1 :: basic_string, std :: __ 1 ::分配器> :: __ init(未签名长,char(",引用 从: std :: __ 1 :: basic_string,std :: __ 1 ::分配器> :: basic_string(basic_string(unsigned long,char( c_helloworld-a3d3b8.o" :: 〜basic_string((",从: std :: __ 1 :: ostreambuf_iterator> std :: __ 1 :: __ ________d_and_output>(std :: __ 1 :: ostreambuf_iterator>,char const*,char const*,char const*,char const*,char const*,char const*,std :: __ 1 :: ios_base&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp; C_Helloworld-a3d3b8.o" std :: __ 1 :: basic_ostream> :: put(char(",从: std :: __ 1 :: basic_ostream>&amp;std :: __ 1 :: endl (std :: __ 1 :: basic_ostream>&amp;(在c_helloworld-a3d3b8.o中 std :: __ 1 :: basic_ostream>&amp;std :: __ 1 :: endl (STD :: __ 1 :: BASIC_OSTREAM>&amp;(在c_helloworld-a3d3b8.o中 :: Sentry :: Sentry(STD :: __ 1 :: BASIC_OSTREAM>&amp;(",从: std :: __ 1 :: basic_ostream>&amp;std :: __ 1 :: __ put_character_sequence (std :: __ 1 :: basic_ostream>&amp; char const*,unsigned long(在c_helloworld-a3d3b8.o中 " std :: __ 1 :: basic_ostream :: Sentry :: 〜Sentry((",从: std :: __ 1 :: basic_ostream>&amp;std :: __ 1 :: __ put_character_sequence (std :: __ 1 :: basic_ostream>&amp; char const*,unsigned long(在c_helloworld-a3d3b8.o中 引用以下参考: _ Main在C_HELELELD-A3D3B8.O中 STD :: __ 1 :: CTYPE CONST&amp;std :: __ 1 :: use_facet>(std :: __ 1 :: locale const&amp;( 在c_helloworld-a3d3b8.o中 从: std :: __ 1 :: basic_ios> :: widen(char(c_helloworld-a3d3b8.o" 从: std :: __ 1 :: basic_ostream>&amp;std :: __ 1 :: __ put_character_sequence (std :: __ 1 :: basic_ostream>&amp; char const*,unsigned long(在c_helloworld-a3d3b8.o中 " std :: __ 1 :: ios_base :: clear(unsigned int(",从: std :: __ 1 :: ios_base :: setState(unsigned int(c_helloworld-a3d3b8.o" std :: terminate((",从: ____clang_call_call_terminate in C_helloworld-a3d3b8.o" ___cxa_begin_catch",从: std :: __ 1 :: basic_ostream>&amp;std :: __ 1 :: __ put_character_sequence (std :: __ 1 :: basic_ostream>&amp; char const*,unsigned long(在c_helloworld-a3d3b8.o中 ____clang_call_call_terminate in C_helloworld-a3d3b8.o" ___cxa_call_unexped",从: std :: __ 1 :: ostreambuf_iterator> :: ostreambuf_iterator(std :: __ 1 :: basic_ostream>&amp;(在c_helloworld-a3d3b8.o
中 " ____cxa_end_catch",从: std :: __ 1 :: basic_ostream>&amp;std :: __ 1 :: __ put_character_sequence (std :: __ 1 :: basic_ostream>&amp; char const*,unsigned long(在c_helloworld-a3d3b8.o中 " ____gxx_personality_v0",从: std :: __ 1 :: basic_ostream>&amp;std :: __ 1 :: __ put_character_sequence (std :: __ 1 :: basic_ostream>&amp; char const*,unsigned long(在c_helloworld-a3d3b8.o中 std :: __ 1 :: ostreambuf_iterator> std :: __ 1 :: __ ________d_and_output>(std :: __ 1 :: ostreambuf_iterator>,char const*,char const*,char const*,char const*,char const*,char const*,std :: __ 1 :: ios_base&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp&amp; c_helloworld-a3d3b8.o std :: __ 1 :: ostreambuf_iterator> :: ostreambuf_iterator(std :: __ 1 :: basic_ostream>&amp;(in C_helloworld-a3d3b8.o std :: __ 1 :: basic_ios> :: widen(char(c_helloworld-a3d3b8.o C_Helloworld-A3D3B8.O LD中的矮人异常易位信息(__eh_frame( clang:错误:链接器命令因出口代码1失败(使用-V查看 调用(

GCC用于编译C程序(默认情况下(,G 适用于C 。因此,这是期望的。

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

gcc -o hello c_helloworld.cpp -lstdc++

ps。我建议您在提出问题之前搜索网站,已经有一个答案。

用GCC

编译C 程序

使用G 更容易编译.CPP文件。通过在目录中运行./outputfile来执行编译代码。因此,基本上会运行

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

在命令行中

编辑:我的错误.. gcc也可以在此处使用,但是只使用G

很简单

最新更新