Error on C++'s Hello World - c++

I started learning C++ and I tried to run the classic Hello World program:
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello, World!";
return 0;
}
but when I compile it in terminal using GCC I always get that error:
Undefined symbols for architecture x86_64:
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const", referenced from:
std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccpe3DPY.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const", referenced from:
std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccpe3DPY.o
"std::cout", referenced from:
_main in ccpe3DPY.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in ccpe3DPY.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccpe3DPY.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccpe3DPY.o
"___gxx_personality_v0", referenced from:
Dwarf Exception Unwind Info (__eh_frame) in ccpe3DPY.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
can you tell me why?

You've compiled your program with gcc, which is a C compiler, not a C++ compiler. Use g++ instead; it knows to include the C++ run-time libraries, where those missing symbols are defined.

Thats a C compiler you need to use a C++ compiler like rob says above g++. Also if you are a beginner I would highly recommend using Microsoft visual studio express if you run a windows OS; if not then netbeans.

Related

Compiling hello world.cpp in terminal (macOS) [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Compiling a C++ program with GCC
(9 answers)
Closed 3 years ago.
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)
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
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++

Issue installing openmp on mac using homebrew

I'm trying to use Openmp on my mac, but i'm getting a compiler error I don't understand. I installed openmp using
brew install llvm
When I try to compile my code using -fopenmp I get errors like this
Undefined symbols for architecture x86_64:
"std::runtime_error::what() const", referenced from:
vtable for boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::runtime_error> > in ccOEbbPM.o
vtable for boost::exception_detail::error_info_injector<std::runtime_error> in ccOEbbPM.o
vtable for boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::range_error> > in ccOEbbPM.o
vtable for boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::overflow_error> > in ccOEbbPM.o
vtable for boost::exception_detail::error_info_injector<std::range_error> in ccOEbbPM.o
vtable for boost::exception_detail::error_info_injector<std::overflow_error> in ccOEbbPM.o
"std::locale::operator==(std::locale const&) const", referenced from:
boost::detail::lcast_put_unsigned<std::char_traits<char>, unsigned long long, char>::convert() in ccOEbbPM.o
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::find_first_not_of(char, unsigned long) const", referenced from:
boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1, (boost::multiprecision::cpp_int_check_type)0, std::allocator<unsigned long long> >::do_get_string[abi:cxx11](std::_Ios_Fmtflags, mpl_::bool_<false> const&) const in ccOEbbPM.o
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const", referenced from:
std::basic_ostream<char, std::char_traits<char> >& boost::multiprecision::operator<< <boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1, (boost::multiprecision::cpp_int_check_type)0, std::allocator<unsigned long long> >, (boost::multiprecision::expression_template_option)1>(std::basic_ostream<char, std::char_traits<char> >&, boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1, (boost::multiprecision::cpp_int_check_type)0, std::allocator<unsigned long long> >, (boost::multiprecision::expression_template_option)1> const&) in ccOEbbPM.o
It's just a small segment of the error code since it goes on for quite a while, but I post the rest if need be. I also have the boost library installed.
Any help is appreciated, thanks.
EDIT:
Simple program that causes the error:
#include <iostream>
#include <omp.h>
using namespace std;
int main(){
int x = 5;
#pragma omp parallel
for(int y = 0; y < 50;y++){
x += 5;
}
cout << x << endl;
return 0;
}
Commands used to compile:
gcc-7 -fopenmp -Wall ex.cc
gcc-7 -std=c++11 -fopenmp -Wall ex.cc (I get less errors doing it this way)
Error when using the second compile command:
Undefined symbols for architecture x86_64:
"std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)
(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
_main in ccXt8Pa8.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
_main in ccXt8Pa8.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccXt8Pa8.o
"std::ios_base::Init::~Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccXt8Pa8.o
"std::cout", referenced from:
_main in ccXt8Pa8.o
"std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
_main in ccXt8Pa8.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
Use g++-7 rather than gcc-7, and all will be fine - it is C++ after all ;-)
g++-7 -std=c++11 -fopenmp openmp.cpp -o openmp

Opening 2 data files with C++

I wrote a program to create gnuplot commands according to data that was read from an input file. I required far too many commands to do this by hand. This worked quite nicely, but I now need to read data from two different data files and use these to create the gnuplot commands. Unfortunately something now seems to be going wrong with the reading of the two files. The portion of code involved in reading from the data files is as follows:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int n_snapshots;
int main () {
std::cout << "Enter number of snapshots" << "\n";
std::cin >> n_snapshots;
int snap_cell_count[n_snapshots];
std::ifstream in_file_count("data/snapshot_data");
std::ifstream in_file_fates("data/cell_fates_data_final");
for (int i=0; i<n_snapshots; i++) {
in_file_count >> snap_cell_count[i];
}
in_file_count.close();
int cell_fates[snap_cell_count[n_snapshots-1]];
for (int i=0; i<snap_cell_count[n_snapshots-1]; i++) {
in_file_fates >> cell_fates[i];
}
in_file_fates.close();
n_snapshots is simply some integer, snap_cell_count[] is an array with "n_shapshots" elements, each of which takes a value read from the data file "snapshot_data". cell_fates[] is an array with a number of elements that is equal to the value of the last element in snap_cell_count[], and again the values of its elements are read from a file, in this case "cell_fates_data_final". The data files to be read from are stored in a folder called "data".
Unfortunately, the compiler returns the following errors.
Undefined symbols for architecture x86_64:
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const", referenced from:
_main in ccrpSAv5.o
"std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::str() const", referenced from:
_main in ccrpSAv5.o
"std::basic_istream<char, std::char_traits<char> >::operator>>(int&)", referenced from:
_main in ccrpSAv5.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
_main in ccrpSAv5.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
_main in ccrpSAv5.o
"std::basic_ifstream<char, std::char_traits<char> >::close()", referenced from:
_main in ccrpSAv5.o
"std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode)", referenced from:
_main in ccrpSAv5.o
"std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()", referenced from:
_main in ccrpSAv5.o
"std::basic_ofstream<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode)", referenced from:
_main in ccrpSAv5.o
"std::basic_ofstream<char, std::char_traits<char> >::close()", referenced from:
_main in ccrpSAv5.o
"std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream()", referenced from:
_main in ccrpSAv5.o
"std::basic_ofstream<char, std::char_traits<char> >::~basic_ofstream()", referenced from:
_main in ccrpSAv5.o
"std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(std::_Ios_Openmode)", referenced from:
_main in ccrpSAv5.o
"std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_stringstream()", referenced from:
_main in ccrpSAv5.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccrpSAv5.o
"std::ios_base::Init::~Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccrpSAv5.o
"std::cin", referenced from:
_main in ccrpSAv5.o
"std::cout", referenced from:
_main in ccrpSAv5.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in ccrpSAv5.o
"___gxx_personality_v0", referenced from:
Dwarf Exception Unwind Info (__eh_frame) in ccrpSAv5.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
This is being compiled with the gcc compiler on Mac OSX Mountain Lion.
Does anyone have any idea what's wrong here?
Well, for starters you can't just say int snap_cell_count[n_snapshots] with the value of n_snapshots being determined at runtime. You have to do something like
int *snap_cell_count = new int[n_snapshots];
// do some stuff
delete[] snap_cell_count;
Same thing for cell_fates
As for your linker errors, maybe this question solves your issues...

C++ : Undefined symbols for architecture x86_64 on MacOS Mountain Lion

Ok, I just switched to MacOS after a while of Windows/Linux coexistence. I open a simple text editor, write a simple program just to check if everything is all right. I know there are other threads about this error but it just seems that in all the other cases the problem lied in a specific part of their program, which in turn is far more complex than this. I just want to understand why the compiler says what it says even with a simple thing like this.
#include <iostream>
#include "numeri.h"
int main(void)
{
std::cout << numeri() << std::endl;
return 0;
}
with
//numeri.h
int numeri(void);
and
//numeri.cpp
#include "numeri.h"
int numeri(void)
{
return (3);
}
what I get from gcc helloworld.cpp -Wall is
Undefined symbols for architecture x86_64:
"numeri()", referenced from:
_main in cc6WY2MJ.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
_main in cc6WY2MJ.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
_main in cc6WY2MJ.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in cc6WY2MJ.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in cc6WY2MJ.o
"std::cout", referenced from:
_main in cc6WY2MJ.o
"std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
_main in cc6WY2MJ.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
2 cases:
1 - I forgot how to use a simple header (I prefix, I'm a physics student, I can program for what it regards physics and numeric simulations [or at least, I could], but I just don't know that much about linkers-compilers-architecture and this kind of stuff)
2 - something's wrong
The C++ compiler is g++, . Also you need to add numeri.cpp to your commmand line.
g++ helloworld.cpp numeri.cpp -Wall

Why doesn't make work on a C++ compile with a reference address pointer?

I am a computer science student and taking my first C++ class. I have a problem understanding what is going on with my code:
// This program uses the address of each element in the array.
#include <iostream>
using namespace std;
int main()
{
const int NUM_COINS = 5;
int coins[NUM_COINS] = {5, 1, 25, 5, 10};
int *p1; // Pointer to a double.
int count; // Counter variable.
// Use the pointer to display the values in the array.
cout << "Here are the values in the coins array: \n";
for(count = 0; count << NUM_COINS; count++)
{
// Get the address of an array element
p1 = &coins[count];
// Display the contents of the element
cout << *p1;
}
cout << endl;
return 0;
}
so my first question is why doesn't make compile it? I have no problems at all with any of my other simple programs. I am using g++ on OS X 4.2.1. I have to type the g++ -o command for it to compile, if not...i get these errors:
g++ -c -o 9-8.o 9-8.cpp cc 9-8.o
-o 9-8 Undefined symbols: "std::basic_ostream >&
std::operator<<
(std::basic_ostream >&, char
const*)", referenced from:
_main in 9-8.o
_main in 9-8.o "std::ios_base::Init::Init()",
referenced from:
__static_initialization_and_destruction_0(int, int)in 9-8.o
"std::basic_string,
std::allocator >::size() const",
referenced from:
std::__verify_grouping(char const*, unsigned long,
std::basic_string,
std::allocator > const&)in 9-8.o
"std::basic_string,
std::allocator
::operator[](unsigned long) const", referenced from:
std::__verify_grouping(char const*, unsigned long,
std::basic_string,
std::allocator > const&)in 9-8.o
std::__verify_grouping(char const*, unsigned long,
std::basic_string,
std::allocator > const&)in 9-8.o
std::__verify_grouping(char const*, unsigned long,
std::basic_string,
std::allocator > const&)in 9-8.o
"___gxx_personality_v0", referenced
from:
std::__verify_grouping(char const*, unsigned long,
std::basic_string,
std::allocator > const&)in 9-8.o
___tcf_0 in 9-8.o
_main in 9-8.o
unsigned long const& std::min(unsigned long
const&, unsigned long const&)in 9-8.o
__static_initialization_and_destruction_0(int, int)in 9-8.o
global constructors keyed to mainin 9-8.o
CIE in 9-8.o "std::ios_base::Init::~Init()",
referenced from:
___tcf_0 in 9-8.o "std::basic_ostream >&
std::endl
(std::basic_ostream >&)",
referenced from:
_main in 9-8.o "std::basic_ostream
::operator<<(std::basic_ostream >&
(*)(std::basic_ostream >&))",
referenced from:
_main in 9-8.o "std::basic_ostream
::operator<<(int)", referenced from:
_main in 9-8.o "std::cout", referenced from:
_main in 9-8.o
_main in 9-8.o
_main in 9-8.o ld: symbol(s) not found collect2: ld returned 1 exit
status make: *** [9-8] Error 1
which leads to my second question. Even if I do type the g++ command, it compiles but after running it outputs an empty array. So my question #2 is: is my code correct? How do I properly use pointers with the reference address statement?
Reason: You are not using the comparision operator correctly. After changing it to be "<", your code should work correctly.
for(count = 0; count << NUM_COINS; count++)
^ should be "<" here
I don't see any problem except that one problem in your for loop:
for(count = 0; count << NUM_COINS; count++)
//^^
That is not comparison. That is left-shift operation. I'm sure you didn't intend that.
That should be : count < NUM_COINS.