Tuesday, December 11, 2007

installing boost::asio

This boost C++ library is an interesting and very useful for dealing with low level sockets. As its portable, you don't have to deal with winsock or socket.h! Programming with it is fairly easy as far as I've gotten so far, and the library supports both synchronous and asynchronous operations on sockets. As of version 1.34 of boost, asio is not included in a standard install of boost, so to install some work (and trouble) is needed.

My experience with installing asio was rather difficult. Instructions are given, but they are long winded and don't contain all the steps and background needed to fully understand what is going on, so I'll attempt to explain., starting with the primary differences between the available download options.

There are two different options for downloading, one requires a distribution of boost, versions 1.33 or 1.34, and the other does not require this. There is not much difference between the two, other then a few functions won't work in the release that does not require boost. As I installed both, I'll go through both below starting with the install that does not require boost as its easier to install.

asio without boost install - Download one of the compressed files that start with asio followed by a number. I downloaded asio-0.3.9.tar.gz. Once you have the download, uncompress it into a new directory. That is it! When using it remember to #include "path/to/asio.hpp"

asio with boost install - Download one of the compressed files that start with boost_asio followed by a number. I downloaded boost_asio_0_3_9.tar.gz. Once you have the download uncompress it into a new directory. If you do not have a boost source install, you will need to download it. Pick the top option, labeled "boost". Uncompress it to a new directory and enter that directory (cd into it). You will need to move the files in the asio download into the boost source files. Do so by copying all files in boost/ and src/ to the respective files in the boost directory. Then type in ./configure (add options as you wish), make, and finally make install if you choose to install this source build to your operating system. (If there is a better way, please comment below, however the instructions on the use of bjam and boost.build were a tad on the confusing side.) When making use of this, make sure to link to the boost::system libraries.

I have heard that asio will come default with boost 1.35, however that was just in a chat on IRC in #boost and can be wildly wrong :).

I'll comment more when I explore the usage of this library and get a chance to compare it to using socket.h. If anyone reading this has better install instructions for asio with boost, I would like to hear about it, as is the instructions involve remaking all boost libs to get the needed boost::system dependency.

Monday, December 10, 2007

Convert C++ string to char array

I've seen this question show up several times in ##C++ on Freenode, so I'll attempt to put a decent answer up here with a few links. Maybe it will help, maybe not. If it saves some poor sap an hour like it took me to figure out a year ago, its worth it :)

Its easy and intuitive to make a char array a string, but not the reverse.

#include <iostream>

int main() {
    char chars[] = "Some Chars";
    std::string s = chars; // could also just be "Some Chars";
    std::cout << s;
    return 0;
}
//Outputs: Some Chars


The above code sample looks nice and easy due to some operator overloading. If you don't know what operator overloading is and would like to learn more, please see the link to wikibooks, it has some decent coverage on the topic.

There is no similar overload for the char (or char array) types as these are not C++ objects. Because of this we have to look up the member function in the string object that returns a char array. The function in this case is c_str();. Follows is an example of its use.

int main() {
    char chars[] = "Some Chars";
    std::string s = chars; // could also just be "Some Chars";
    std::cout << s << " from string object" << std::endl;
    char returned_chars[10] = s.c_str();
    std::cout << returned_chars << " from char array" << std::end;
    return 0;
}
//Outputs:
//Some Chars from string object
//Some Chars from char array


Its really easy once you find what function to use. For more information about strings in general, have a look at this useful guide on the string object.

Also if anyone was curious, the std::cout object overrides the << symbol, which allows it to output text to your terminal.

Blog details

I created this blog as somewhere decent to type out what I've seen and done as far as programming in C++ and or using linux, perhaps both at once! I should clarify that I'm an university student, working on a comp-sci major. I'll be taking my first real programming course next quarter, until then I'm satisfied with playing with programming and computers. Some languages I have some knowledge of include C++, perl, and php.