lvandeve / lodepng

PNG encoder and decoder in C and C++.
zlib License
2.06k stars 421 forks source link

Add recipe to build static or dynamic library? #49

Open tpham3783 opened 7 years ago

tpham3783 commented 7 years ago

Hi,

I wonder why isn't there any recipe to generate a static or dynamic library for this library? Should we add one?

jslee02 commented 7 years ago

According to this comment of the Author, this library is meant to be used as the source code itself rather than binaries. :smile:

tpham3783 commented 7 years ago

Understood, but if it is a static library, it is easier for the developer to link to it. Below is a Makefile to compile the library, developers can just link to it with "-static -llodepng" :-)

PREFIX?=/usr
INSTALL?=install

SRC=$(wildcard *.cpp)

SRC:=$(filter-out lodepng_unittest.cpp,$(SRC))
SRC:=$(filter-out lodepng_benchmark.cpp,$(SRC))

OBJS=$(SRC:.cpp=.o)

CXXFLAGS:=-fPIC -Os

TARGET=liblodepng.a liblodepng.so

all: $(TARGET)
    echo "Built libraries $(TARGET) successfully."

liblodepng.a: $(OBJS)
    $(AR) $(ARFLAGS) $@ $^

liblodepng.so: $(OBJS)
    $(CXX) -o $@ -shared $^

install: $(TARGET)
    $(INSTALL) lodepng.h lodepng_util.h $(DESTDIR)/$(PREFIX)/include

clean:
    rm -f $(OBJS) $(TARGET)
jslee02 commented 7 years ago

It could be. @lvandeve may have the right answer for you. I just replied as this reminds me the comment. :sweat:

lvandeve commented 7 years ago

Yes, it is only one source file and one header and is intended to be added directly as source files to your project. The source file also has no dependencies. It can also be renamed to have the .c extension to use as pure C only. There is a second source file lodepng_util but it is only needed for edge cases.

There is no makefile, it opens a whole can of worms and requires a lot of maintenance as platforms and popular make systems change. For this simple project, the actual C source is in fact simpler to be multiplatform than a makefile :)

I would suggest to integrate the source files in the build system you are using, it's only 1 file and one header after all.

jlaumon commented 6 years ago

What about adding a LODEPNG_API macro to give the user the ability of adding __declspec(dllexport) if needed? (Some other one-file libraries do it that way (eg. dear imgui))