This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

dllimport question


Hi!  

I'm using g++ under windows to create some separately
loadable modules for GNU Octave.  As it stands, libstdc++
is linked statically and each module huge.  To reduce the
dll size, I've been trying to create a dll version of
libstdc++.

Using dlltool, this isn't too hard, though I do have to
use the following in my source: 
  #include <iostream>
  namespace std { 
	__declspec(dllimport) ostream cout; 
  } ;
The resulting program size (25787) is considerably smaller 
than that from linking directly to libstdc++.a (448537), 
even after stripping (210944).

I've included hello.cc and Makefile below.
Use "make hello" to test.  You may have to change the
STDCXX variable to point to the appropriate libstdc++.a

The problem comes when I use possibly different 
__declspec(dllimport) commands in separately compiled 
modules.  In this case I get the link error:
  world.o(.bss+0x0):world.cc: multiple definition of `D'
  hello.o(.bss+0x):hello.cc: first defined here
This error does not occur using __declspec(dllimport) in
C source.  nm shows the _D symbol marked with 'C' when
compiled with gcc, and 'B' when compiled with g++.

Use "make world" to generate the error.

I get the same error on two compilers
   mingw:  gcc version 3.2 (mingw special 20020817-1)
   cygwin: gcc version 3.2 20020927 (prerelease)
I even get the error when not doing any tricks with
libstdc++, but just have __declspec(dllimport) statements
in different .o files which I am linking together.

Any idea what I am doing wrong?

Is there a better way to avoid linking libstdc++ into
all separately loaded modules?

Thanks in advance,

Paul Kienzle
pkienzle@nist.gov

PS, please CC any responses to me as I am not subscribed to
the list.

--- Makefile ---

STDCXX=/usr/lib/libstdc++.a
#STDCXX=/mingw/lib/libstdc++.a
%.o: %.cc ; g++ -c $< -o $@ -DSTDCXX_DLL

hello: hello.o libstdc++.dll
	gcc -o $@ $< -Wl,--enable-auto-import -L. -lstdc++.dll

world: hello.o world.o libstdc++.dll
	gcc -o $@ hello.o world.o -Wl,--enable-auto-import -L. -lstdc++.dll

libstdc++.dll: ${STDCXX}
	dlltool --export-all --output-exp $@.exp --dllname $@ --output-lib $@.a $<
	gcc -shared $@.exp $< -o $@
	rm $@.exp

clean:
	rm -f *.o *.a *.dll *.exe

--- hello.cc ---

#include <iostream>

#if defined(STDCXX_DLL)
namespace std { __declspec(dllimport) ostream cout; } ;
#endif

int main(int argc, char *argv[])
{
   std::cout << "Hello, world!" << std::endl;
   return 0;
}

--- world.cc ---

#include <iostream>
#if defined(STDCXX_DLL)
namespace std { __declspec(dllimport) istream cin; } ;
#endif
int world(void)
{
  int x;
  std::cin >> x;
  return x;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]