簡體   English   中英

C ++(macOS)的編譯錯誤

[英]Compilation error with C++ (macOS)

嘗試在Mac(HighSierra:10.13.3版)中編譯此非常簡單的C ++程序時遇到錯誤。 在此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
             ^      ~~~~~~~~~~~~~

您正在使用gcc編譯C源代碼。

就是這個問題,您應該改用g++

您可以嘗試使用g++

g++ filename.cpp -o filename.out

或Apple的clang++

clang++ filename.cpp -o filename.out

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM