Using STL

J

jlea

Is there a library that I need to include to use STL with a MC++ project?
Thanks. Jon.
 
M

Mark Mullin

Short anwser - yes

Longer answer - depends on which STL constructs you are using

Here are some examples

#include <vector>
#include <stack>
#include <map>
#include <string>
#include <algorithm>
#include <set>
#include <string>
 
J

jlea

I tried that, it compiles but at link, it can't find a bunch of stuff. It's
like I need to include a .lib file in the link or something in stdafx.h.
Jon.
 
M

Mark Mullin

I can't comment on the LeapSoft reference, but I can make some general
comments and offer a workable strategy.

1) STL is a very heavy user of very complicated templates. When it
was first distributed it had a reputation for busting compilers right
and left, even though it was completely legal C++ code.

2) Use of templates requires that the compiler instantiate
specializations of them based on how you are using them. Effectively,
using list<MyObject> requires an internal class be constructed that
represents the template code where the template variables have been
replaced with MyObject.

3) In the early days of templates, use of them could lead to massive
'code bloat', i.e. the template was instantiated and reinstantiated
multiple times as it was encountered/used. You could get a template
version of "Hello World" weighing in at umpteen megs. Further work by
both standards committees and compiler vendors has attempted to
address this, but at its heart, the problem is just plain nasty.

4) MSFT does many strange and wonderful things with new and delete
operators and mem<x> operations in their headers and libraries. I'm
not pointing fingers, they have many more constituencies to serve than
just myself, but I've been bitten by this often.

5) Precompiled headers are an attempt to ensure that developers do not
visibly age while waiting for the compiler to convert their source to
object. Precompiled headers, templates, and template optimization can
often step on each others feet.

So heres the solution we use.

First, don't let visual studio decide how it's going to do precompiled
headers. Its not that bright.

Create a .cpp file that will specifically be the one to create the
precompiled headers. It should read in all the windows cruft, any
files you use to configure the compiler (usually pragmas), and the
relevant stl libraries. Here's a slice from the file used to generate
precompiled headers for our Node library. Note it doesn't have
windows includes, you probably will have em, after compiler config and
any real gut level stuff like new.h

#include "compilerconfig.h" (this is our file used to configure the
compiler pragmas and fpu)
#include <new.h> (you may not need this)
#include <math.h> (we're math geeks)
#include <float.h> (really major math geeks)
#include <iostream> (probably)
#include <fstream> (probably)
#include "template.h" (heres where you bring in stl)

The template.h file should include the headers you want, ours (for
node) looks like
#pragma once
#include <vector>
#include <stack>
#include <map>
#include <string>
#include <algorithm>
#include <set>
#include <string>
/*! \ingroup SYSCORE */
class less_string_Ptr {
public:
bool operator()(const std::string* a,const std::string* b) const
{
return *a < *b;
}
bool operator()(std::string* a,std::string* b) const
{
return *a < *b;
}
};


Now, pop open the properties of your precompiled.cpp, and set it to
'create precompiled headers' under the precompiled headers tab. For
all other .cpp files, set the option to 'use precompiled headers'.
Set the .h file in both to be the last .h file in precompiled.cpp that
you want to be in this.

If you haven't mucked about with precompiled headers before, crack
open the doco and read up a bit on em. It's important that all files
using precompiled headers have a common .h file sequence that serves
to match everything up, the doco will expain this.

When that's all done, you should be ready to rock.

MMM
 
J

jlea

Thanks for the detail response and I'll give your suggestions a try. The
reason why I was confused is that with v6.0 of Visual Studio, we just
included <list> in a header file of our choice and presto, we were able to
declare member variables of type list within that header file. It appears it
isn't that simple in .NET? Jon.
 
J

jlea

I'll investigate the precompiled header scenario and let you know what I
find out. Thanks. Jon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top