Help: running a method from the importing file's method "space"

Rowan rowanreeve at gmail.com
Tue May 1 14:19:29 PDT 2012


On Tuesday, 1 May 2012 at 17:34:23 UTC, Regan Heath wrote:
> I'm not sure if any of this is helpful but I've had a tinker 
> and had some
> measure of success :p
>
> I think you have the following issues to resolve:
> 1. When you build the lib, you need to tell it that the 
> progMain symbol is
> to be found 'extern'ally.
> 2. When you build the exe, you have to make sure the linker 
> goes looking
> for WinMain and not main /and/ that it looks in the lib for it.
>
> I've managed to get something "working" but it's not what you 
> want (but it
> might help you get there) and it's not pretty...
>
> 1. Add the following line to mylib.d:
>             extern(C) int progMain();
>
>             This tells dmd that when it compiles mylib.d the 
> symbol progMain
> is
> to
> be found externally, and it's C linkage (I tried D linkage, but 
> this
> prepends "MyLib" to the symbol it searches for).
>
> 2. Modify test.d and make it read:
>             extern(C)
>             void progMain() {
>
>             This makes progMain a C linkage symbol, which the 
> linker will
> find
> and
> use.
>
> 3. Compile mylib.d with:
>              dmd -c mylib
>
>             (I know, this doesn't build a lib more on that 
> later..)
>
> 4. Compile test.d with:
>              dmd -c test
>
> 5. Run link.exe manually, e.g.
>
>             <path to dmd bin>\link.exe
> test+mylib,,nul,phobos+user32+kernel32/noi;
>
>             This produces a test.exe which runs and executes 
> progMain from
> test.d.
>
>
> Initially I was trying to get it working with a lib but I got 
> dead-ended.
>
> Using the code changes I mentioned above, and a test.def which 
> reads:
>             EXETYPE NT
>             SUBSYSTEM WINDOWS
>
> If you go:
>
>             dmd -lib mylib
>             dmd -c test
>             link.exe 
> test,,nul,mylib+phobos+user32+kernel32/noi,test.def;
>
> you get:
>
>             OPTLINK : Warning 134: No Start Address
>
> I think that the reason for this is that test.obj does not 
> define an entry
> point, see:
>             
> http://www.digitalmars.com/ctg/OptlinkErrorMessages.html
>             (No Start Address)
>
> This implies it is looking for entry points in object files, 
> and perhaps
> that means /not/ libs??  Not sure.
>
>
> So, I tried to re-use my earlier extern trick adding:
>
> extern(Windows)
> int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR 
> lpCmdLine,
> int nCmdShow);
>
> to test.d, but that gave the same results.
>
>
> References..
> http://dlang.org/windows.html
> http://www.digitalmars.com/ctg/optlink.html
> https://github.com/AndrejMitrovic/DWinProgramming

Thank you very much for the input, I tried to do what you said
and followed your instructions and went through link.exe and did
get a successful compile. What you have done does work (tested it
by omitting the .def file in link to get the console window and
did a writeln in WinMain for mylib AND progMain for test and it
does work (both wrileln's from WinMain and progMain print to
console) :-). This is pretty much what I wanted, yet it is still,
as you put it "not pretty", and making a lib for it isn't
working. It's a bit late here, so I'll tinker around tomorrow or
the day after with this and see if I can get towards more of a
cleaner way of doing this, I'll post back here with what I find.
Once again thank you, this seems like a nudge in the right
direction.

-Rowan


More information about the Digitalmars-d-learn mailing list