From 05b9d50e874d20f050715d66ef9ee53faa4cf10f Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Wed, 26 Jul 2023 18:51:50 +0200 Subject: [PATCH 1/4] guard C API with extern "C" allows compiling both, C and C++ API at the same time --- lodepng.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lodepng.h b/lodepng.h index 81d49853..c33d2686 100644 --- a/lodepng.h +++ b/lodepng.h @@ -28,6 +28,7 @@ freely, subject to the following restrictions: #include /*for size_t*/ +extern "C" { extern const char* LODEPNG_VERSION_STRING; /* @@ -101,6 +102,7 @@ in a comment in the lodepng.c(pp) file in the 'else' case of the searchable LODE or comment out LODEPNG_COMPILE_CRC below*/ #define LODEPNG_COMPILE_CRC #endif +} /*compile the C++ version (you can disable the C++ wrapper here even when compiling for C++)*/ #ifdef __cplusplus @@ -116,6 +118,7 @@ or comment out LODEPNG_COMPILE_CPP below*/ #include #endif /*LODEPNG_COMPILE_CPP*/ +extern "C" { #ifdef LODEPNG_COMPILE_PNG /*The PNG color types (also used for raw image).*/ typedef enum LodePNGColorType { @@ -246,6 +249,7 @@ unsigned lodepng_encode24_file(const char* filename, #endif /*LODEPNG_COMPILE_DISK*/ #endif /*LODEPNG_COMPILE_ENCODER*/ +} #ifdef LODEPNG_COMPILE_CPP namespace lodepng { @@ -303,6 +307,7 @@ unsigned encode(const std::string& filename, #endif /*LODEPNG_COMPILE_CPP*/ #endif /*LODEPNG_COMPILE_PNG*/ +extern "C" { #ifdef LODEPNG_COMPILE_ERROR_TEXT /*Returns an English description of the numerical error code.*/ const char* lodepng_error_text(unsigned code); @@ -1088,6 +1093,7 @@ to handle such files and encode in-memory */ unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename); #endif /*LODEPNG_COMPILE_DISK*/ +} #ifdef LODEPNG_COMPILE_CPP /* The LodePNG C++ wrapper uses std::vectors instead of manually allocated memory buffers. */ From 2dd89fec4c8ae35521699b854604a0d25343b21d Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Wed, 26 Jul 2023 18:52:35 +0200 Subject: [PATCH 2/4] add shared library target * compile as shared library * compile static with -fPIC --- Makefile | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 78afa1ba..8c8c0722 100644 --- a/Makefile +++ b/Makefile @@ -9,10 +9,10 @@ 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) +override CFLAGS := -W -Wall -Wextra -ansi -pedantic -O3 -Wno-unused-function -fPIC $(CFLAGS) +override CXXFLAGS := -W -Wall -Wextra -ansi -pedantic -O3 -fPIC $(CXXFLAGS) -all: unittest benchmark pngdetail showpng +all: unittest benchmark pngdetail showpng liblodepng.so %.o: %.cpp @mkdir -p `dirname $@` @@ -30,5 +30,8 @@ pngdetail: lodepng.o lodepng_util.o pngdetail.o showpng: lodepng.o examples/example_sdl.o $(CXX) -I ./ $^ $(CXXFLAGS) -lSDL2 -o $@ +liblodepng.so: lodepng.o + $(CXX) $(CXXFLAGS) $^ -shared -o $@ + clean: rm -f unittest benchmark pngdetail showpng lodepng_unittest.o lodepng_benchmark.o lodepng.o lodepng_util.o pngdetail.o examples/example_sdl.o From d6d56f5ef9044b0d53733b2fa47fc17cf337dffb Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Wed, 26 Jul 2023 19:03:58 +0200 Subject: [PATCH 3/4] only compile extern "C" if c++ is used --- lodepng.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lodepng.h b/lodepng.h index c33d2686..e31393dc 100644 --- a/lodepng.h +++ b/lodepng.h @@ -28,7 +28,9 @@ freely, subject to the following restrictions: #include /*for size_t*/ +#ifdef __cplusplus extern "C" { +#endif extern const char* LODEPNG_VERSION_STRING; /* @@ -102,7 +104,10 @@ in a comment in the lodepng.c(pp) file in the 'else' case of the searchable LODE or comment out LODEPNG_COMPILE_CRC below*/ #define LODEPNG_COMPILE_CRC #endif + +#ifdef __cplusplus } +#endif /*compile the C++ version (you can disable the C++ wrapper here even when compiling for C++)*/ #ifdef __cplusplus @@ -118,7 +123,10 @@ or comment out LODEPNG_COMPILE_CPP below*/ #include #endif /*LODEPNG_COMPILE_CPP*/ +#ifdef __cplusplus extern "C" { +#endif + #ifdef LODEPNG_COMPILE_PNG /*The PNG color types (also used for raw image).*/ typedef enum LodePNGColorType { @@ -249,7 +257,9 @@ unsigned lodepng_encode24_file(const char* filename, #endif /*LODEPNG_COMPILE_DISK*/ #endif /*LODEPNG_COMPILE_ENCODER*/ +#ifdef __cplusplus } +#endif #ifdef LODEPNG_COMPILE_CPP namespace lodepng { @@ -307,7 +317,10 @@ unsigned encode(const std::string& filename, #endif /*LODEPNG_COMPILE_CPP*/ #endif /*LODEPNG_COMPILE_PNG*/ +#ifdef __cplusplus extern "C" { +#endif + #ifdef LODEPNG_COMPILE_ERROR_TEXT /*Returns an English description of the numerical error code.*/ const char* lodepng_error_text(unsigned code); @@ -1093,7 +1106,10 @@ to handle such files and encode in-memory */ unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename); #endif /*LODEPNG_COMPILE_DISK*/ + +#ifdef __cplusplus } +#endif #ifdef LODEPNG_COMPILE_CPP /* The LodePNG C++ wrapper uses std::vectors instead of manually allocated memory buffers. */ From 8b79e379e3bf2ba5b37d40037c27a84ffd5e53eb Mon Sep 17 00:00:00 2001 From: akallabeth Date: Tue, 1 Aug 2023 11:35:06 +0200 Subject: [PATCH 4/4] add a target to create a static library --- Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8c8c0722..26aa79c1 100644 --- a/Makefile +++ b/Makefile @@ -8,11 +8,12 @@ CC ?= gcc CXX ?= g++ +AR ?= ar override CFLAGS := -W -Wall -Wextra -ansi -pedantic -O3 -Wno-unused-function -fPIC $(CFLAGS) override CXXFLAGS := -W -Wall -Wextra -ansi -pedantic -O3 -fPIC $(CXXFLAGS) -all: unittest benchmark pngdetail showpng liblodepng.so +all: unittest benchmark pngdetail showpng liblodepng.so liblodepng.a %.o: %.cpp @mkdir -p `dirname $@` @@ -33,5 +34,8 @@ showpng: lodepng.o examples/example_sdl.o liblodepng.so: lodepng.o $(CXX) $(CXXFLAGS) $^ -shared -o $@ +liblodepng.a: lodepng.o + $(AR) rcs $@ $^ + clean: rm -f unittest benchmark pngdetail showpng lodepng_unittest.o lodepng_benchmark.o lodepng.o lodepng_util.o pngdetail.o examples/example_sdl.o