Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

MacOS Dynamic Library Problems (dylib)



  • I am having a problem getting a program to work with dynamic librarys. XCode 7.2.1, MacOS 10.10.5, Qt 5.6.0. I am looking for suggestions to solve this.

    The program works with statically linked libraries but I get the following error when trying to run the program using dynamically linked libraries:

    dyld: Library not loaded: libcompfiles.1.dylib
      Referenced from: /Users/ron/MyProjects/compview/Debug/compview.app/Contents/MacOS/compview
      Reason: image not found
    

    I ran otool -L on the compiled program and it shows the following:

    bash-3.2$ otool -L compview
    	libcompfiles.1.dylib (compatibility version 1.0.0, current version 1.0.0)
    	libgridfiles.1.dylib (compatibility version 1.0.0, current version 1.0.0)
    	/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1404.32.0)
    	/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 22.0.0)
    	@rpath/QtOpenGL.framework/Versions/5/QtOpenGL (compatibility version 5.6.0, current version 5.6.0)
    	@rpath/QtWidgets.framework/Versions/5/QtWidgets (compatibility version 5.6.0, current version 5.6.0)
    	@rpath/QtGui.framework/Versions/5/QtGui (compatibility version 5.6.0, current version 5.6.0)
    	@rpath/QtCore.framework/Versions/5/QtCore (compatibility version 5.6.0, current version 5.6.0)
    	/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
    ...
    

    The first entries from otool -L show the libraries but seem suspicious in that there is no path (or even @rpath). I think this is part of the problem but I am not sure why.

    On a side note the name of the library file generated after compiling doesn't have the version information: libgridfiles.dylib and not libgridfiles.1.dylib. I have both files as I made a copy of libgridfiles.dylib and called it libgridfiles.1.dylib so I don't think the name is an issue (?). Is it possible to have the library files created with the version info in the name along with a alias for the shortened name? It seems this is how the other libraries work.

    Program QMake Section:

    DEPENDPATH += 		../libcompfiles/lib	\
    			../libgridfiles/lib
    INCLUDEPATH += 		../libcompfiles/src \
    			../libgridfiles/src
    LIBS+=  			-L../libcompfiles/lib -lcompfiles \
    			-L../libgridfiles/lib -lgridfiles
    

  • Lifetime Qt Champion

    Hi,

    Either set the DYLD_LIBRARY_PATH environment variable to contain the path(s) where these libraries can be found or update the libraries id to contain the full path to where it is.



  • Yeah, those seem to be the options.

    I noticed the program ran fine when run through QtCreator. After some investigation I found it automatically set the DYLD_LIBRARY_PATH and DYLD_FRAMEWORK_PATH so this method works. The downside is that you cannot run the program through Finder or from a terminal window (will likely work if the libraries were 'installed' in a standard location).

    I was able to get it to run through XCode by adding post link step that added the necessary path information to the library referenced in the executable:

    # very rough but it works.
    QMAKE_POST_LINK += install_name_tool -change libcompfiles.1.dylib /Users/ron/MyProjects/libcompfiles/lib/libcompfiles.dylib $$OUT_PWD/Debug/compview.app/Contents/MacOS/$$TARGET $$escape_expand(\n)
    QMAKE_POST_LINK += install_name_tool -change libgridfiles.1.dylib /Users/ron/MyProjects/libgridfiles/lib/libgridfiles.dylib $$OUT_PWD/Debug/compview.app/Contents/MacOS/$$TARGET
    

    One plus is that this solves the other issue where the library file included the version number (the change option from install_name_tool allowed me to use the non-versioned library name).

    I need to find a way to clean up the post link commands so they are more generic (use variables instead of hard coded paths for example). I am assuming using a full path makes it easier for macdeployqt (?). It seems like something is wrong though as I didn't expect I would need to do all this to get it to work on this OS.


  • Lifetime Qt Champion

    IIRC, you should be able to achieve that using the OUT_PWD variable.


Log in to reply