Linker complains about unresolved symbol in STL mapp class

V

Vijay Bajwa

I declared a static std::map<string, int> in one.cpp at the module
level. This is only used inside the file and I did it to avoid global
namespace pollution

The file one.o goes into the library archive mylib.a

Then when I try to compile file two.cpp, which links to mylib.a, the
linker spits out incomprehensible messages about undefined symbols. I
understood them enough to know that it's complaining about methods of
the map class being undefined.

Any relief here? I saw some esoteric discussions on C++ FAQ Lite, but
missed the point.

Thanks a lot for your kind responses!
Vijay
 
A

Alan Johnson

Vijay said:
I declared a static std::map<string, int> in one.cpp at the module
level. This is only used inside the file and I did it to avoid global
namespace pollution

The file one.o goes into the library archive mylib.a

Then when I try to compile file two.cpp, which links to mylib.a, the
linker spits out incomprehensible messages about undefined symbols. I
understood them enough to know that it's complaining about methods of
the map class being undefined.

Any relief here? I saw some esoteric discussions on C++ FAQ Lite, but
missed the point.

Thanks a lot for your kind responses!
Vijay

Not enough information. Show some code that demonstrates the problem
you are having. Show us the error message you are getting. I just
tried to reproduce your problem and got no errors. Here was my test
setup.

one.cpp:
#include <map>
#include <string>

static std::map<int, std::string> my_map ;

void init()
{
my_map[1] = "Hello" ;
my_map[2] = " world" ;
}

std::string f(unsigned i)
{
return my_map ;
}

two.cpp:
#include <iostream>
#include <string>

void init() ;
std::string f(unsigned i) ;

int main()
{
init() ;
std::cout << f(1) << f(2) << std::endl ;
}

Build with:
$ g++ -W -Wall -ansi -pedantic -c one.cpp two.cpp
$ ar sr libone.a one.o
ar: creating libone.a
$ g++ -otest two.o -L. -lone
$ ./test
Hello world
 
V

Vijay Bajwa

you are having. Show us the error message you are getting. I just
tried to reproduce your problem and got no errors. Here was my test
setup.
.... snip...

-------------------------------- GTSSAXHandlers.cpp
-----------------------------
// string has a default compare functor <less> defined to order strings
static std::map <string, int> oaeb_field_map;

// fieldmap for OASEquityALloc block
static std::map <string, int> oaea_field_map;
....
....
void GTSEqTrdHandler::parse_eqtyBlock (const char* name, string& data)
{
static const char* func = "parse_eqtyBlock:" ;
static char* errMsg ;


const char* chdata = data.c_str();

if (!chdata) {
ERROUT << func << "no data for tag: " << name << ENDL
return ;
}

VDBGOUT << func << "tag=" << name << ", data=" << data << ENDL
.....
----------------------------- end GTSSAXhandlers.cpp
-------------------------------------

compile and add to library archive:
sun4-cross[GTS-Oasys]: make install
/xenv/Forte/sun4/7.0/5.8p4/prod/bin/CC -D_REENTRANT -g -Dsvr4 -DSVR4
-DSUN -DCPLUSREL=7.0 -DOSREV=5.8p4 -DY2K -DCOMPILE_STYLE=CPP_COMPILE
-DTYPE_TOKEN_1=\"C\" -I../include -I/home/vb11258/OAD5.0/src/include
-I/xenv/OasysDirect/sun4/5.8p4/3.0/src/include
-I/xenv/xerces-c++/sun4/5.8p4/2.2.0/include -c GTSSAXHandlers.cpp
/xenv/OS/sun4/5.8p4/sun4u/ccs/bin/ar rv libencdec.a GTSSAXHandlers.o
r - GTSSAXHandlers.o
ar: writing libencdec.a
/xenv/OS/sun4/5.8p4/sun4u/ccs/bin/ranlib libencdec.a
/usr/ucb/install libencdec.a ../../install


Then go to the directory that has a program that links to this archive.
Note that the program does not even call call the above piece of code
that has the std::map. So here goes:
vb11258@eqzps22d:/home/vb11258/work/GTS-Oasys/src/driver
sun4-cross[GTS-Oasys]: make
/xenv/Forte/sun4/7.0/5.8p4/prod/bin/CC -D_REENTRANT -g -Dsvr4 -DSVR4
-DSUN -DCPLUSREL=7.0 -DOSREV=5.8p4 -DY2K -DCOMPILE_STYLE=CPP_COMPILE
-DTYPE_TOKEN_1=\"C\" -I../include -I/home/vb11258/OAD5.0/src/include
-I/xenv/OasysDirect/sun4/5.8p4/3.0/src/include
-I/xenv/xerces-c++/sun4/5.8p4/2.2.0/include -c DecoderMain.cpp
/xenv/Forte/sun4/7.0/5.8p4/prod/bin/CC -D_REENTRANT -g -Dsvr4 -DSVR4
-DSUN -DCPLUSREL=7.0 -DOSREV=5.8p4 -DY2K -DCOMPILE_STYLE=CPP_COMPILE
-DTYPE_TOKEN_1=\"C\" -I../include -I/home/vb11258/OAD5.0/src/include
-I/xenv/OasysDirect/sun4/5.8p4/3.0/src/include
-I/xenv/xerces-c++/sun4/5.8p4/2.2.0/include -g -Bdynamic -zignore
-Qoption ld -t -mt -Bdynamic -lposix4 -o DecoderMain DecoderMain.o
.../../install/liboaphapp.a ../../install/libencdec.a
.../../install/libOAPHUtils.a -Bdynamic -L
/xenv/xerces-c++/sun4/5.8p4/2.2.0/lib -lxerces-c
-L/xenv/OasysDirect/sun4/5.8p4/3.0/lib -loasdirect -L
/xenv/mqi/sun4/5.x/5.3.0.5/lib -Bdynamic -lmqic -lmqmcs -lmqmzse
-limqb23as -limqc23as -lpthread
Undefined first referenced
symbol in file
__rwstd::__rb_tree said:
,std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>,__rwstd::__select1st<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int> > >::iterator __rwstd::__rb_tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>,__rwstd::__select1st<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int> > >::erase(__rwstd::__rb_tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>,__rwstd::__select1st<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int> > >::iterator,__rwstd::__rb_tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>,__rwstd::__select1st<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int> > >::iterator) ../../install/libencdec.a(GTSSAXHandlers.o)
std::pair said:
,std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>,__rwstd::__select1st<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int> > >::iterator,bool>__rwstd::__rb_tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>,__rwstd::__select1st<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int> > >::insert(const std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>&) ../../install/libencdec.a(GTSSAXHandlers.o)
void
__rwstd::__rb_tree said:
,std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>,__rwstd::__select1st<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,int> > >::__deallocate_buffers() ../../install/libencdec.a(GTSSAXHandlers.o)
ld: fatal: Symbol referencing errors. No output written to DecoderMain
*** Error code 1
make: Fatal error: Command failed for target `DecoderMain'

Funny thing is, when DecoderMain was in the directory containing the
std::map code, and I was directly linking DecoderMain.o to
GTSSAXhandlers.o all was well. Go figure!

Alan, thanks for taking the time to try this out!

Regards,
Vijay
 
V

Vijay Bajwa

By putting the following 2 lines in two.cpp main() the problem was
resolved:
std::map<string, int> mymap;
mymap["one"] = 1;

The following line in two.cpp main also resolved the problem:

XMLDecoder myDecoder;

The XMLDecoder builds the static map<string,int> declared in one.cpp,
ie calls the insertion method. I guess declaring a static object at
file scope (thus causing constructors to run before main) wasn't enough
for the compiler to generate code for the class?

Also, what is mystifying is that XMLDecoder objet is instantiated in
one of the thread functions, just not in main. I'm chalking it upto the
Forte compiler and/or Rougue Wave STL libraries that compiler uses (I
think. I see a lpt of rw stuff in all the C++ header files).

Regards,
Vijay
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top