--- a/Makefile
+++ b/Makefile
@@ -9,26 +9,29 @@
 CC ?= gcc
 CXX ?= g++
 
-override CFLAGS := -W -Wall -Wextra -ansi -pedantic -O3 -Wno-unused-function $(CFLAGS)
-override CXXFLAGS := -W -Wall -Wextra -ansi -pedantic -O3 $(CXXFLAGS)
+CFLAGS += -W -Wall -Wextra -ansi -pedantic -Wno-unused-function
+CXXFLAGS += -W -Wall -Wextra -ansi -pedantic
 
-all: unittest benchmark pngdetail showpng
+all: unittest benchmark pngdetail showpng liblodepng.so
 
 %.o: %.cpp
 	@mkdir -p `dirname $@`
-	$(CXX) -I ./ $(CXXFLAGS) -c $< -o $@
+	$(CXX) -I ./ $(CXXFLAGS) -fPIC -c $< -o $@
 
-unittest: lodepng.o lodepng_util.o lodepng_unittest.o
-	$(CXX) $^ $(CXXFLAGS) -o $@
+liblodepng.so: lodepng.o
+	$(CXX) $(CXXFLAGS) $(LDFLAGS) -fPIC -shared -Wl,-soname,liblodepng.so $^ -o $@
 
-benchmark: lodepng.o lodepng_benchmark.o
-	$(CXX) $^ $(CXXFLAGS) -lSDL -o $@
+unittest: liblodepng.so lodepng_util.o lodepng_unittest.o
+	$(CXX) $^ $(CXXFLAGS) $(LDFLAGS) -fPIE -o $@ -L. -llodepng
 
-pngdetail: lodepng.o lodepng_util.o pngdetail.o
-	$(CXX) $^ $(CXXFLAGS) -o $@
+benchmark: liblodepng.so lodepng_benchmark.o
+	$(CXX) $^ $(CXXFLAGS) $(LDFLAGS) -fPIE -lSDL -o $@ -L. -llodepng
 
-showpng: lodepng.o examples/example_sdl.o
-	$(CXX) -I ./ $^ $(CXXFLAGS) -lSDL -o $@
+pngdetail: liblodepng.so lodepng_util.o pngdetail.o
+	$(CXX) $^ $(CXXFLAGS) $(LDFLAGS) -fPIE -o $@ -L. -llodepng
+
+showpng: liblodepng.so examples/example_sdl.o
+	$(CXX) -I ./ $^ $(CXXFLAGS) $(LDFLAGS) -fPIE -lSDL -o $@ -L. -llodepng
 
 clean:
 	rm -f unittest benchmark pngdetail showpng lodepng_unittest.o lodepng_benchmark.o lodepng.o lodepng_util.o pngdetail.o examples/example_sdl.o
