Carpetfizz
Carpetfizz

Reputation: 9179

Unable to link dynamic library in macOS

I'm trying to use a C library called quirc in my C project. So far, I have generated a libquirc.dylib.1.0 by modifying the Makefile which was using Linux .so files.

quirc/helloquirc.c

#include <quirc.h>
#include <stdio.h>


int main() {

    struct quirc *qr;

    qr = quirc_new();

    if (!qr) {
        printf("Failed to allocate memory");
    }

    quirc_destroy(qr);

    return 0;
}

I've created the above source file at the root of the repository. I'm using the following command to compile it:

gcc helloquirc.c -lquirc -L. -Ilib -o helloquirc

To my understanding the -l flag specifies the name of the dynamic library, the -L flag specifies the location of the dynamic library, the -I flag specifies the location of the header files, and -o specifies the name of the executable.

When I run this command I get the following error:

ld: library not found for -lquirc
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I changed the Makefile by using this line

.PHONY: libquirc.dylib
libquirc.dylib: libquirc.$(LIB_VERSION).dylib

libquirc.$(LIB_VERSION).dylib: $(LIB_OBJ)
    $(CC) -shared -dynamiclib -o $@ $(LIB_OBJ) $(LDFLAGS) -lm

and changing other instances of .so.$(LIB_VERSION) to .$(LIB_VERSION).dylib

Upvotes: 0

Views: 292

Answers (1)

Dietrich Epp
Dietrich Epp

Reputation: 213827

Something is wrong with the way quirc was built. The correct library name would be something like libquirc.1.0.dylib with a symlink named libquirc.dylib.

It looks like quirc has a handwritten makefile instead of using something sensible like gyp or cmake. Handwritten makefiles are just fine as long as you're not trying to build shared libraries on multiple platforms.

However, if you are just compiling it yourself, you may find things simpler if you just use a static library instead. There is no point in having a shared library if you are not sharing it with anybody (if no other programs are using the same exact copy of libquirc).

Upvotes: 2

Related Questions