简体   繁体   中英

Compilation error with C++ (macOS)

I am getting an error when I am trying to compile this very simple C++ program in Mac (HighSierra: Version 10.13.3). The gcc version being used in this Mac is 5.3.0.

This is the C++ program:

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

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

Following an answer from an earlier question, I installed command line tools using xcode-select --install

After the aforementioned command line installation, when I try to compile this program using

gcc filename.cpp -o filename.out

I get the following error:

/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

How can I solve this problem?


Update:

Following the answers, now I used g++ , ie

g++ filename.cpp -o filename.out

Now, I am getting the following error:

/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
             ^      ~~~~~~~~~~~~~

You are compiling C source code with gcc .

That's the problem, you should be using g++ instead.

You could try to use g++ :

g++ filename.cpp -o filename.out

Or Apple's clang++ :

clang++ filename.cpp -o filename.out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM