4

I write applications in C++/Qt, and sometimes I have to compile them in Windows. For that I use MinGW. Unfortunately, that makes the executable dependant on mingwm10.dll and libgcc_s_dw2-1.dll. I prefer to solve such problems by just copying needed DLLs into application folder and distributing it like this. I use QMAKE_POST_LINK in the *.pro file to automate the process.

Unfortunately, for various reasons MinGW ends up in different directories on different systems I use. Is there a way to get the path to the compiler executable in Qt project? MinGW libs lie near its exe, so it would solve the problem. Thanks.

UPDATE

I'm afraid I didn't put myself clear enough.

I know that 90% of users don't have Qt, and 99% don't have MinGW installed on their Windows desktop. This is why I distribute all needed DLLs along with the executable, so that it can run everywhere without installing anything. I just copy them in the same folder with the *.exe. To automate this copying process, I add a copy command to the QMAKE_POST_LINK, which executes it after the linker finishes linking my program.

The problem is that this copy command still needs to know from where to copy MinGW libraries. Hence my question: how can one know the compiler path in compile time?

$$OUT_PWDin Qt *.pro file expands to the path to the directory where built application is put to. $$PWD expands to the project directory. Is there a similar macro (or maybe a hack) which gives the path to the compiler executable?

ScumCoder
  • 690
  • 1
  • 8
  • 20

1 Answers1

1

You are trying to approach the problem from the wrong angle. In particular, the solution you want will not be robust. It will still break on different systems, for example, the ones that have different version of MinGW toolchain installed and therefore different runtime DLLs, which if loaded by your application (built with another version of MinGW) will most probably cause crashes and undefined behavior during runtime. Furthermore, there might be systems which do not even have MinGW installed. These are the main reasons why applications built with certain version of toolchain should be distributed with the corresponding runtime DLLs included (this is what is called redistributable runtime) which is common practice on Windows, and there is nothing wrong with it.

However, there is much better way to solve your problem. Simply link all the MinGW runtime libraries statically into your application. This is a very good practice for Windows applications (and a very bad one for Unix ones). To do that, add the following to the corresponding *.pro:

QMAKE_LFLAGS_WINDOWS += -static-libgcc -static-libstdc++ -static

NOTE: Qt libraries themselves have to be built with these flags too, i.e. they should be independent of the MinGW runtime too. If not, then you'd still have a transitive dependency to the MinGW runtime because of the Qt libraries your application is linked against.

NOTE: _WINDOWS suffix will ensure that your application gets linked with these flags only for the Windows platform, while on Unix it will still link dynamically with runtimes (such as glibc.so on Linux) for the reasons discussed above.

After that your application will no longer explicitly depend on any MinGW runtime DLLs what will solve both distribution and maintenance headaches.

NOTE: If you ever decide to migrate from QMake to CMake (recommended), then here's how you do the same for CMake.

Update


You could check the QMAKE_CXX variable, but most likely it does not contain an absolute path to g++.exe. Usually it is simply g++, and the directory containing it is searched in PATH then. So in this case you'd have to parse PATH and check each directory in it for existence of g++ in it. This is very nasty logic to write in such a limited build system as QMake. So, as I stated above, you better off with 2 options:

  • Link runtimes statically into the application, so that you don't have to distribute anything. If you don't want to do it, then I want to know why - please write a comment;
  • Migrate to more feature-rich and flexible build system - CMake.
Community
  • 1
  • 1
Alexander Shukaev
  • 16,674
  • 8
  • 70
  • 85
  • I've clarified my question. – ScumCoder Dec 11 '13 at 17:17
  • Updated, take a look. – Alexander Shukaev Dec 11 '13 at 17:43
  • Thank you very much for your answer. Static linking is a very powerful and handy instrument, but unfortunately there are licence issues. For example, you have to either release your code under GPL or pay for commercial Qt version. MinGW don't even give you the latter option since it's GPL-only. I am a huge fan of OSS myself and I try to distribute my sources whenever I can, but my employer does not share this approach, so yeah. I'll mark your comment as answer since it looks like the closest possible solution. – ScumCoder Dec 12 '13 at 11:24
  • 1
    I think you've missed one important point. Don't think that I've proposed you to link with Qt libraries statically. Of course not, because then you'd need to buy license for them. You should still link dynamically with Qt. What I meant was that you better rebuild Qt from source (not download the one that comes from the official website) with MinGW and with the `-static-libgcc -static-libstdc++ -static` flags, so that dynamic Qt libraries that you will distribute with your application do not have dependency on MinGW runtime. Also build your own application with the same flags, and you're clean. – Alexander Shukaev Dec 12 '13 at 12:03
  • Whoa, building Qt from source sounds like a piece of work. I guess I'll just continue copying MinGW libs to application folder. Thanks again though, you are very helpful. – ScumCoder Dec 12 '13 at 14:25
  • Building Qt from source is actually a piece of cake if you've done it at least once. I do it all the time myself when new major version of MinGW-w64 or Qt comes out. This way I have full control over optimizations which are used to build Qt and that it's independent of MinGW runtimes. Try to find some time some day in order to learn how to do it since you are deeply involved with it. Trust me, it would be very useful and important skill. `;)`. Good luck! – Alexander Shukaev Dec 12 '13 at 15:06