I’m sitting in the Cooper Square Hotel in Manhattan (long story), working on a nice little article on fast, small, simple unit testing, and looking south at downtown. I can see the cables and towers of the Williamsburg, Manhattan and Brooklyn Bridges. Cooper Square is where Bowery meets East Village. It’s nice. Think “Mission Hipster” diluted with more blue collar types and NYU students.
Not much traffic noise up here on the 17th floor.
It’s a nice place to work…
So, I’m working on this small bit of test code for a future article. My first pass through the compiler resulted in this little gem:
$ gcc -o test test_linesegment.cpp unittest.cpp ../linesegment.c /cygdrive/c/Users/doolin/AppData/Local/Temp/ccHFVssO.o:test_linesegment.cpp:(.te xt+0xd): undefined reference to `std::basic_string, std::allocator >::size() const'
My first emotion was disappointment and irritation. I’m a smart guy, I’ve been coding a long time, I should NOT be getting these kinds of linker errors!*
But get them I did…
And if you haven’t been away from the command line for the last couple of years, you should be able to see the problem pretty fast. It’s really simple. As anyone can plainly see, I was using gcc (a C compiler) to build a C++ program. So it croaked on linking, as it should: C compilers should NOT be expected to link with C++ libraries.
Here’s one way to fix it:
$ gcc -o test test_linesegment.cpp unittest.cpp ../linesegment.c -lstdc++
A better way to handle it is use a C++ compiler. This works just fine, which is what I was expecting in the first place:
$ g++ -o test test_linesegment.cpp unittest.cpp ../linesegment.c
Such stupid little details are part of the “perishable” skill set. Stuff too trivial, too minor to remember, much less write down anywhere. The next time I have this error, I’m going to search here first. Although having written it down, I’ll probably never forget it now.
Sometimes, things aren’t as hard as we make them out to be.
More linker love
The “std::basic_string” link error isn’t too hard to find on Google. Here’s what I found and used to help write this article:
- http://www.codeguru.com/forum/archive/index.php/t-351875.html
- http://gcc.gnu.org/ml/gcc-help/2004-05/msg00028.html
- http://cboard.cprogramming.com/cplusplus-programming/82569-undefined-reference-`std-basic_string.html. This site links to CPP Reference, which I’m including here because I’ve never heard of it before, and it’s really cool!
That’s enough for now… time to head up Midtown for some fried chicken at the Blue Ribbon Bar & Sushi Grill.








