Having Trouble with using makefile with boost threading - c++

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.

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

CMake works when I link a dynamic library but not a static library

When I link a dynamic library with CMake, create the make file and then use make, it compiles and builds correctly.
When I link a static library, CMake doesn't complain but when I run make, it throws a linker error at me, even though all I did was replace the .dylib with a .a of the same name.
This is my CMake file linking to a static library:
cmake_minimum_required (VERSION 3.0)
add_executable (TEST main.cpp)
include_directories ("${CMAKE_SOURCE_DIR}/Dependencies/libpqxx/include")
target_link_libraries (TEST "${CMAKE_SOURCE_DIR}/Dependencies/libpqxx/lib/libpqxx.a")
Any ideas?
Output for cmake:
>> cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/john/Documents/Test/src
Output for make:
>> make
[ 33%] Linking CXX executable TEST
Undefined symbols for architecture x86_64:
"_PQbackendPID", referenced from:
pqxx::connection_base::backendpid() const in libpqxx.a(connection_base.o)
"_PQcancel", referenced from:
pqxx::connection_base::cancel_query() in libpqxx.a(connection_base.o)
"_PQclear", referenced from:
pqxx::internal::clear_result(pg_result const*) in libpqxx.a(result.o)
"_PQclientEncoding", referenced from:
pqxx::connection_base::encoding_code() in libpqxx.a(connection_base.o)
"_PQcmdStatus", referenced from:
pqxx::result::cmd_status() const in libpqxx.a(result.o)
"_PQcmdTuples", referenced from:
pqxx::result::affected_rows() const in libpqxx.a(result.o)
"_PQconnectPoll", referenced from:
pqxx::connect_async::do_completeconnect(pg_conn*) in libpqxx.a(connection.o)
"_PQconnectStart", referenced from:
pqxx::connect_async::do_startconnect(pg_conn*) in libpqxx.a(connection.o)
"_PQconnectdb", referenced from:
pqxx::connectionpolicy::normalconnect(pg_conn*) in libpqxx.a(connection.o)
"_PQconsumeInput", referenced from:
pqxx::connection_base::consume_input() in libpqxx.a(connection_base.o)
"_PQdb", referenced from:
pqxx::connection_base::dbname() in libpqxx.a(connection_base.o)
"_PQencryptPassword", referenced from:
pqxx::encrypt_password(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in libpqxx.a(connection_base.o)
"_PQendcopy", referenced from:
pqxx::connection_base::write_copy_line(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in libpqxx.a(connection_base.o)
"_PQerrorMessage", referenced from:
pqxx::connection_base::err_msg() const in libpqxx.a(connection_base.o)
pqxx::connectionpolicy::normalconnect(pg_conn*) in libpqxx.a(connection.o)
pqxx::connect_direct::do_startconnect(pg_conn*) in libpqxx.a(connection.o)
pqxx::connect_async::do_startconnect(pg_conn*) in libpqxx.a(connection.o)
pqxx::connect_async::do_completeconnect(pg_conn*) in libpqxx.a(connection.o)
"_PQescapeByteaConn", referenced from:
pqxx::connection_base::esc_raw(unsigned char const*, unsigned long) in libpqxx.a(connection_base.o)
"_PQescapeIdentifier", referenced from:
pqxx::connection_base::quote_name(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in libpqxx.a(connection_base.o)
"_PQescapeStringConn", referenced from:
pqxx::connection_base::esc(char const*, unsigned long) in libpqxx.a(connection_base.o)
"_PQexec", referenced from:
pqxx::connection_base::exec(char const*, int) in libpqxx.a(connection_base.o)
pqxx::connection_base::add_receiver(pqxx::notification_receiver*) in libpqxx.a(connection_base.o)
"_PQexecParams", referenced from:
pqxx::connection_base::parameterized_exec(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const* const*, int const*, int const*, int) in libpqxx.a(connection_base.o)
pqxx::connection_base::exec_params(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, pqxx::internal::params const&) in libpqxx.a(connection_base.o)
"_PQexecPrepared", referenced from:
pqxx::connection_base::prepared_exec(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const* const*, int const*, int const*, int) in libpqxx.a(connection_base.o)
pqxx::connection_base::exec_prepared(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, pqxx::internal::params const&) in libpqxx.a(connection_base.o)
"_PQfinish", referenced from:
pqxx::connectionpolicy::normalconnect(pg_conn*) in libpqxx.a(connection.o)
pqxx::connectionpolicy::do_disconnect(pg_conn*) in libpqxx.a(connection.o)
"_PQfname", referenced from:
pqxx::result::column_name(unsigned int) const in libpqxx.a(result.o)
"_PQfnumber", referenced from:
pqxx::result::column_number(char const*) const in libpqxx.a(row.o)
"_PQfreeCancel", referenced from:
(anonymous namespace)::cancel_wrapper::~cancel_wrapper() in libpqxx.a(connection_base.o)
"_PQfreemem", referenced from:
pqxx::internal::freepqmem(void const*) in libpqxx.a(util.o)
"_PQftable", referenced from:
pqxx::result::column_table(unsigned int) const in libpqxx.a(result.o)
"_PQftablecol", referenced from:
pqxx::result::table_column(unsigned int) const in libpqxx.a(result.o)
"_PQftype", referenced from:
pqxx::result::column_type(unsigned int) const in libpqxx.a(result.o)
"_PQgetCancel", referenced from:
pqxx::connection_base::cancel_query() in libpqxx.a(connection_base.o)
"_PQgetCopyData", referenced from:
pqxx::connection_base::read_copy_line(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) in libpqxx.a(connection_base.o)
"_PQgetResult", referenced from:
pqxx::connection_base::set_up_state() in libpqxx.a(connection_base.o)
pqxx::connection_base::read_copy_line(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) in libpqxx.a(connection_base.o)
pqxx::connection_base::end_copy_write() in libpqxx.a(connection_base.o)
pqxx::connection_base::get_result() in libpqxx.a(connection_base.o)
"_PQgetisnull", referenced from:
pqxx::result::get_is_null(unsigned long, unsigned int) const in libpqxx.a(result.o)
"_PQgetlength", referenced from:
pqxx::result::get_length(unsigned long, unsigned int) const in libpqxx.a(result.o)
"_PQgetvalue", referenced from:
pqxx::result::GetValue(unsigned long, unsigned int) const in libpqxx.a(result.o)
"_PQhost", referenced from:
pqxx::connection_base::hostname() in libpqxx.a(connection_base.o)
"_PQisBusy", referenced from:
pqxx::connection_base::is_busy() const in libpqxx.a(connection_base.o)
"_PQisthreadsafe", referenced from:
pqxx::describe_thread_safety() in libpqxx.a(util.o)
"_PQnfields", referenced from:
pqxx::result::columns() const in libpqxx.a(result.o)
"_PQnotifies", referenced from:
pqxx::connection_base::get_notifs() in libpqxx.a(connection_base.o)
"_PQntuples", referenced from:
pqxx::result::size() const in libpqxx.a(result.o)
pqxx::result::empty() const in libpqxx.a(result.o)
"_PQoidValue", referenced from:
pqxx::result::inserted_oid() const in libpqxx.a(result.o)
"_PQport", referenced from:
pqxx::connection_base::port() in libpqxx.a(connection_base.o)
"_PQprepare", referenced from:
pqxx::connection_base::register_prepared(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in libpqxx.a(connection_base.o)
"_PQprotocolVersion", referenced from:
pqxx::connection_base::protocol_version() const in libpqxx.a(connection_base.o)
"_PQputCopyData", referenced from:
pqxx::connection_base::write_copy_line(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in libpqxx.a(connection_base.o)
"_PQputCopyEnd", referenced from:
pqxx::connection_base::end_copy_write() in libpqxx.a(connection_base.o)
"_PQreset", referenced from:
pqxx::connection_base::reset() in libpqxx.a(connection_base.o)
"_PQresultErrorField", referenced from:
pqxx::result::ThrowSQLError(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const in libpqxx.a(result.o)
pqxx::result::errorposition() const in libpqxx.a(result.o)
"_PQresultErrorMessage", referenced from:
pqxx::result::StatusError() const in libpqxx.a(result.o)
"_PQresultStatus", referenced from:
pqxx::result::StatusError() const in libpqxx.a(result.o)
"_PQsendQuery", referenced from:
pqxx::connection_base::set_up_state() in libpqxx.a(connection_base.o)
pqxx::connection_base::start_exec(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in libpqxx.a(connection_base.o)
"_PQserverVersion", referenced from:
pqxx::connection_base::read_capabilities() in libpqxx.a(connection_base.o)
"_PQsetErrorVerbosity", referenced from:
pqxx::connection_base::set_verbosity(pqxx::connection_base::error_verbosity) in libpqxx.a(connection_base.o)
"_PQsetNoticeProcessor", referenced from:
pqxx::connection_base::set_up_state() in libpqxx.a(connection_base.o)
pqxx::connection_base::close() in libpqxx.a(connection_base.o)
"_PQsocket", referenced from:
(anonymous namespace)::socket_of(pg_conn const*) in libpqxx.a(connection_base.o)
"_PQstatus", referenced from:
pqxx::connection_base::status() const in libpqxx.a(connection_base.o)
pqxx::connectionpolicy::normalconnect(pg_conn*) in libpqxx.a(connection.o)
pqxx::connect_direct::do_startconnect(pg_conn*) in libpqxx.a(connection.o)
pqxx::connect_async::do_startconnect(pg_conn*) in libpqxx.a(connection.o)
"_PQtrace", referenced from:
pqxx::connection_base::internal_set_trace() in libpqxx.a(connection_base.o)
"_PQunescapeBytea", referenced from:
pqxx::connection_base::unesc_raw(char const*) in libpqxx.a(connection_base.o)
"_PQuntrace", referenced from:
pqxx::connection_base::internal_set_trace() in libpqxx.a(connection_base.o)
"_PQuser", referenced from:
pqxx::connection_base::username() in libpqxx.a(connection_base.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [TEST] Error 1
make[1]: *** [CMakeFiles/TEST.dir/all] Error 2
make: *** [all] Error 2
if you send error messages it'd be better , but try this:
link_directories(${CMAKE_SOURCE_DIR}/Dependencies/libpqxx/lib)
target_link_libraries (TEST pqxx)
I've figured it out. I was just missing some dependencies for the pqxx library. I've added a reference to libpq.a, libssl.a, and libcrypto.a as well as their include/ directories and it ended up working. I guess .dylib files link to dependencies by themselves but you have to manually include them with static libraries?

How to get face_detection_ex.cpp in dlib to work in c++?

I'm new to c++, so sorry if there is an obvious solution I'm missing. I'm trying to run this example code for dlib in c++. I'm getting this error...
In file included from /Users/john/ClionProjects/dlib/examples/face_detection_ex.cpp:41:
In file included from /Users/john/ClionProjects/dlib/dlib/../dlib/gui_widgets.h:13:
In file included from /Users/john/ClionProjects/dlib/dlib/../dlib/image_processing/../gui_widgets/widgets.h:9:
In file included from /Users/john/ClionProjects/dlib/dlib/../dlib/image_processing/../gui_widgets/drawable.h:8:
In file included from /Users/john/ClionProjects/dlib/dlib/../dlib/image_processing/../gui_widgets/../gui_core.h:14:
In file included from /Users/john/ClionProjects/dlib/dlib/../dlib/image_processing/../gui_core/xlib.h:4:
/Users/john/ClionProjects/dlib/dlib/../dlib/image_processing/../gui_core/gui_core_kernel_2.h:11:2: error: "DLIB_NO_GUI_SUPPORT is defined so you can't use the GUI code. Turn DLIB_NO_GUI_SUPPORT off if you want to use it."
#error "DLIB_NO_GUI_SUPPORT is defined so you can't use the GUI code. Turn DLIB_NO_GUI_SUPPORT off if you want to use it."
^
/Users/johnoberhauser/ClionProjects/dlib/dlib/../dlib/image_processing/../gui_core/gui_core_kernel_2.h:12:2: error: "Also make sure you have libx11-dev installed on your system"
#error "Also make sure you have libx11-dev installed on your system"
I've tried going into /dlib/CMakeLists.txt and adding
set(DLIB_NO_GUI_SUPPORT OFF)
I know it's found the X11 package as I'm not getting a message from the cmake there (and I just installed XQuartz)
Also, I'm using Clion, so I'm not running cmake from the command line. And I'm on OSX
Edit: I tried removing the parts of the code that use the gui, just to see if it would work. But I then get this error:
Undefined symbols for architecture x86_64:
"_USER_ERROR__missing_dlib_all_source_cpp_file__OR__inconsistent_use_of_DEBUG_or_ENABLE_ASSERTS_preprocessor_directives", referenced from:
_dlib_check_consistent_assert_usage in main.cpp.o
"_cblas_saxpy", referenced from:
dlib::blas_bindings::cblas_axpy(int, float, float const*, int, float*, int) in main.cpp.o
"_cblas_sscal", referenced from:
dlib::blas_bindings::cblas_scal(int, float, float*) in main.cpp.o
"_dgesvd_", referenced from:
dlib::lapack::binding::gesvd(char, char, int, int, double*, int, double*, double*, int, double*, int, double*, int) in main.cpp.o
"_png_create_info_struct", referenced from:
dlib::png_loader::read_image(char const*) in libdlib.a(png_loader.cpp.o)
"_png_create_read_struct", referenced from:
dlib::png_loader::read_image(char const*) in libdlib.a(png_loader.cpp.o)
"_png_destroy_read_struct", referenced from:
dlib::png_loader::read_image(char const*) in libdlib.a(png_loader.cpp.o)
dlib::png_loader::~png_loader() in libdlib.a(png_loader.cpp.o)
dlib::png_loader::~png_loader() in libdlib.a(png_loader.cpp.o)
"_png_get_bit_depth", referenced from:
dlib::png_loader::read_image(char const*) in libdlib.a(png_loader.cpp.o)
"_png_get_color_type", referenced from:
dlib::png_loader::read_image(char const*) in libdlib.a(png_loader.cpp.o)
"_png_get_image_height", referenced from:
dlib::png_loader::read_image(char const*) in libdlib.a(png_loader.cpp.o)
"_png_get_image_width", referenced from:
dlib::png_loader::read_image(char const*) in libdlib.a(png_loader.cpp.o)
"_png_get_rows", referenced from:
dlib::png_loader::read_image(char const*) in libdlib.a(png_loader.cpp.o)
"_png_init_io", referenced from:
dlib::png_loader::read_image(char const*) in libdlib.a(png_loader.cpp.o)
"_png_read_png", referenced from:
dlib::png_loader::read_image(char const*) in libdlib.a(png_loader.cpp.o)
"_png_set_longjmp_fn", referenced from:
dlib::png_loader::read_image(char const*) in libdlib.a(png_loader.cpp.o)
dlib::png_loader_user_error_fn_silent(png_struct_def*, char const*) in libdlib.a(png_loader.cpp.o)
"_png_set_palette_to_rgb", referenced from:
dlib::png_loader::read_image(char const*) in libdlib.a(png_loader.cpp.o)
"_png_set_sig_bytes", referenced from:
dlib::png_loader::read_image(char const*) in libdlib.a(png_loader.cpp.o)
"_png_sig_cmp", referenced from:
dlib::png_loader::read_image(char const*) in libdlib.a(png_loader.cpp.o)
ld: symbol(s) not found for architecture x86_64
I think this is a separate issue from the first one, but idk, maybe they are related? For this issue, I've tried doing what was mentioned here https://github.com/davisking/dlib/issues/57 I made the link for the first part, then added these lines to my cmakelist.txt
include_directories(../../../../usr/X11/lib)
include_directories(../../../../usr/local/include)
include_directories(../../../../usr/local/lib)
Didn't seem to fix the issue though.
Edit 2: So I think both these issues are resolved now. I think added those include_directories lines that I mentioned above help. Also, I started using cmake from the command line, instead of with clion.
However, I am now getting a new error :/
Undefined symbols for architecture x86_64:
"dlib::base_window::invalidate_rectangle(dlib::rectangle const&)", referenced from:
void dlib::image_display::set_image<dlib::array2d<unsigned char, dlib::memory_manager_stateless_kernel_1<char> > >(dlib::array2d<unsigned char, dlib::memory_manager_stateless_kernel_1<char> > const&) in main.cpp.o
"dlib::base_window::set_size(int, int)", referenced from:
void dlib::image_window::set_image<dlib::array2d<unsigned char, dlib::memory_manager_stateless_kernel_1<char> > >(dlib::array2d<unsigned char, dlib::memory_manager_stateless_kernel_1<char> > const&) in main.cpp.o
"dlib::image_window::add_overlay(std::__1::vector<dlib::image_display::overlay_rect, std::__1::allocator<dlib::image_display::overlay_rect> > const&)", referenced from:
void dlib::image_window::add_overlay<dlib::rgb_pixel>(std::__1::vector<dlib::rectangle, std::__1::allocator<dlib::rectangle> > const&, dlib::rgb_pixel) in main.cpp.o
"dlib::image_window::clear_overlay()", referenced from:
_main in main.cpp.o
"dlib::image_window::image_window()", referenced from:
_main in main.cpp.o
"dlib::image_window::~image_window()", referenced from:
_main in main.cpp.o
"dlib::popup_menu_region::disable()", referenced from:
void dlib::image_display::set_image<dlib::array2d<unsigned char, dlib::memory_manager_stateless_kernel_1<char> > >(dlib::array2d<unsigned char, dlib::memory_manager_stateless_kernel_1<char> > const&) in main.cpp.o
"dlib::scrollable_region::set_total_rect_size(unsigned long, unsigned long)", referenced from:
void dlib::image_display::set_image<dlib::array2d<unsigned char, dlib::memory_manager_stateless_kernel_1<char> > >(dlib::array2d<unsigned char, dlib::memory_manager_stateless_kernel_1<char> > const&) in main.cpp.o
"dlib::draw_sunken_rectangle(dlib::canvas const&, dlib::rectangle const&, unsigned char)", referenced from:
dlib::scrollable_region_style_default::draw_scrollable_region_border(dlib::canvas const&, dlib::rectangle const&, bool) const in main.cpp.o
"dlib::image_display::get_image_display_rect() const", referenced from:
void dlib::image_window::set_image<dlib::array2d<unsigned char, dlib::memory_manager_stateless_kernel_1<char> > >(dlib::array2d<unsigned char, dlib::memory_manager_stateless_kernel_1<char> > const&) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
One issue after another :/
Edit 3: Success! So I was using Clion for compiling. Even though the cmake from clion is the same version as the cmake I'm using in the command line, it just didn't work for some reason.
I solved this by deleting the "cmake-build-debug" directory.
Then reopen the project(CLion will regenerate it).
It seems there remain some cache files would affect the compiling.
But before this step, please make sure you do install XQuartz.

xcode 7.2.1+ os 10.11.9 + opencv3.1.0 Undefined symbols for architecture x86_64?

I have a cpp file which works fine with g++ before upgrading these stuff.
Currently, when running : g++ -I/usr/local/Cellar/opencv3/3.1.0_1/include -stdlib=libc++ bs.cpp -o bs
Results are following:
Undefined symbols for architecture x86_64:
"cv::namedWindow(cv::String const&, int)", referenced from:
_main in bs-df9f1a.o
"cv::VideoCapture::read(cv::_OutputArray const&)", referenced from:
processVideo(char*) in bs-df9f1a.o
"cv::VideoCapture::release()", referenced from:
processVideo(char*) in bs-df9f1a.o
"cv::VideoCapture::VideoCapture(cv::String const&)", referenced from:
processVideo(char*) in bs-df9f1a.o
"cv::VideoCapture::~VideoCapture()", referenced from:
processVideo(char*) in bs-df9f1a.o
"cv::destroyAllWindows()", referenced from:
_main in bs-df9f1a.o
"cv::createBackgroundSubtractorMOG2(int, double, bool)", referenced from:
_main in bs-df9f1a.o
"cv::Mat::deallocate()", referenced from:
cv::Mat::release() in bs-df9f1a.o
"cv::Mat::copySize(cv::Mat const&)", referenced from:
cv::Mat::operator=(cv::Mat const&) in bs-df9f1a.o
"cv::String::deallocate()", referenced from:
cv::String::~String() in bs-df9f1a.o
"cv::String::allocate(unsigned long)", referenced from:
cv::String::String(char const*) in bs-df9f1a.o
cv::String::String(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in bs-df9f1a.o
"cv::imread(cv::String const&, int)", referenced from:
processImages(char*) in bs-df9f1a.o
"cv::imshow(cv::String const&, cv::_InputArray const&)", referenced from:
processVideo(char*) in bs-df9f1a.o
processImages(char*) in bs-df9f1a.o
"cv::putText(cv::_InputOutputArray const&, cv::String const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)", referenced from:
processVideo(char*) in bs-df9f1a.o
processImages(char*) in bs-df9f1a.o
"cv::waitKey(int)", referenced from:
processVideo(char*) in bs-df9f1a.o
processImages(char*) in bs-df9f1a.o
"cv::fastFree(void*)", referenced from:
cv::Mat::~Mat() in bs-df9f1a.o
"cv::rectangle(cv::_InputOutputArray const&, cv::Point_<int>, cv::Point_<int>, cv::Scalar_<double> const&, int, int, int)", referenced from:
processVideo(char*) in bs-df9f1a.o
processImages(char*) in bs-df9f1a.o
"cv::VideoCapture::get(int) const", referenced from:
processVideo(char*) in bs-df9f1a.o
"cv::VideoCapture::isOpened() const", referenced from:
processVideo(char*) in bs-df9f1a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I do not know how to solve this problem, it's not related to source code or include, it's related to the linker. I see the post:C++ linking error after upgrading to Mac OS X 10.9 / Xcode 5.0.1
Which really sounds reasonable, but I do not know how to add -stdlib=libstdc++ to the linking command. ?
Hope sb could help. Thanks in advance.

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