Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-3431

Libraries linked with libmongoc overlinked if found via cmake

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 1.16.0
    • Affects Version/s: 1.15.2
    • Component/s: cmake
    • None
    • Environment:
      macOS

      On macOS 10.13.6, I have mongo-c-driver 1.15.2 installed with MacPorts, and then I built mongo-cxx-driver 3.4.0 and noticed that the library it installed is overlinked:

      $ otool -L /opt/local/lib/libmongocxx.3.4.0.dylib
      /opt/local/lib/libmongocxx.3.4.0.dylib:
      	/opt/local/lib/libmongocxx._noabi.dylib (compatibility version 0.0.0, current version 3.4.0)
      	/opt/local/lib/libbsoncxx._noabi.dylib (compatibility version 0.0.0, current version 3.4.0)
      	/opt/local/lib/libmongoc-1.0.0.dylib (compatibility version 0.0.0, current version 0.0.0)
      	/opt/local/lib/libbson-1.0.0.dylib (compatibility version 0.0.0, current version 0.0.0)
      	/opt/local/lib/libsasl2.3.dylib (compatibility version 4.0.0, current version 4.0.0)
      	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1455.11.0)
      	/System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 58286.70.14)
      	/usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0)
      	/opt/local/lib/libsnappy.1.dylib (compatibility version 1.0.0, current version 1.1.7)
      	/opt/local/lib/libicuuc.65.dylib (compatibility version 65.0.0, current version 65.1.0)
      	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0)
      	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4)
      

      Those references to libsasl2, libresolv, libsnappy, libicuuc, and the Security and CoreFoundation frameworks should not be there because libmongocxx does not itself use them. Only its dependency libmongoc uses them.

      I discovered that the reason for this is that mongo-cxx-driver finds out how to link with libmongoc by checking the file libmongoc-1.0-config.cmake. This file erroneously lists all of its dependencies; it should not since that's not needed for dynamic linking. (Compare with libmongoc-static-1.0-config.cmake which does and should list all of those dependencies, since it's needed for static linking.)

      The problem would not have arisen if mongo-cxx-driver had used pkg-config instead of cmake to find mongo-c-driver, because mongo-c-driver's libmongoc-1.0.pc already correctly does not list those libraries in its Libs line:

      $ pkg-config libmongoc-1.0 --libs
      -L/opt/local/lib -lmongoc-1.0 -lbson-1.0
      

      and libmongoc-static-1.0.pc also already correctly does list those libraries:

      $ pkg-config libmongoc-static-1.0 --libs --static
      -L/opt/local/lib -lmongoc-static-1.0 -lsasl2 -framework CoreFoundation -framework Security -lresolv -lz -lsnappy -lzstd -licuuc -lbson-static-1.0 /usr/lib/libm.dylib
      

      I fixed it by removing the lines from libmongoc-1.0-config.cmake that add the libraries, as in the attached patch. After rebuilding mongo-cxx-driver with that, it's now linked like this:

      $ otool -L /opt/local/lib/libmongocxx.3.4.0.dylib
      /opt/local/lib/libmongocxx.3.4.0.dylib:
      	/opt/local/lib/libmongocxx._noabi.dylib (compatibility version 0.0.0, current version 3.4.0)
      	/opt/local/lib/libbsoncxx._noabi.dylib (compatibility version 0.0.0, current version 3.4.0)
      	/opt/local/lib/libmongoc-1.0.0.dylib (compatibility version 0.0.0, current version 0.0.0)
      	/opt/local/lib/libbson-1.0.0.dylib (compatibility version 0.0.0, current version 0.0.0)
      	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0)
      	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4)
      

      Much better.

      The relevant file has already been removed from your git master due to CDRIVER-3047. I haven't tested master so I don't know if the problem still exists there.

            Assignee:
            roberto.sanchez@mongodb.com Roberto Sanchez
            Reporter:
            ryandesign Ryan Schmidt
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: