Getting Boost asio to work - c++

I am trying to use the Boost asio library to for sockets.
I installed boost using homebrew brew install boost
After it was built I tried the tutorial for creating a server on boost's website.
All I did was paste that code into a file called main.cc
When I try to compile g++ main.cc I get this error:
Undefined symbols for architecture x86_64:
"boost::system::system_category()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccTbzxpk.o
boost::asio::error::get_system_category() in ccTbzxpk.o
boost::system::error_code::error_code()in ccTbzxpk.o
"boost::system::generic_category()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccTbzxpk.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
What is going wrong?

Those are linker errors. If you read the docs:
The following libraries must be available in order to link programs
that use Boost.Asio:
Boost.System for the boost::system::error_code and
boost::system::system_error classes.
Boost.Regex (optional) if you use
any of the read_until() or async_read_until() overloads that take a
boost::regex parameter.
OpenSSL (optional) if you use Boost.Asio's SSL
support.
Furthermore, some of the examples also require the Boost.Thread,
Boost.Date_Time or Boost.Serialization libraries.
Now, the errors you posted all say: boost::system, this means you need to link like this (assuming everything is the default):
g++ main.cc -lboost_system
Please read What do 'statically linked' and 'dynamically linked' mean? for in-depth information about linking.

Related

linking Boost library to Xcode project

I'm trying to create a project that uses the Boost library. I'm on OS X 10.9.5 (I should update that) and using Xcode 6.2. I installed boost with homebrew brew install boost and it's located in /usr/local/Cellar/boost/1.59.0. I added the path the the /usr/local/Cellar/boost/1.59.0/include to the header search path in Xcode and it seems to recognize it because the autocomplete hinting works.
In the boost documentation it mentions that some of the Boost libraries must be built before they can be used. I assume homebrew took care of that because I have a bunch of .a and .dylib files in /usr/local/Cellar/boost/1.59.0/lib
I'm still new to C++ and the Xcode build process but it seems I still need to link the compiled libraries to my project. I tried adding the path /usr/local/Cellar/boost/1.59.0/lib to my project's library search paths but I'm not sure if that is correct.
Here is the error I get when I try to build my project.
Undefined symbols for architecture x86_64:
"boost::filesystem::detail::create_directory(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
boost::filesystem::create_directory(boost::filesystem::path const&) in main.o
"boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
boost::filesystem::exists(boost::filesystem::path const&) in main.o
"boost::system::system_category()", referenced from:
___cxx_global_var_init2 in main.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in main.o
___cxx_global_var_init1 in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Is there something else I need to configure to get this to work?
Adding the path is correct but you also need to specify the libraries you need. On the commandline you would use -l for that in Xcode you can add them to Other Linker Flags.
The libraries you need are boost_filesystem and boost_system.

Issue compiling Go program when linking to a compiled version of zeromq on Mac

I've compiled ZeroMQ on Mac OSX 10.9.5 in order to then link to a simple Go program using github.com/pebbe/zmq4, but I'm getting this error I don't understand or know exactly how to remedy.
# github.com/pebbe/zmq4
ld: warning: ignoring file /usr/local/lib/libzmq.dylib, file was built for x86_64 which is not the architecture being linked (i386): /usr/local/lib/libzmq.dylib
Undefined symbols for architecture i386:
"_zmq_bind", referenced from:
__cgo_59814aec404b_C2func_zmq_bind in zmq4.cgo2.o
__cgo_59814aec404b_Cfunc_zmq_bind in zmq4.cgo2.o
(maybe you meant: __cgo_59814aec404b_Cfunc_zmq_bind, __cgo_59814aec404b_C2func_zmq_bind )
"_zmq_close", referenced from:
__cgo_59814aec404b_C2func_zmq_close in zmq4.cgo2.o
__cgo_59814aec404b_Cfunc_zmq_close in zmq4.cgo2.o
(maybe you meant: __cgo_59814aec404b_C2func_zmq_close,
...
"_zmq_z85_encode", referenced from:
__cgo_59814aec404b_Cfunc_zmq_z85_encode in zmq4.cgo2.o
(maybe you meant: __cgo_59814aec404b_Cfunc_zmq_z85_encode)
ld: symbol(s) not found for architecture i386
collect2: error: ld returned 1 exit status
I think it makes sense that the dylib is made for x86_64 (the Mac) but why Go is trying to link with i386? How do I control that and make it link differently? or even if it's correct?
If you build libzmq yourself, configure with --disable-shared --enable-static keys. If you use homebrew, uninstall zmq and build the library manually. If you need "fat" universal static library use this script:
https://github.com/drewcrawford/libzmq-ios

OSX Disk Arbitration linker errors

Trying to compile my C++ application using clang which works but I get the following linker error:
Undefined symbols for architecture x86_64:
"_DADiskCopyDescription", referenced from:
Security::getHddID() in Security.cpp.o
"_DADiskCreateFromBSDName", referenced from:
Security::getHddID() in Security.cpp.o
"_DASessionCreate", referenced from:
Security::getHddID() in Security.cpp.o
ld: symbol(s) not found for architecture x86_64
I've checked the official documentation for Disk Arbitration on the Apple website but that's awful, since it doesn't even tell you which library file to include, let alone what to link to.
I'm using CMake to set up my build chain.
You need to use -framework DiskArbitration in your linker arguments.

Boost linker errors when using header-only libraries

I am using the following components of Boost 1.53.0 in conjunction with C++11 libraries...
boost::signals2::scoped_connection
boost::signals2::signal
boost::signals2::connection
boost::math::constants
boost::circular_buffer
boost::lexical_cast
According to this answer, I do not need to link against libraries to use these parts of Boost. According to this answer, signals2 should be header-only also. However, I still receive linker errors...
Undefined symbols for architecture i386:
"boost::system::system_category()", referenced from:
___cxx_global_var_init2 in Main.o
...
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in Main.o
___cxx_global_var_init1 in Main.o
...
Why?
Boost.Signals2 is indeed header-only, but Boost.System is not. You have to make sure that you don't have any dependency on that library. If it's in your own code, you have to build Boost.System and link against it. If it's called from any header-only Boost library, file a bug report.
The problem was a spurious
#include <boost/thread/mutex.hpp>
accidentally left in the middle of a file.

Trying to compile C++ code on mac

I'm trying to compile some C++ code on my mac. I can run it using Xcode, but when I try to compile from the terminal using gcc I get the following error:
Undefined symbols for architecture x86_64:
"std::cout", referenced from:
_main in ccOJDOlb.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 ccOJDOlb.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccOJDOlb.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccOJDOlb.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Anyone know what might be going on?
ld: symbol(s) not found for architecture x86_64
This is saying that 'ld', the linker, is not able to find definitions for some symbols your program uses. So you know compilation has succeeded.
And ld lists the specific symbols. Notice that they are all from the standard library. This should tell you that the standard library is probably not being found by the linker.
Typically when you use a library you have to tell the compiler toolchain using a link flag. But for a language's standard library the compilers in the GNU compiler collection generally just assume you want the library for that language. So running the gcc command automatically includes the C standard libraries, running g++ automatically includes the C++ standard libraries, etc. But notice, running 'gcc' does not automatically link in the C++ standard library. It's very likely that you're running the command 'gcc' and simply not adding the correct linker flag for the C++ standard library.
If for some reason you want to use gcc and not g++ you'll have to explicitly state that you want the standard library, using a flag like -lstdc++.
Also, unless you really want gcc and you're installing the latest versions of it yourself on OS X you may want to switch over to clang/clang++ and the libc++ implementation of the C++ standard library. The gcc that comes with Xcode is based on an old version, and the standard library is similarly old. Xcode has been transitioning to clang as the system compiler for some time now. The clang compiler driver has basic compatibility with gcc so you use many of the same basic flags. Here's a good set to start with:
clang++ -std=c++11 -stdlib=libc++ -Wall -Wextra
I try to compile from the terminal using gcc
Then try g++ instead of plain ol' gcc. The former automatically links against the C++ standard library, the latter does not.