Customizing environment on Windows (separate build paths)


First of all this title is a little misleading, but due to lack of better idea I decided to leave it as it is.

What this article is about (brief explanation)?
When You build an application most of the IDE (of Your choice) set up for You automatically system variables, like system “path” variable for example. That’s allow IDE to find appropriate libraries and use/link them. For example I use QtCreator to build Qt application and on application start up QtCreator set up system “path” variable and adds path to QtSDK, i.e. “c:\Qt”. That way when I press “run” in IDE application finds all dependences (dll) and runs fine. Problem starts when I close QtCreator, due to unset path variables, I can’t no longer run my application (at least not when path variables don’t point to the QtSDK directory with libraries).
If I remember correctly older release of Qt, up to 4.5, set up system path variables right after installation, but that changed with >4.6, and variables are “dynamically” set up by QtCreator (QtCreator->Projects->Run Settings->Run Environment). This is actually really good behaviour, because if developer use more then one release of SDK then there’s no way that wrong version of the library will be linked (assuming that there is no paths to the SDK in system path variable pointing to respective directories). But on other hand You can’t run, in this case, QtApplication outside QtCreator.

There is a few solution to solve that, i.e:

  • copy dll’s to application directory, but unfortunately You need to do that for every application, so it’s simply a waste of HDD space
  • copy dll’s to, for example, %SystemRoot%\system or %SystemRoot%\system32 directory. This is probably the worst solution but it will work for all applications.
  • set path variable, either system or user, manually and point it to, in this case, QtSDK. Better and cleaner solution the previous, but little confusing new developer when it comes to deployment (I mean, what dll redistribute with application?, switching between user will solve that – if path is not global)
  • in my humble opinion the most efficient way to handle this problem is to add custom option to the menu for the executable file, that will automatically set custom path when executable file is executed – and how to do that will be presented in this article.
  • After doing this, developer will end up with clean “path” variable for the system. Each executable will have another option, something like “Set env” (or whatever name You will chose), that will set up appropriate paths. And of course all executables will be run in the “old fashioned” way (using only standard system environment variables). See picture below.

    my_paths option

    Additional option for the executable files

     

     

    Disclaimer:
    Before You proceed with this it is strongly recommended to backup Your registry, in case something goes wrong (I don’t take any responsibility for the damage that This can cause to Your operating system).

     

    How to set it up:

    1. Open registry editor (regedit.exe) and find:
    HKEY_LOCAL_MACHINE\SOFTWARE\Classes\exefile

    2. Create additional keys under shell branch:
    NEW->Key, type key name. This name will be visible in the file menu.

    3. Create new sub key named command

    4. Modify / add value to the default string key, i.e:
    exe_env.bat %1, better %1 %*

    exe_env.bat should be in the default path, i.e. c:\windows

    Reg edit

    Registry editor, example command "My path".

    This will create new menu entry.
    Now set up batch, so it will add path to the environment and launch selected application.

    Example exe_env.bat file content:

    set QTDIR=C:\Qt\2010.05\qt
    echo -- QTDIR set to C:\Qt\2010.05\qt
    set QMAKESPEC=win32-g++
    echo -- win32-g++
    set PATH=%PATH%;C:\Qt\2010.05\mingw\bin;C:\Qt\2010.05\qt\bin;
    echo -- Added PATH --
    %*

    Create batch file based on this one, or simply copy / paste content, and save it in i.e. %SystemRoot% (most likely c:\windows)
    Description and hints:

    set PATH=c:\Qt\…

    set environment variable named PATH and add to it value “c:\Qt\…”

    %PATH%

    return current values associated with variable, in this case PATH += PATH + c:\Qt…

    Most important part is this one:

    %1, or better %*

    This will return first argument in the argument chain, if there is one, so to sum it up this will return path with the file name (.exe) that is currently executed / selected. This line actually run the program! %* return file path / file name / arguments chain (good for CLI prorgams)

    So You have it, custom option that will set up path when it’s needed. That way You can see how program will behave in user environment without switching users or using additional application / IDE.

    Worth to note here is how Windows OS look for the dll’s.
    By default OS looks in the:

    application directory
    %SystemRoot%
    %SystemRoot%\system
    %SystemRoot%\system32

    and then in every directory listed in the PATH variable, either user or global.

    In this article I used QtSDK as an example, but in reality this apply to any SDK / library that uses dynamic libraries.

    , , ,

    Comments are closed.