www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Additional path for libs

reply Heinz <malagana15 yahoo.com> writes:

May be a silly situation but i'm trying to append a search path for
libraries from the command line. I'm running under win32 with
DMD/OPTLINK and i use a batch file for compilation of my project.
Adding the path to sc.ini won't help because the project' sources
might be moving around in different machines and folders so i'm
trying to set a path relative to the sources when invoking dmd.
Copying sc.ini to the source path won't help much because DMD might
be in different locations at other machines and also the idea is
not to mess with the config file.
Setting the LIB enviroment variable doesn't produce a positive
Here's my batch file:

  echo on
set LIB=..\lib
dmd -O -release -inline -ofdemo.exe -I..\import main.d main.def

The libs are called from sources with pragma(lib, "mylib.lib");
Any tips on how to accomplish this, if possible?

Feb 23 2011
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
See this:


You pass switches to the linker via -L, so the switch might be:

Of course you would have to update the LIB environment variable in
command line, or via a batch file before calling DMD:
 set lib=C:\DirWithLibFiles\;%lib%
I think that should do it, although I haven't tested it.
Feb 23 2011
parent reply Heinz <malagana15 yahoo.com> writes:

Thanks for the reply. I already tried /SCANLIB before with no
I couldn't find a solution so i'm sticking to the classic method of
declaring each lib in the dmd command line.
Lib paths at command line could be added to OPTLINK, it should work
in the same way the DMD -I argument work. This is beyond our scope
anyway, this is a task for Walter.

Thanks anyway.
Feb 24 2011
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
My bad. DMD uses sc.ini (on windows) to set the LIB path, which will
overwrite anything already in the LIB variable once you invoke DMD.

What you can do is change sc.ini (or the equivalent .conf file on
linux), so it appends and not overwrites the LIB path.

e.g. before:
LIB="% P%\..\lib";\dm\lib;

LIB="% P%\..\lib";\dm\lib;%LIB%

After modifying DMD\DMD2\Windows\Bin\sc.ini I can test it in a project:

module main;
import std.stdio;
pragma(lib, "mylib.lib");
extern(C) int foo(int x, int y);
void main()
    assert(foo(5, 5) == 10);

extern(C) int foo(int x, int y)
    return x + y;

I compile my lib:
D:\test\libfolder\dmd -lib mylib.d

Then I run this batch file from D:\test\:
set LIB=%cd%\libfolder;%LIB%
dmd main.d

Our project compiles.

The assert passes.

I don't really know why sc.ini is configued to /overwrite/ the LIB
variable, when it can just prepend any paths that it requires before
the rest of the contents of LIB. This shouldn't cause any conflicts.

You'll have to ask Walter about why sc.ini is configured this way (I'm
curious too).
Feb 24 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
One more thing:

If your lib files are relative to your current working directory, then
you can just pass them to DMD. E.g. if your lib file is under the
subdir folder of your current working dir you can just use:

dmd main.d subdir\mylibfile.lib

But maybe you knew that already.
Feb 23 2011
prev sibling parent Bekenn <leaveme alone.com> writes:
Apologies for the late reply, but this could help:

As you've already seen in the other replies, the provided sc.ini file is 
confounding your attempts at setting the LIB environment variable. 
However, OPTLINK's search path for sc.ini includes the current 
directory, so if you're reluctant to alter the original file, you can 
just create an empty sc.ini in the current directory.  You are then free 
to redefine LIB and DFLAGS to whatever you like.

Here's an excerpt from one of my makefiles (I use Microsoft's NMAKE):

	 move /y "$(APPNAME).map" $( D) > NUL

sc.ini :
	 copy NUL sc.ini
Feb 26 2011