Search code examples
c++macosqtdynamic-library

C++ Qt: Static builds and external dylibs


I've been doing some funny stuff in personal projects with Qt. I'm not an expert in this so if it's a dumb question (guess not I couldn't find anything useful) feel free to kill me.

Anyway, I'm in OSX and usually I compile the final versions against a static version of Qt using a shell script with this commands:

cd "project folder...";
PATH=/Users/TCB13/(...)/qt-source/bin:$PATH;  --> Path to my static QT.
export PATH;
qmake -config release; 
make; 
make clean;

So far if I move the compiled binary to another computer without Qt installed everything works just fine! ;)

Yesterday I start to play around with a dynamic library provided by a company and I've included the dylib in my .pro file like this: (I've a copy of the dylib in the project folder)

#macx: LIBS += -L$$PWD/ -lwpsapi

#INCLUDEPATH += $$PWD/
#DEPENDPATH += $$PWD/

And when I compile it "statically" (using the commands above) and run it on the other computer I get:

dyld: Library not loaded: @executable_path/libwpsapi.dylib
 Referenced from: /Users/TCB13/Desktop/dude111
 Reason: image not found
Trace/BPT trap: 5

I noticed that the size of my compiled binary is the same with or without including the dylib so, I googled how to include and external lib and some people are saying that I need to add "CONFIG += static" to my .pro file. I did it and the size of the file increased but I still got the same error.

Hope someone can help me.


Solution

  • The error message is pretty clear, you need to distribute libwpsapi.dylib with your application. If you want to avoid that, consult the documentation of this third-party library to make it use the statically linked code. Note that the fact that you are telling the linker to link a static library, does not mean that the code is referenced. I suspect that there is some kind of preprocessor #define in the code (or defaulted) to use the dynamically loaded library.