1

I'm trying to compile a C++ helloWorld in terminal.

#include <iostream>
using namespace std;

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

I cd to the directory containing the code. I manage to compile with the command g++ -o hello c_helloworld.cpp. But when I use the command gcc -o hello c_helloworld.cpp I get the following error.

Undefined symbols for architecture x86_64: "std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from: std::__1::ctype const& std::__1::use_facet >(std::__1::locale const&) in c_helloworld-a3d3b8.o "std::__1::ios_base::getloc() const", referenced from: std::__1::basic_ios >::widen(char) const in c_helloworld-a3d3b8.o "std::__1::basic_string, std::__1::allocator >::__init(unsigned long, char)", referenced from: std::__1::basic_string, std::__1::allocator >::basic_string(unsigned long, char) in c_helloworld-a3d3b8.o "std::__1::basic_string, std::__1::allocator ::~basic_string()", referenced from: std::__1::ostreambuf_iterator > std::__1::__pad_and_output >(std::__1::ostreambuf_iterator >, char const*, char const*, char const*, std::__1::ios_base&, char) in c_helloworld-a3d3b8.o "std::__1::basic_ostream >::put(char)", referenced from: std::__1::basic_ostream >& std::__1::endl (std::__1::basic_ostream >&) in c_helloworld-a3d3b8.o "std::__1::basic_ostream >::flush()", referenced from: std::__1::basic_ostream >& std::__1::endl (std::__1::basic_ostream >&) in c_helloworld-a3d3b8.o "std::__1::basic_ostream ::sentry::sentry(std::__1::basic_ostream >&)", referenced from: std::__1::basic_ostream >& std::__1::__put_character_sequence (std::__1::basic_ostream >&, char const*, unsigned long) in c_helloworld-a3d3b8.o
"std::__1::basic_ostream ::sentry::~sentry()", referenced from: std::__1::basic_ostream >& std::__1::__put_character_sequence (std::__1::basic_ostream >&, char const*, unsigned long) in c_helloworld-a3d3b8.o "std::__1::cout", referenced from: _main in c_helloworld-a3d3b8.o "std::__1::ctype::id", referenced from: std::__1::ctype const& std::__1::use_facet >(std::__1::locale const&) in c_helloworld-a3d3b8.o "std::__1::locale::~locale()", referenced from: std::__1::basic_ios >::widen(char) const in c_helloworld-a3d3b8.o "std::__1::ios_base::__set_badbit_and_consider_rethrow()", referenced from: std::__1::basic_ostream >& std::__1::__put_character_sequence (std::__1::basic_ostream >&, char const*, unsigned long) in c_helloworld-a3d3b8.o
"std::__1::ios_base::clear(unsigned int)", referenced from: std::__1::ios_base::setstate(unsigned int) in c_helloworld-a3d3b8.o "std::terminate()", referenced from: ___clang_call_terminate in c_helloworld-a3d3b8.o "___cxa_begin_catch", referenced from: std::__1::basic_ostream >& std::__1::__put_character_sequence (std::__1::basic_ostream >&, char const*, unsigned long) in c_helloworld-a3d3b8.o ___clang_call_terminate in c_helloworld-a3d3b8.o "___cxa_call_unexpected", referenced from: std::__1::ostreambuf_iterator >::ostreambuf_iterator(std::__1::basic_ostream >&) in c_helloworld-a3d3b8.o
"___cxa_end_catch", referenced from: std::__1::basic_ostream >& std::__1::__put_character_sequence (std::__1::basic_ostream >&, char const*, unsigned long) in c_helloworld-a3d3b8.o
"___gxx_personality_v0", referenced from: std::__1::basic_ostream >& std::__1::__put_character_sequence (std::__1::basic_ostream >&, char const*, unsigned long) in 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) in c_helloworld-a3d3b8.o std::__1::ostreambuf_iterator >::ostreambuf_iterator(std::__1::basic_ostream >&) in c_helloworld-a3d3b8.o std::__1::basic_ios >::widen(char) const in c_helloworld-a3d3b8.o Dwarf Exception Unwind Info (__eh_frame) in c_helloworld-a3d3b8.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

2 Answers2

2

gcc is used to compile C programs (by default), g++ is for C++. So, the behaviour is expected.

By default, gcc links to the standard C library. If you want to compile C++ programs you can link to the standard C++ library by adding the following option:

gcc -o hello c_helloworld.cpp -lstdc++

PS. I suggest you to search the website before asking a question, there was already an answer for this.

Compiling a C++ program with gcc

ovalb
  • 555
  • 6
  • 15
1

Using g++ is easier for compiling .cpp files. Execute the compiled code by running ./outputfile in the directory. So basically that would be running

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

in the command line

Edit: My mistake.. gcc can be used here as well, but it's simpler to just use g++

Xosrov
  • 719
  • 4
  • 22