【问题标题】:Compilation error with C++ (macOS)C++ (macOS) 的编译错误
【发布时间】:2018-02-21 21:35:47
【问题描述】:

当我尝试在 Mac 中编译这个非常简单的 C++ 程序时出现错误(HighSierra:版本 10.13.3)。这台 Mac 使用的 gcc 版本是 5.3.0。

这是 C++ 程序:

#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;

int main() 
{
  cout<<sin(1);
} 

根据先前问题的答案,我使用 xcode-select --install 安装了命令行工具

上述命令行安装后,当我尝试使用编译这个程序时

gcc filename.cpp -o filename.out

我收到以下错误:

/var/folders/mp/z7xpkw3538z71904cdqhztv00000gn/T//ccGJEHr7.s:24:11: warning: section "__textcoal_nt" is deprecated
    .section __TEXT,__textcoal_nt,coalesced,pure_instructions
             ^      ~~~~~~~~~~~~~
/var/folders/mp/z7xpkw3538z71904cdqhztv00000gn/T//ccGJEHr7.s:24:11: note: change section name to "__text"
    .section __TEXT,__textcoal_nt,coalesced,pure_instructions
             ^      ~~~~~~~~~~~~~
Undefined symbols for architecture x86_64:
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(double)", referenced from:
      _main in ccpjcybF.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in ccpjcybF.o
  "std::ios_base::Init::~Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in ccpjcybF.o
  "std::cout", referenced from:
      _main in ccpjcybF.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

我该如何解决这个问题?


更新:

按照答案,现在我使用g++,即

g++ filename.cpp -o filename.out

现在,我收到以下错误:

/var/folders/mp/z7xpkw3538z71904cdqhztv00000gn/T//ccJjEuJy.s:24:11: warning: section "__textcoal_nt" is deprecated
    .section __TEXT,__textcoal_nt,coalesced,pure_instructions
             ^      ~~~~~~~~~~~~~
/var/folders/mp/z7xpkw3538z71904cdqhztv00000gn/T//ccJjEuJy.s:24:11: note: change section name to "__text"
    .section __TEXT,__textcoal_nt,coalesced,pure_instructions
             ^      ~~~~~~~~~~~~~

【问题讨论】:

    标签: c++ macos gcc compilation g++


    【解决方案1】:

    您正在使用 gcc 编译 C 源代码。

    这就是问题所在,您应该改用g++

    【讨论】:

      【解决方案2】:

      你可以尝试使用g++:

      g++ filename.cpp -o filename.out
      

      或者苹果的clang++

      clang++ filename.cpp -o filename.out
      

      【讨论】:

        猜你喜欢
        • 2014-05-18
        • 2018-05-12
        • 2019-04-01
        • 2015-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多