Opening 2 data files with C++ - 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...

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++

Error on C++'s Hello World

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.

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

Having Trouble with using makefile with boost threading

Can anyone tell me what I'm doing wrong?
Here's my code main.cpp
#include <iostream>
#include <boost/thread.hpp>
using namespace boost;
using namespace boost::this_thread;
using namespace std;
// Global function called by thread
void GlobalFunction()
{
for (int i=0;i<10;++i)
{
cout << i << "Do something in parallel with main method." << endl;
boost::this_thread::yield();
}
}
void GlobalThreadTest()
{
boost::thread t(&GlobalFunction);
for (int i = 0; i<10; ++i) {
cout << i << "Do something in main method. " << endl;
}
}
int main(int argc, const char * argv[])
{
GlobalThreadTest();
return 0;
}
Make File Code
BOOST_ROOT=/opt/local
PRODUCT_NAME=example
BOOST_INCLUDE_DIR=$(BOOST_ROOT)/include
BOOST_LIB_DIR=/usr/local/lib
BOOST_LINK_FLAGS=lboost_thread-mt
main: main.cpp
gcc main.cpp -I$(BOOST_INCLUDE_DIR) -L$(BOOST_LIB_DIR) -o $(PRODUCT_NAME)
clean:
rm -f main
My boost installation is done through MacPorts and is stored at /opt/local/include
Library folder /usr/local/lib I believe this is the case
Here's the error I'm seeing
$ make -f makefile
gcc main.cpp -I/opt/local/include -L/usr/local/lib -o example
Undefined symbols for architecture x86_64:
"boost::this_thread::yield()", referenced from:
GlobalFunction() in ccPsXnoZ.o
"boost::detail::thread_data_base::~thread_data_base()", referenced from:
boost::detail::thread_data<void (*)()>::~thread_data()in ccPsXnoZ.o
boost::detail::thread_data<void (*)()>::~thread_data()in ccPsXnoZ.o
"boost::system::system_category()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccPsXnoZ.o
boost::thread_exception::thread_exception(int, char const*)in ccPsXnoZ.o
"boost::system::generic_category()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccPsXnoZ.o
"boost::thread::start_thread()", referenced from:
boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)in ccPsXnoZ.o
"boost::thread::detach()", referenced from:
boost::thread::~thread()in ccPsXnoZ.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const", referenced from:
boost::system::system_error::what() constin ccPsXnoZ.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::empty() const", referenced from:
boost::system::system_error::what() constin ccPsXnoZ.o
"std::bad_exception::what() const", referenced from:
vtable for boost::exception_detail::bad_exception_in ccPsXnoZ.o
vtable for boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>in ccPsXnoZ.o
"std::runtime_error::what() const", referenced from:
boost::system::system_error::what() constin ccPsXnoZ.o
"std::bad_alloc::what() const", referenced from:
vtable for boost::exception_detail::bad_alloc_in ccPsXnoZ.o
vtable for boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>in ccPsXnoZ.o
"std::allocator<char>::allocator()", referenced from:
boost::system::system_error::system_error(boost::system::error_code, char const*)in ccPsXnoZ.o
"std::allocator<char>::~allocator()", referenced from:
boost::system::system_error::system_error(boost::system::error_code, char const*)in ccPsXnoZ.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:
GlobalThreadTest() in ccPsXnoZ.o
GlobalFunction() in ccPsXnoZ.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
GlobalThreadTest() in ccPsXnoZ.o
GlobalFunction() in ccPsXnoZ.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)", referenced from:
boost::system::system_error::system_error(boost::system::error_code, char const*)in ccPsXnoZ.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
std::runtime_error::runtime_error(std::runtime_error const&)in ccPsXnoZ.o
boost::system::system_error::system_error(boost::system::system_error const&)in ccPsXnoZ.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()", referenced from:
boost::system::system_error::system_error(boost::system::error_code, char const*)in ccPsXnoZ.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
boost::system::system_error::what() constin ccPsXnoZ.o
boost::system::system_error::~system_error()in ccPsXnoZ.o
boost::system::system_error::~system_error()in ccPsXnoZ.o
boost::system::system_error::~system_error()in ccPsXnoZ.o
boost::system::system_error::system_error(boost::system::error_code, char const*)in ccPsXnoZ.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(char const*)", referenced from:
boost::system::system_error::what() constin ccPsXnoZ.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator+=(char const*)", referenced from:
boost::system::system_error::what() constin ccPsXnoZ.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator+=(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
boost::system::system_error::what() constin ccPsXnoZ.o
"std::bad_exception::~bad_exception()", referenced from:
boost::exception_detail::bad_exception_::~bad_exception_()in ccPsXnoZ.o
boost::exception_detail::bad_exception_::~bad_exception_()in ccPsXnoZ.o
boost::exception_detail::bad_exception_::~bad_exception_()in ccPsXnoZ.o
"std::runtime_error::runtime_error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
boost::system::system_error::system_error(boost::system::error_code, char const*)in ccPsXnoZ.o
"std::runtime_error::~runtime_error()", referenced from:
boost::system::system_error::~system_error()in ccPsXnoZ.o
boost::system::system_error::~system_error()in ccPsXnoZ.o
boost::system::system_error::~system_error()in ccPsXnoZ.o
boost::system::system_error::system_error(boost::system::error_code, char const*)in ccPsXnoZ.o
typeinfo for boost::thread_exceptionin ccPsXnoZ.o
typeinfo for boost::thread_resource_errorin ccPsXnoZ.o
...
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for __cxxabiv1::__vmi_class_type_info", referenced from:
typeinfo for boost::exception_detail::bad_alloc_in ccPsXnoZ.o
typeinfo for boost::exception_detail::bad_exception_in ccPsXnoZ.o
typeinfo for boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>in ccPsXnoZ.o
typeinfo for boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>in ccPsXnoZ.o
typeinfo for boost::exception_detail::error_info_injector<boost::thread_resource_error>in ccPsXnoZ.o
typeinfo for boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error> >in ccPsXnoZ.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for boost::detail::thread_data_base", referenced from:
boost::detail::thread_data_base::thread_data_base()in ccPsXnoZ.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for std::bad_exception", referenced from:
std::bad_exception::bad_exception()in ccPsXnoZ.o
std::bad_exception::bad_exception(std::bad_exception const&)in ccPsXnoZ.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for std::runtime_error", referenced from:
std::runtime_error::runtime_error(std::runtime_error const&)in ccPsXnoZ.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for std::bad_alloc", referenced from:
std::bad_alloc::bad_alloc()in ccPsXnoZ.o
std::bad_alloc::bad_alloc(std::bad_alloc const&)in ccPsXnoZ.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for std::exception", referenced from:
std::exception::exception()in ccPsXnoZ.o
std::exception::exception(std::exception const&)in ccPsXnoZ.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"operator delete(void*)", referenced from:
boost::detail::sp_counted_base::~sp_counted_base()in ccPsXnoZ.o
boost::detail::sp_counted_base::~sp_counted_base()in ccPsXnoZ.o
boost::exception_detail::clone_base::~clone_base()in ccPsXnoZ.o
boost::exception_detail::clone_base::~clone_base()in ccPsXnoZ.o
boost::exception_detail::bad_alloc_::~bad_alloc_()in ccPsXnoZ.o
boost::exception_detail::bad_alloc_::~bad_alloc_()in ccPsXnoZ.o
boost::exception_detail::bad_exception_::~bad_exception_()in ccPsXnoZ.o
...
"operator new(unsigned long)", referenced from:
boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>::clone() constin ccPsXnoZ.o
boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>::clone() constin ccPsXnoZ.o
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error> >::clone() constin ccPsXnoZ.o
boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>*)in ccPsXnoZ.o
boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>()in ccPsXnoZ.o
boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>*)in ccPsXnoZ.o
boost::detail::shared_count::shared_count<boost::detail::thread_data<void (*)()> >(boost::detail::thread_data<void (*)()>*)in ccPsXnoZ.o
...
"___cxa_allocate_exception", referenced from:
boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>::rethrow() constin ccPsXnoZ.o
boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>::rethrow() constin ccPsXnoZ.o
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error> >::rethrow() constin ccPsXnoZ.o
void boost::throw_exception<boost::thread_resource_error>(boost::thread_resource_error const&)in ccPsXnoZ.o
"___cxa_begin_catch", referenced from:
boost::system::system_error::what() constin ccPsXnoZ.o
boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>*)in ccPsXnoZ.o
boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>*)in ccPsXnoZ.o
boost::detail::shared_count::shared_count<boost::detail::thread_data<void (*)()> >(boost::detail::thread_data<void (*)()>*)in ccPsXnoZ.o
"___cxa_call_unexpected", referenced from:
boost::system::system_error::what() constin ccPsXnoZ.o
boost::exception::~exception()in ccPsXnoZ.o
boost::system::system_error::~system_error()in ccPsXnoZ.o
boost::system::system_error::~system_error()in ccPsXnoZ.o
boost::system::system_error::~system_error()in ccPsXnoZ.o
"___cxa_end_catch", referenced from:
boost::system::system_error::what() constin ccPsXnoZ.o
boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>*)in ccPsXnoZ.o
boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>*)in ccPsXnoZ.o
boost::detail::shared_count::shared_count<boost::detail::thread_data<void (*)()> >(boost::detail::thread_data<void (*)()>*)in ccPsXnoZ.o
"___cxa_free_exception", referenced from:
void boost::throw_exception<boost::thread_resource_error>(boost::thread_resource_error const&)in ccPsXnoZ.o
"___cxa_guard_abort", referenced from:
boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>()in ccPsXnoZ.o
boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_exception_>()in ccPsXnoZ.o
"___cxa_guard_acquire", referenced from:
boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>()in ccPsXnoZ.o
boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_exception_>()in ccPsXnoZ.o
"___cxa_guard_release", referenced from:
boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>()in ccPsXnoZ.o
boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_exception_>()in ccPsXnoZ.o
"___cxa_pure_virtual", referenced from:
vtable for boost::detail::sp_counted_basein ccPsXnoZ.o
vtable for boost::exceptionin ccPsXnoZ.o
vtable for boost::exception_detail::clone_basein ccPsXnoZ.o
vtable for boost::system::error_categoryin ccPsXnoZ.o
"___cxa_rethrow", referenced from:
boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>*)in ccPsXnoZ.o
boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>*)in cck3nfxq.o
boost::detail::shared_count::shared_count<boost::detail::thread_data<void (*)()> >(boost::detail::thread_data<void (*)()>*)in cck3nfxq.o
"___cxa_call_unexpected", referenced from:
boost::system::system_error::what() constin cck3nfxq.o
boost::exception::~exception()in cck3nfxq.o
boost::system::system_error::~system_error()in cck3nfxq.o
boost::system::system_error::~system_error()in cck3nfxq.o
boost::system::system_error::~system_error()in cck3nfxq.o
"___cxa_end_catch", referenced from:
boost::system::system_error::what() constin cck3nfxq.o
boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>*)in cck3nfxq.o
boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>*)in cck3nfxq.o
boost::detail::shared_count::shared_count<boost::detail::thread_data<void (*)()> >(boost::detail::thread_data<void (*)()>*)in cck3nfxq.o
"___cxa_free_exception", referenced from:
void boost::throw_exception<boost::thread_resource_error>(boost::thread_resource_error const&)in cck3nfxq.o
"___cxa_guard_abort", referenced from:
boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>()in cck3nfxq.o
boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_exception_>()in cck3nfxq.o
"___cxa_guard_acquire", referenced from:
boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>()in cck3nfxq.o
boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_exception_>()in cck3nfxq.o
"___cxa_guard_release", referenced from:
boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>()in cck3nfxq.o
boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_exception_>()in cck3nfxq.o
"___cxa_pure_virtual", referenced from:
vtable for boost::detail::sp_counted_basein cck3nfxq.o
vtable for boost::exceptionin cck3nfxq.o
vtable for boost::exception_detail::clone_basein cck3nfxq.o
vtable for boost::system::error_categoryin cck3nfxq.o
"___cxa_rethrow", referenced from:
boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>*)in cck3nfxq.o
boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>*)in cck3nfxq.o
boost::detail::shared_count::shared_count<boost::detail::thread_data<void (*)()> >(boost::detail::thread_data<void (*)()>*)in cck3nfxq.o
"___cxa_throw", referenced from:
boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>::rethrow() constin cck3nfxq.o
boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>::rethrow() constin cck3nfxq.o
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error> >::rethrow() constin cck3nfxq.o
void boost::throw_exception<boost::thread_resource_error>(boost::thread_resource_error const&)in cck3nfxq.o
"___gxx_personality_v0", referenced from:
Dwarf Exception Unwind Info (__eh_frame) in cck3nfxq.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [main] Error 1
I can't get it to work with XCode either. I keep seeing compile errors. If anyone has any ideas of how to get the boost thread program to work using xcode 4 please share.
Update*
I added the linker
BOOST_ROOT=/opt/local
PRODUCT_NAME=example
BOOST_INCLUDE_DIR=$(BOOST_ROOT)/include
BOOST_LIB_DIR=/usr/local/lib
BOOST_LINK_FLAGS=lboost_thread-mt
main: main.cpp
g++ main.cpp -I$(BOOST_INCLUDE_DIR) -$(BOOST_LINK_FLAGS) -L$(BOOST_LIB_DIR) -o $(PRODUCT_NAME)
clean:
rm -f main
Error:
make -f makefile
g++ main.cpp -I/opt/local/include -lboost_thread-mt -L/usr/local/lib -o example
ld: library not found for -lboost_thread-mt
collect2: ld returned 1 exit status
make: *** [main] Error 1
You have two errors.
The first is that you try to compile a C++ file with gcc. While it works it doesn't add all libraries and other things needed for C++ files. You should be using g++ instead.
The second is in the Makefile in the line
BOOST_LINK_FLAGS=lboost_thread-mt
That doesn't look like a proper library flag to me. Besides, you don't even have it in your command line when linking, which means you don't link with the library.

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.