Archive for the 'programming' Category

C++ Committee Needs You

Friday, September 12th, 2008

JTC1/SC22/WG21 - The C++ Standards CommitteeToday, Bronek Kozicki - a member of BSI C++ panel - shared some bad news on ACCU mailing list, about sponsorship problems of WG21 meeting in March 2009:

I just learned that original sponsor of this meeting backed out, leaving guys from EDG not only with organizational tasks, but also financial burden.

So, Bronek has also put out call for help to all interested parties:

Therefore, it would really nice if those of ACCU members who are working in large companies (and which also happen to utilize C++ to great extent) could pass a message up the management chain that sponsoring ISO/IEC committee meeting is unusual and very prestigious PR opportunity. Sponsors of past meetings include Intel, Microsoft and Google, among other IT giants; unfortunately EDG is only giant in intelectual sense.

If your managers find this opportunity compelling, please contact them with J. Stephen Adamczyk (jsa (at) edg (dot) com).

C++ Concurrency in Action

Friday, August 22nd, 2008

By the beginning of the year 2009, new must-read book for C++ hackers is rolling around. Anthony Williams is writing book titled: C++ Concurrency in Action (ISBN: 1933988770):

I will be covering all aspects of multithreaded programming with the new C++0x standard, from the details of the new C++0x memory model and atomic operations to managing threads and designing parallel algorithms and thread-safe containers. The book will also feature a complete reference to the C++0x Standard Thread Library.

from Anthony’s blog

Since June, Anthony’s book is available through Manning Early Access Program. The final release is planned on February 2009.

In the meantime, Anthony has published an article Simpler Multithreading in C++0x introducing multithreading support and thread library as a new feature in the C++0x standard.

WKB hex decoder in C++

Thursday, August 21st, 2008

In PostGIS world, I often need to construct geometry from Well-Known-Binary (WKB) or PostGIS EWKB stream encoded as hex stream. It’s easy to do if I have access to PostgreSQL/PostGIS client which accepts SQL queries:

SELECT
   ST_AsText(
      ST_GeomFromWKB(
         decode('0101000000e5d022dbf93e2e40dbf97e6abc743540', 'hex'),
         4326))

I often need to do the same directly in C++ code - parse hex encoded binary stream to raw stream of bytes. Here is simple hex decoder I use:

#include <sstream>
#include <string>
#include <vector>
typedef std::vector<unsigned char> ewkb_t;

void hex_to_bytes(std::string const& hexstr, ewkb_t bytes)
{
    bytes.clear();
    for(std::string::size_type i = 0; i < hexstr.size() / 2; ++i)
    {
        std::istringstream iss(hexstr.substr(i * 2, 2));
        unsigned int n;
        iss >> std::hex >> n;
        bytes.push_back(static_cast<unsigned char>(n));
    }
}

For example, I use it to build FDO geometry objects using another utility CreateGeometryFromExtendedWkb defined in FDO provider for PostGIS:

// POINT (1.234 5.678)
std::string ewkbhex("01010000005839B4C876BEF33F83C0CAA145B61640");
ewkb_t ewkb;
hex_to_bytes(ewkbhex, ewkb);
if (!ewkb.empty())
{
   FdoPtr<fdoigeometry> g = CreateGeometryFromExtendedWkb(ewkb);
   // ... use geometry
}

Boost 1.36.0

Wednesday, August 20th, 2008

After 5 months since last release of Boost 1.35, new version has been announced. It brings four new libraries: accumulators, exception, unit, unordered and a number of fixes and improvements to existing libraries.

Find more details on the page Version 1.36.0

libgeotiff lesson for today

Thursday, July 31st, 2008

Doing final tests of libLAS on Linux (Ubuntu 7.04), before Hobu will release 1.0.0, I attempted to build it with latest libgeotiff 1.2.5. I moved my fingers on keyboard and did ./configure && make, shortly then I saw strange looking error message:

/usr/bin/ld: makegeo: hidden symbol `__stack_chk_fail_local' in /usr/lib/libc_nonshared.a(stack_chk_fail_local.oS) is referenced by DSO
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: ld returned 1 exit status

Google is your friend, so in a few seconds I knew what’s going wrong. ./configure sets Makefile to call ld -shared. However, it does not work with GCC 4, because ld linker does not include all required references to other shared libraries. Simply, GCC linker driver seems to handle shared references better, than bare ld.

The conclusion is, that if you use GCC 4.x and you want to build libgeotiff, configure it this way:

./configure --with-ld-shared="gcc -shared"

Update July 31, 2008: Another solution is to add -fno-stack-protector option to CFLAGS and CXXFLAGS. Regarding Ubuntu Linux, this issue seems to be related to enabled SSP support (see also Ubuntu Wiki) available in GCC 4.1.

Juggling SVN keywords

Saturday, July 26th, 2008

When working in a team and maintaining large source code repositories, it’s easy to forget to set svn:keywords property for newly added file or update the property when adding new keywords to files.

The first solution that may come to once mind is to involve cron job or define post-commit hook. The former is easier to setup but may be unsafe if commits are allowed by authenticated users. The second option seems appealing as we can avoid authentication problems, but it’s generally not recommended to set properties from a hook. See warning in red frame in the Hook Scripts chapter of the SVN book.

After short discussion with folks on #gdal IRC channel, we have came to the conclusion that the easiest and safest option will be to periodically run a script fixing the svn:keywords where needed. Such script will be executed by team members on their local machines and against working copy of GDAL sources. This way we assure that the commit process is well authenticated and under human control :-)

So, here is the script - svnkeywords.sh (backed up here):

$ ./svnkeywords $HOME/dev/gdal/_svn/trunk
Entering '/home/mloskot/dev/gdal/_svn/trunk'
Setting svn:keywords property........done.

It is quite generic and can run against any sources tree maintained by Subversion. The only requirement is Unix OS with toolset like /bin/sh, find and grep. Happy using!

Yet another argument for -Wall always on

Thursday, July 24th, 2008

Yes, I’m religious about writing correct and standard C/C++ code and the decalogue of my religion tells do not ignore compiler warnings. Yet another pinch to the vast jar of examples is about getting detailed warning on mixed types in printf format specifiers:

#include <stdio .h>
int main (void)
{
printf ("%d", 0.0);
printf ("%lu", 0u);
printf ("%lu", sizeof 0);
return 0;
}

Building the test above with following three commands will answer why it’s a good idea to help the compiler to help us: