--- main/Makefile
+++ main/Makefile
@@ -165,6 +165,13 @@
 AST_EMBED_LIBS:=$(foreach dep,$(EMBED_LIBS),$(value $(dep)))
 OBJS:=$(sort $(OBJS))
 
+ifneq ($(findstring EMBED_RES,$(MENUSELECT_EMBED)),)
+  ifeq ($(findstring res_rtp_asterisk,$(MENUSELECT_RES)),)
+    AST_EMBED_LDFLAGS+=$(shell $(MAKE) --no-print-directory --quiet -C $(ASTTOPDIR)/res PJPROJECT_EMBED_LDFLAGS)
+    AST_EMBED_LIBS+=$(shell $(MAKE) --no-print-directory --quiet -C $(ASTTOPDIR)/res PJPROJECT_EMBED_LIBS)
+  endif
+endif
+
 ifneq ($(wildcard ../channels/h323/Makefile.ast),)
   include ../channels/h323/Makefile.ast
 else
--- res/Makefile
+++ res/Makefile
@@ -70,31 +70,29 @@
 ael/pval.o: ael/pval.c
 
 clean::
-	@if [ -f pjproject/build.mak ]; then $(MAKE) -C pjproject realclean; fi
 	rm -f snmp/*.o snmp/*.i ael/*.o ael/*.i ais/*.o ais/*.i snmp/*.gcda snmp/*.gcno ael/*.gcda ael/*.gcno
 
 dist-clean:: distclean
 distclean::
-	rm -f pjproject/build.mak
 
-pjproject/build.mak: pjproject/aconfigure
-	(cd pjproject && CFLAGS="-fPIC" ./configure --build=$(BUILD_PLATFORM) --host=$(HOST_PLATFORM) --disable-floating-point --disable-sound --disable-oss --disable-speex-aec --disable-l16-codec --disable-gsm-codec --disable-g722-codec --disable-g7221-codec --disable-speex-codec --disable-ilbc-codec --disable-g711-codec)
-
-ifneq ($(findstring $(MAKECMDGOALS),all),)
--include pjproject/build.mak
+ifneq ($(findstring $(MAKECMDGOALS),all PJPROJECT_EMBED_LDFLAGS PJPROJECT_EMBED_LIBS),)
+ifeq ($(strip $(PJPROJECT_BUILD_MAK_DIR)),)
+$(error PJPROJECT_BUILD_MAK_DIR variable pointing to a directory containing pjproject build.mak file is not set)
+endif
+include $(strip $(PJPROJECT_BUILD_MAK_DIR))/build.mak
 endif
 
 .PHONY: FORCE
 FORCE:
 
-$(PJ_LIB_FILES): FORCE
-	$(MAKE) -C $(patsubst %/lib/,%,$(dir $@))/build/ ../lib/$(notdir $@)
-
-ifeq ($(subst 1:0,1,$(UUID)), 1)
-res_rtp_asterisk.o: $(PJ_LIB_FILES)
+res_rtp_asterisk.o: _ASTCFLAGS+=$(filter-out -I%,$(PJ_CFLAGS))
 res_rtp_asterisk.o: _ASTCFLAGS+=-DUSE_PJPROJECT
-res_rtp_asterisk.o: _ASTCFLAGS+=$(PJ_CFLAGS)
-res_rtp_asterisk.so: _ASTLDFLAGS+=$(PJ_LDFLAGS)
-res_rtp_asterisk.so: LIBS+=$(PJ_LDLIBS)
-endif
+res_rtp_asterisk.so: _ASTLDFLAGS+=$(filter-out -L%,$(PJ_LDFLAGS))
+res_rtp_asterisk.so: LIBS+=-Wl,--as-needed $(PJ_LDLIBS) -Wl,--no-as-needed
+
+PJPROJECT_EMBED_LDFLAGS: FORCE
+	@echo "$(filter-out -L%,$(PJ_LDFLAGS))"
+
+PJPROJECT_EMBED_LIBS: FORCE
+	@echo "-Wl,--as-needed $(PJ_LDLIBS) -Wl,--no-as-needed"
 
--- res/res_rtp_asterisk.c
+++ res/res_rtp_asterisk.c
@@ -29,7 +29,6 @@
  */
 
 /*** MODULEINFO
-	<use type="external">uuid</use>
 	<support_level>core</support_level>
  ***/
 
@@ -50,11 +49,13 @@
 #ifdef USE_PJPROJECT
 /* Asterisk discourages the use of bzero in favor of memset, in fact if you try to use bzero it will tell you to use memset. As a result bzero has to be undefined
  * here since it is used internally by pjlib. The only other option would be to modify pjlib... which won't happen. */
+#if 0 /* disable this ugly hack as it doesn't work with uClibc, we use a patched version of pjlib */
 #undef bzero
 #define bzero bzero
-#include "pjlib.h"
-#include "pjlib-util.h"
-#include "pjnath.h"
+#endif
+#include <pjlib.h>
+#include <pjlib-util.h>
+#include <pjnath.h>
 #endif
 
 #include "asterisk/stun.h"
