mkmf, linking against a specific DLL on windows

D

Daniel Berger

Hi all,

Ruby 1.8.2
Windows XP
Free MS VC compiler

How do I link against a specific DLL on Windows again? I tried have_library,
but that always looks for the .lib file apparently.

I also tried $LDFLAGS += " -lfoo.dll", as well as "-link foo.dll" and "-l foo"
and "-link foo", but none of that seemed to work.

What's the correct way?

Thanks.

Dan
 
N

nobuyoshi nakada

Hi,

At Tue, 18 Oct 2005 02:16:42 +0900,
Daniel Berger wrote in [ruby-talk:160905]:
How do I link against a specific DLL on Windows again? I tried have_library,
but that always looks for the .lib file apparently.

What do you want to link against? AFAIK, VC link.exe can't
handle DLL files as input, without import library files (i.e.,
lib files).
 
N

nobuyoshi nakada

Hi,

At Wed, 26 Oct 2005 07:42:03 +0900,
Daniel Berger wrote in [ruby-talk:162622]:
Ok, but you can generate a .lib file from a .def, and you can generate
a .def from a .dll. Here's a script I cobbled together that did the
trick:

Of course you can, but I don't think it should be done
silently by mkmf.rb. It belongs to setting up of a particular
library, not building an extension for it.
# Create a .def file from the results of a 'link -dump -exports'

Actually, it calls dumpbin.exe.
# Create a .lib file from a .def file. Note that the lib command doesn't
# seem to like long path names, so we'll shorten them.

Really?

$ lib -machine:x86 -def:../stable-build/i686-mswin32/msvcrt-ruby18.def -out:../stable-build/i686-mswin32/msvcrt-ruby18.lib
Microsoft (R) Library Manager Version 7.10.2240.8
Copyright (C) Microsoft Corporation. All rights reserved.

Creating library ../stable-build/i686-mswin32/msvcrt-ruby18.lib and object ../stable-build/i686-mswin32/msvcrt-ruby18.exp

It's possible to tweak win32/mkexports.rb to accept DLL files
as well as object files.


Index: win32/mkexports.rb
===================================================================
RCS file: /cvs/ruby/src/ruby/win32/mkexports.rb,v
retrieving revision 1.4
diff -U2 -p -u -r1.4 mkexports.rb
--- win32/mkexports.rb 18 Jun 2002 10:23:31 -0000 1.4
+++ win32/mkexports.rb 26 Oct 2005 02:19:04 -0000
@@ -4,13 +4,21 @@ SYM = {}

objs = ARGV.collect {|s| s.tr('/', '\\')}
-IO.foreach("|dumpbin -symbols " + objs.join(' ')) do |l|
- next if /^[0-9A-F]+ 0+ UNDEF / =~ l
- next unless l.sub!(/.*\sExternal\s+\|\s+/, '')
- if l.sub!(/^_/, '')
- next if /@.*@/ =~ l || /@[0-9a-f]{16}$/ =~ l
- elsif !l.sub!(/^(\S+) \([^@?\`\']*\)$/, '\1')
- next
+filetype = nil
+IO.foreach("|dumpbin -symbols -exports " + objs.join(' ')) do |l|
+ if (filetype = l[/^File Type: (.+)/, 1])..(/^\f/ =~ l)
+ case filetype
+ when /OBJECT/
+ next if /^[0-9A-F]+ 0+ UNDEF / =~ l
+ next unless l.sub!(/.*\sExternal\s+\|\s+/, '')
+ if l.sub!(/^_/, '')
+ next if /@.*@/ =~ l || /@[0-9a-f]{16}$/ =~ l
+ elsif !l.sub!(/^(\S+) \([^@?\`\']*\)$/, '\1')
+ next
+ end
+ when /DLL/
+ next unless l.sub!(/^\s*\d+\s+[[:xdigit:]]+\s+[[:xdigit:]]+\s+/, '')
+ end
+ SYM[l.strip] = true
end
- SYM[l.strip] = true
end
 
N

nobuyoshi nakada

Hi,

At Wed, 26 Oct 2005 11:29:24 +0900,
nobuyoshi nakada wrote in [ruby-talk:162652]:
It's possible to tweak win32/mkexports.rb to accept DLL files
as well as object files.
Oops,

+filetype = nil
+IO.foreach("|dumpbin -symbols -exports " + objs.join(' ')) do |l|
+ if (filetype = l[/^File Type: (.+)/, 1])..(/^\f/ =~ l)
+ case filetype
+ when /OBJECT/

Forgot /LIBRARY/ here.
 
D

Daniel Berger

nobuyoshi said:
Hi,

At Wed, 26 Oct 2005 07:42:03 +0900,
Daniel Berger wrote in [ruby-talk:162622]:
Ok, but you can generate a .lib file from a .def, and you can generate
a .def from a .dll. Here's a script I cobbled together that did the
trick:

Of course you can, but I don't think it should be done
silently by mkmf.rb. It belongs to setting up of a particular
library, not building an extension for it.
# Create a .def file from the results of a 'link -dump -exports'

Actually, it calls dumpbin.exe.

My research indicates that older compilers use dumpbin.exe, but the
more recent ones just use 'link -dump'. I don't have dumpbin.exe on my
machine for example, but I do have link.exe.
Really?

$ lib -machine:x86 -def:../stable-build/i686-mswin32/msvcrt-ruby18.def -out:../stable-build/i686-mswin32/msvcrt-ruby18.lib
Microsoft (R) Library Manager Version 7.10.2240.8
Copyright (C) Microsoft Corporation. All rights reserved.

Creating library ../stable-build/i686-mswin32/msvcrt-ruby18.lib and object ../stable-build/i686-mswin32/msvcrt-ruby18.exp

Perhaps it has something to do with the version of lib.exe I have at
work, because I had to download it off the web. For whatever reason,
the most recent versions of the free compiler from MS don't seem to
include lib.exe by default any more. That, or I messed something up.
I *do* have it on my older laptop, but not on my desktop, for example
(which has a more recent version).

Someone please correct me if I'm wrong, but I couldn't find it in any
of the packages MS supplied.
It's possible to tweak win32/mkexports.rb to accept DLL files
as well as object files.

Cool, but you're going to have to make a check to see if dumpbin exists
and use link -dump instead if it doesn't.

Regards,

Dan
 
D

Daniel Berger

nobuyoshi nakada wrote:

Really?

$ lib -machine:x86 -def:../stable-build/i686-mswin32/msvcrt-ruby18.def -out:../stable-build/i686-mswin32/msvcrt-ruby18.lib
Microsoft (R) Library Manager Version 7.10.2240.8
Copyright (C) Microsoft Corporation. All rights reserved.

Creating library ../stable-build/i686-mswin32/msvcrt-ruby18.lib and object ../stable-build/i686-mswin32/msvcrt-ruby18.exp

Oh, you know what? I meant to say "path names with spaces in them".
Sorry, bad comment.

Regards,

Dan
 
N

nobuyoshi nakada

Hi,

At Wed, 26 Oct 2005 13:47:03 +0900,
Daniel Berger wrote in [ruby-talk:162672]:
My research indicates that older compilers use dumpbin.exe, but the
more recent ones just use 'link -dump'. I don't have dumpbin.exe on my
machine for example, but I do have link.exe.

What version do you have?

With VC++2003, 'link.exe -dump' shows dumpbin.exe's message.

Microsoft (R) COFF Binary File Dumper Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

usage: DUMPBIN [options] [files]

and putting -exports option before -dump, i.e., 'link.exe
-exports -dump' errs.

LINK : warning LNK4044: unrecognized option '/exports'; ignored
LINK : warning LNK4044: unrecognized option '/dump'; ignored
Perhaps it has something to do with the version of lib.exe I have at
work, because I had to download it off the web. For whatever reason,
the most recent versions of the free compiler from MS don't seem to
include lib.exe by default any more. That, or I messed something up.
I *do* have it on my older laptop, but not on my desktop, for example
(which has a more recent version).

Packaging miss? Also, msvcrt.lib was lost in VC++2003.
Cool, but you're going to have to make a check to see if dumpbin exists
and use link -dump instead if it doesn't.

Like this?

if path = ENV["PATH"]
path = path.split(File::pATH_SEPARATOR)
else
path = %w[.]
end
dumpbin = "dumpbin.exe"
unless path.find {|dir| File.join(dir, dumpbin)}
dumpbin = "link.exe -dump"
end

IO.foreach("|#{dumpbin} -symbols -exports " + objs.join(' ')) do |l|
...
end
 

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,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top