This is the mail archive of the cygwin@sources.redhat.com mailing list for the Cygwin project.


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

Re: AW: Linking Dynamic Libraries


"Charles S. Wilson" wrote:
> 
> There's an easier way -- one command!  'gcc -shared' will create a dll
> for you.  Just do:
> 
> $(BASE)=foo
> 
>   gcc -shared -o cyg$(BASE).dll -Wl,--out-implib=lib$(BASE).dll.a \
>     -Wl,--export-all -Wl,--enable-auto-image-base \
>     -Wl,--output-def=cyg$(BASE).def $(OBJS)

I can't get this to work. Whenever I link with the import lib produced
this way, the resulting program crashes with a STATUS_ACCESS_VIOLATION.
(See attached code.) I don't even need to call any of the DLL functions
to trigger it -- just linking with -lfoo makes it unworkable. What am I
doing wrong?

And where is all this stuff documented anyway? The ld docs mention
--export-all-symbols (not --export-all), but not any of the others.

-------------------- cut here --------------------

// demo.cpp

#include "foo.hpp"
#include <iostream>
#include <string>

int main() {
  std::string thing("thing");
  std::cout << thing << std::endl;
//  std::cout << global_mangle(thing) << std::endl;
  return 0;
}

-------------------- cut here --------------------

// foo.hpp

#ifndef FOO_HEADER
#define FOO_HEADER

#include <string>

extern std::string global_mangle(const std::string& source);

#endif

-------------------- cut here --------------------

// foo.cpp

#include "foo.hpp"

std::string global_mangle(const std::string& source) {
  return source + "_global";
}

-------------------- cut here --------------------

// Makefile

DLLNAME = foo
DLLOBJ = foo.o
EXENAME = demo
EXEOBJ = demo.o
CPLUS = g++ -W -Wall -Werror
LINKDLL = g++ -shared -Wl,--export-all -Wl,--enable-auto-image-base
LINKEXE = g++ -L.

.DELETE_ON_ERROR:

.PHONY: all clean

all: $(DLLNAME).dll $(EXENAME)

clean:
    rm -f $(DLLOBJ) $(DLLNAME).dll lib$(DLLNAME).a $(EXEOBJ)
$(EXENAME).exe

%.o: %.cpp
    $(CPLUS) -c $< -o $@

$(DLLNAME).dll: $(DLLOBJ)
    $(LINKDLL) $(DLLOBJ) -o $(DLLNAME).dll
-Wl,--out-implib=lib$(DLLNAME).a

$(EXENAME): $(EXEOBJ) $(DLLNAME).dll
    $(LINKEXE) $(EXEOBJ) -l$(DLLNAME) -o $(EXENAME)

demo.o: demo.cpp foo.hpp
foo.o: foo.cpp foo.hpp

-------------------- cut here --------------------

-- 
Ross Smith <ross.s@ihug.co.nz> The Internet Group, Auckland, New Zealand
========================================================================
"C++ is to programming as sex is to reproduction. Better ways might
technically exist but they're not nearly as much fun." -- Nikolai Irgens

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


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