Adam Eberlin
Adam Eberlin

Reputation: 14205

dyld: Library not loaded: libmysql.16.dylib (MAC)

I started experimenting with the MySQL C API today, which I compiled today from source on my 10.6.8 macbook, and rolled out a quick makefile + app skeleton. I feel a bit rusty, though, because I can't seem to get it to link properly (fixed, now, it seems) or dynamically load the library (problem stated below).

The exact error I receive is below:

$ make clean && make
gcc -L/usr/local/mysql/lib -I/usr/local/mysql/include -lmysql -lpthread -ggdb -Wall -o adapter main.c adapter.c 

$ ./adapter
dyld: Library not loaded: libmysql.16.dylib
  Referenced from: /tmp/./adapter
  Reason: image not found
Trace/BPT trap

I looked around, and a lot of results regarding an install_name_tool came up. After reading through a few of those, though, I still don't completely understand what this actually does. There was a lot of talk about Frameworks, which didn't really fit into what I would expect to be involved in such a simple project..

Anyway, here is my code (including makefile).

main.c:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "adapter.h"

int main (int argc, char *argv[]) {
    printf("Version: %s\n", get_version());
    return 0;
}

adapter.c:

#include <mysql.h>

char *get_version () {
    return (char *)mysql_get_client_info();
}

adapter.h:

char *get_version ();

Makefile:

CC = gcc
MYSQL_FLAGS = -L/usr/local/mysql/lib -I/usr/local/mysql/include -lmysql -lpthread
CFLAGS = -ggdb -Wall
OBJS = 

adapter: $(OBJS)
    $(CC) $(MYSQL_FLAGS) $(CFLAGS) -o adapter main.c adapter.c $(OBJS)

clean:
    @rm -f *~ *.o adapter
    @rm -rf *.dSYM

And, a side note, the files really do exist:

$ ls /usr/local/mysql/lib
libmysql.16.0.0.dylib   libmysql.dylib      libmysqlclient.dylib
libmysql.16.dylib       libmysqlclient.a    libmysqlclient_r.dylib

$ ls /usr/local/mysql/include
atomic          my_atomic.h     my_md5.h        myisampack.h        sslopt-case.h
base64.h        my_attribute.h  my_net.h        mysql.h             sslopt-longopts.h
config-win.h    my_base.h       my_no_pthread.h mysql_com.h         sslopt-vars.h
decimal.h       my_bit.h        my_nosys.h      mysql_time.h        t_ctype.h
errmsg.h        my_bitmap.h     my_pthread.h    mysql_version.h     thr_alarm.h
hash.h          my_charsets.h   my_stacktrace.h mysqld_error.h      thr_lock.h
keycache.h      my_config.h     my_sys.h        mysys               typelib.h
lf.h            my_dbug.h       my_time.h       mysys_err.h         violite.h
m_ctype.h       my_dir.h        my_tree.h       queues.h            waiting_threads.h
m_string.h      my_getopt.h     my_trie.h       service_versions.h  wqueue.h
my_aes.h        my_global.h     my_uctype.h     sha1.h
my_alarm.h      my_libwrap.h    my_vle.h        sha2.h
my_alloc.h      my_list.h       my_xml.h        sql_common.h

I have a gut feeling that something is wrong with my Makefile or it's an issue particular to Mac OS X, but in reality, I'm really not sure what the problem is.

Upvotes: 1

Views: 3559

Answers (1)

user405725
user405725

Reputation:

This is a dynamic linker error and has nothing to do with source code. There are a lot of ways to fix it. The simplest perhaps is to add /usr/local/mysql/lib to the DYLD_LIBRARY_PATH before you run adapter. For example:

$ export DYLD_LIBRARY_PATH="${DYLD_LIBRARY_PATH}:/usr/local/mysql/lib"
$ ./adapter

Upvotes: 4

Related Questions