diff --git a/Makefile b/Makefile
index 33f2cd69..95b7c0f5 100644
--- a/Makefile
+++ b/Makefile
@@ -42,7 +42,11 @@ else
   LIBTCC=libtcc$(DLLSUF)
   export LD_LIBRARY_PATH := $(CURDIR)/$(TOP)
   ifneq ($(CONFIG_rpath),no)
-   LINK_LIBTCC += -Wl,-rpath,"$(libdir)"
+    ifndef CONFIG_OSX
+      LINK_LIBTCC += -Wl,-rpath,"$(libdir)"
+    else
+      LINK_LIBTCC += -Wl,-rpath,"@executable_path/$(TOP)"
+    endif
   endif
  endif
  NATIVE_TARGET = $(ARCH)
@@ -52,7 +56,6 @@ else
     LDFLAGS += -flat_namespace -undefined warning
   endif
   export MACOSX_DEPLOYMENT_TARGET := 10.6
-  export DYLD_LIBRARY_PATH := $(CURDIR)/$(TOP)
  endif
 endif
 
@@ -240,7 +243,7 @@ libtcc.so: LDFLAGS+=-fPIC
 
 # OSX dynamic libtcc library
 libtcc.dylib: $(LIBTCC_OBJ)
-	$S$(CC) -shared -o $@ $^ $(LDFLAGS)
+	$S$(CC) -shared -install_name @rpath/$@ -o $@ $^ $(LDFLAGS)
 
 # windows dynamic libtcc library
 libtcc.dll : $(LIBTCC_OBJ)
diff --git a/tests/Makefile b/tests/Makefile
index d33fb7c7..0b74fbc9 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -62,6 +62,11 @@ ifeq ($(OS),Windows_NT) # for libtcc_test to find libtcc.dll
  PATH := $(CURDIR)/$(TOP)$(if $(findstring ;,$(PATH)),;,:)$(PATH)
 endif
 
+ifdef CONFIG_OSX
+LIBS += $(LINK_LIBTCC)
+endif
+
+
 ifeq ($(ARCH),arm)
 # tcctest refers to the alignment of functions, and with thumb mode
 # the low bit of code addresses selects the mode, so the "alignment"