ld and C++

ld and C++

Post by f.. » Thu, 22 Jun 2000 04:00:00



I am encountering a strange problem (at least for me) with compiling a
rather simple program in a single step or in multiple steps into an
executable.

I am using Solaris Sparc 2.7 and WorkShop 5.

Two scenarios's:

1) I use CC to directly build the source code from Appendix A into an
executable that works.

2) I build step by step a) compiling into an object b) making a lib
out of it and c) finally the executable itself but know I have a bunch
of unresolved symbols (see Appendix B).

Anybody ideas ?

Appendix A:
========== file: foo.cpp
#include <map>
#include <string>
#include <stdio.h>

using namespace std;

typedef map<string, string> STRINGMAP;

int main()
{
#ifdef USE_MAP
  STRINGMAP mymap;
  STRINGMAP::iterator iter;

  iter = mymap.begin();
#endif

  string mystring = "Hello World";

  string str2 = "Goodbye";

#ifdef USE_MAP
  mymap[mystring] = str2;
  iter = mymap.find(mystring);

 printf("Mystring is %s, stored string is %s\n", mystring.c_str(),
(*iter).second.c_str() );

#else

  printf("Mystring is %s, str2 is %s\n", mystring.c_str(),
str2.c_str() );
#endif

return 0;

Appendix B:
==========

CC -c foo.cpp -o foo.o -DUSE_MAP
ld -r -o foo.lib foo.o
CC foo.lib -o foo1
Undefined   first referenced
 symbol         in file
void
__rwstd::__rb_tree<std::basic_string<char,std::char_traits<char>,std::allocator<char>

Quote:>,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char>
>,std::basic_string<char,std::char_traits<char>,std::allocator<char> >
>,__rwstd::__select1st<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char>
>,std::basic_string<char,std::char_traits<char>,std::allocator<char> >
>,std::basic_string<char,std::char_traits<char>,std::allocator<char> >
>,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char>

...

ld: fatal: Symbol referencing errors. No output written to foo1
*** Error code 1
make: Fatal error: Command failed for target `foo1'

Compilation exited abnormally with code 1 at Tue Jun 20 21:14:}

 
 
 

ld and C++

Post by Phil Edwar » Thu, 22 Jun 2000 04:00:00


[cc'd to poster]

+ CC -c foo.cpp -o foo.o -DUSE_MAP
+ ld -r -o foo.lib foo.o
+ CC foo.lib -o foo1
+ Undefined   first referenced
+  symbol         in file
[missing C++ things]

Common mistake.  You're not telling the linker to link in any other
libraries, such as the one containing STL symbols.

Linkers are generally unaware of language issues, like which language was
used to program an object file (and hence which language-support libraries
should "obviously" be linked in by default).  That's mostly okay because
that's not their job.

This is why you should almost always invoke the compiler rather than the
linker, especially when creating libraries and executables.  The compiler
"knows" about language issues and will add the necessary options to the
command line when it in turn invokes the linker.

(A lot of documentation has you call ld directly, e.g., when creating a
shared library.  That works with C, where the linkage is fast, loose, and
wacky, but with very few other languages.  Most good manuals point this
out these days, and tell you to call the C++ (or whatever) compiler with a
"make a shared lib" option.)

Phil

 
 
 

ld and C++

Post by Thomas Deh » Thu, 22 Jun 2000 04:00:00



> [cc'd to poster]


> + CC -c foo.cpp -o foo.o -DUSE_MAP
> + ld -r -o foo.lib foo.o
> + CC foo.lib -o foo1
> + Undefined   first referenced
> +  symbol         in file
> [missing C++ things]

> Common mistake.  You're not telling the linker to link in any other
> libraries, such as the one containing STL symbols.

He should use CC -xia
to link the library, not ld, which does not know anything
about templates (templates come in via the standard header files).

Thomas

 
 
 

ld and C++

Post by Joe Higgin » Thu, 22 Jun 2000 04:00:00


Quote:> CC -c foo.cpp -o foo.o -DUSE_MAP
> ld -r -o foo.lib foo.o
> CC foo.lib -o foo1

Try:

CC -o foo.o -c foo.cc -DUSE_MAP
CC -xar -o foo.a foo.o
CC -o foo foo.a

The -xar makes sure that all the template garbage is added to
the .a.

Or try something dynamic like:

CC -o foo.o -c -KPIC foo.cc -DUSE_MAP
CC -G -o foo.so foo.o
CC -o foo foo.so

(The latter needs foo.so to hang around in order to execute
foo.)

Look at the C++ User's Guide in the Sun WorkShop Compiler C++
5.0 Collection, at the section headed: Building Libraries.

Regards, Joe

 
 
 

ld and C++

Post by Thomas Deh » Fri, 23 Jun 2000 04:00:00




> > [cc'd to poster]


> > + CC -c foo.cpp -o foo.o -DUSE_MAP
> > + ld -r -o foo.lib foo.o
> > + CC foo.lib -o foo1
> > + Undefined   first referenced
> > +  symbol         in file
> > [missing C++ things]

> > Common mistake.  You're not telling the linker to link in any other
> > libraries, such as the one containing STL symbols.

> He should use CC -xia
> to link the library, not ld, which does not know anything
> about templates (templates come in via the standard header files).

typo, should be CC -xar

Thomas