1

I have few libraries of someone else's products, and few my libraries (dlls and headers). And for good structuring of my project (and just for increase my knowledge), I want to place dlls in others directories (not dir with exe).

As far as I know, Windows uses dlls (looking dlls for exe) only in exedir and sysdir. Also, exist regkeys which can help me, but it's global solution as PATH var.

1. Can I link exe with some dll, which placed in dir on lower hierarchy about exe? How?

Also, I want to use for this MinGW.

I found not enough information about my question, and I found this g++ keys for linker:
-Wl,-rpath-link,...; -Wl,-rpath,...;

2. Can I use this keys to solve my problem on Windows with MinGW? How?

I tried this:
g++ -Wl,-rpath-link,.\out\lib\ -Wl,-rpath,.\lib\ -L .\out\lib\ -l:foolib.dll main.o -o out\main.exe
but I still can run exe without errors only if dlls placed in exedir.

So, I found arg(?) $ORIGIN for -Wl,-rpath,... which as I understand, is fullpath to workdir for some obj file? Or not?

3. What is $ORIGIN? Should\Can I use it to solve my problem?

Sorry for my English. Thank you in advance.

qioalice
  • 15
  • 4

1 Answers1

4
  1. Can I link exe with some dll, which placed in dir on lower hierarchy about exe? How?

No. You cannot, unfortunately. The Windows DLL search path order is, by default, as follows:

  1. The directory from which the application loaded.
  2. The system directory. Use the GetSystemDirectory function to get the path of this directory.
  3. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
  4. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
  5. The current directory.
  6. The directories that are listed in the PATH environment variable.

So if you cannot place your DLLs in one of those directories, then they will not be visible by automatic dynamic loading. However, a possible alternative is to set PATH locally just before starting the EXE. For example, a BAT file that appends to PATH then launches the program, or another program you've written that does the same.

-Wl,-rpath-link,...; -Wl,-rpath,...;

  1. Can I use this keys to solve my problem on Windows with MinGW? How?

No. These do not apply to MinGW and will just be ignored. There is no equivalent on Windows. Unlike ELF binaries (e.g. Linux), EXE's do not support the specification of additional runtime search paths. See Linking with -R and -rpath switches on Windows and Is there a Windows/MSVC equivalent to the -rpath linker flag? for more information.

  1. What is $ORIGIN? Should\Can I use it to solve my problem?

No. $ORIGIN is just a special token that essentially just signifies "the path where this application is located". From the man page for ld:

  $ORIGIN (or equivalently ${ORIGIN})
          This expands to the directory containing the program or shared
          object.  ...

Since -rpath doesn't work on Windows anyways, this is irrelevant here.


So you'll have to either put the DLLs in their expected locations, or launch the executable through a batch file or other program that sets up PATH first.

By the way, please don't add anything to the system/user-wide PATH variable if you can help it. I know it's tempting, and many applications do this, but pollution of PATH becomes a problem for users especially when a lot of applications with conflicting versions of the same DLLs/EXEs are installed.

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182