1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
From 5c152fc2966cad01dc403ebfa4137a589b415cac Mon Sep 17 00:00:00 2001
From: yangfl <yangfl@users.noreply.github.com>
Date: Mon, 22 Feb 2021 00:24:45 +0800
Subject: [PATCH 2/2] Make C++ shared lib
---
cpp/Makefile | 53 ++++++++++++++++++++++++++++++++-----
cpp/compat.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++
cpp/qrcodegen.cpp | 2 ++
cpp/qrcodegen.hpp | 18 +++++++++++++
cpp/qrcodegencpp.pc.in | 10 +++++++
5 files changed, 136 insertions(+), 6 deletions(-)
create mode 100644 cpp/compat.cpp
create mode 100644 cpp/qrcodegencpp.pc.in
diff --git a/cpp/Makefile b/cpp/Makefile
index 57dc0bf..d95c7e2 100644
--- a/cpp/Makefile
+++ b/cpp/Makefile
@@ -51,29 +51,70 @@ CXXFLAGS += -std=c++11 -O
# ---- Targets to build ----
LIB = qrcodegencpp
-LIBFILE = lib$(LIB).a
-LIBOBJ = qrcodegen.o
+LIBFILE = lib$(LIB).so
+ARFILE = lib$(LIB).a
+SO_NAME = $(LIBFILE).1
+REAL_NAME = $(LIBFILE).$(VERSION)
+PCFILE := $(LIB).pc
+HEADERS = qrcodegen.hpp
+LIBOBJ = qrcodegen.o compat.o
MAINS = QrCodeGeneratorDemo
+MULTIARCH ?= $(shell $(CC) --print-multiarch)
+
+PREFIX ?= /usr
+INCLUDEDIR ?= include/qrcodegen
+LIBDIR ?= lib/$(MULTIARCH)
+
# Build all binaries
-all: $(LIBFILE) $(MAINS)
+all: $(LIBFILE) $(ARFILE) $(MAINS)
# Delete build output
clean:
- rm -f -- $(LIBOBJ) $(LIBFILE) $(MAINS:=.o) $(MAINS)
+ rm -f -- $(LIBOBJ) $(LIBFILE) $(ARFILE) $(MAINS:=.o) $(MAINS) $(PCFILE)
rm -rf .deps
+.PHONY: install-shared
+install-shared: $(LIBFILE)
+ install -d $(DESTDIR)$(PREFIX)/$(LIBDIR) || true
+ install -m 0644 $< $(DESTDIR)$(PREFIX)/$(LIBDIR)/$(REAL_NAME)
+ rm -f $(DESTDIR)$(PREFIX)/$(LIBDIR)/$(SO_NAME)
+ ln -s $(REAL_NAME) $(DESTDIR)$(PREFIX)/$(LIBDIR)/$(SO_NAME)
+ rm -f $(DESTDIR)$(PREFIX)/$(LIBDIR)/$(LIBFILE)
+ ln -s $(SO_NAME) $(DESTDIR)$(PREFIX)/$(LIBDIR)/$(LIBFILE)
+
+.PHONY: install-static
+install-static: $(ARFILE)
+ install -d $(DESTDIR)$(PREFIX)/$(LIBDIR) || true
+ install -m 0644 $< $(DESTDIR)$(PREFIX)/$(LIBDIR)/$(ARLIB)
+
+.PHONY: install-header
+install-header: $(HEADERS) $(PCFILE)
+ install -d $(DESTDIR)$(PREFIX)/$(INCLUDEDIR) || true
+ install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/$(INCLUDEDIR)/
+ install -d $(DESTDIR)$(PREFIX)/$(LIBDIR)/pkgconfig || true
+ install -m 0644 $(PCFILE) $(DESTDIR)$(PREFIX)/$(LIBDIR)/pkgconfig
+
+.PHONY: install
+install: install-shared install-static install-header
+
# Executable files
%: %.o $(LIBFILE)
- $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $< -L . -l $(LIB)
+ $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $< -L . -l $(LIB) $(LDFLAGS)
# The library
$(LIBFILE): $(LIBOBJ)
+ $(CXX) -shared -Wl,-soname,$(SO_NAME) $(LDFLAGS) -o $@ $^
+
+$(ARFILE): $(LIBOBJ)
$(AR) -crs $@ -- $^
+$(PCFILE): $(PCFILE).in
+ sed 's|@prefix@|$(PREFIX)|; s|@libdir@|$(LIBDIR)|; s|@includedir@|$(INCLUDEDIR)|; s|@version@|$(VERSION)|' $< > $@
+
# Object files
%.o: %.cpp .deps/timestamp
- $(CXX) $(CXXFLAGS) -c -o $@ -MMD -MF .deps/$*.d $<
+ $(CXX) $(CPPFLAGS) $(CXXFLAGS) -fPIC -c -o $@ -MMD -MF .deps/$*.d $<
# Have a place to store header dependencies automatically generated by compiler
.deps/timestamp:
diff --git a/cpp/compat.cpp b/cpp/compat.cpp
new file mode 100644
index 0000000..19144ca
--- /dev/null
+++ b/cpp/compat.cpp
@@ -0,0 +1,59 @@
+#define QRCODEGEN_COMPAT
+
+#include <climits>
+#include <sstream>
+#include <stdexcept>
+#include <utility>
+#include "qrcodegen.hpp"
+
+
+namespace qrcodegen {
+
+QrSegment::QrSegment(Mode md, int numCh, const std::vector<bool> &dt) :
+ mode(&compatMode),
+ compatMode(md),
+ numChars(numCh),
+ data(dt) {
+ if (numCh < 0)
+ throw std::domain_error("Invalid value");
+}
+
+
+QrSegment::QrSegment(Mode md, int numCh, std::vector<bool> &&dt) :
+ mode(&compatMode),
+ compatMode(md),
+ numChars(numCh),
+ data(std::move(dt)) {
+ if (numCh < 0)
+ throw std::domain_error("Invalid value");
+}
+
+
+std::string QrCode::toSvgString(int border) const {
+ if (border < 0)
+ throw std::domain_error("Border must be non-negative");
+ if (border > INT_MAX / 2 || border * 2 > INT_MAX - getSize())
+ throw std::overflow_error("Border too large");
+
+ std::ostringstream sb;
+ sb << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
+ sb << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
+ sb << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 ";
+ sb << (getSize() + border * 2) << " " << (getSize() + border * 2) << "\" stroke=\"none\">\n";
+ sb << "\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n";
+ sb << "\t<path d=\"";
+ for (int y = 0; y < getSize(); y++) {
+ for (int x = 0; x < getSize(); x++) {
+ if (getModule(x, y)) {
+ if (x != 0 || y != 0)
+ sb << " ";
+ sb << "M" << (x + border) << "," << (y + border) << "h1v1h-1z";
+ }
+ }
+ }
+ sb << "\" fill=\"#000000\"/>\n";
+ sb << "</svg>\n";
+ return sb.str();
+}
+
+}
diff --git a/cpp/qrcodegen.cpp b/cpp/qrcodegen.cpp
index 0957b79..fd3b358 100644
--- a/cpp/qrcodegen.cpp
+++ b/cpp/qrcodegen.cpp
@@ -160,6 +160,7 @@ QrSegment QrSegment::makeEci(long assignVal) {
QrSegment::QrSegment(const Mode &md, int numCh, const std::vector<bool> &dt) :
mode(&md),
+ compatMode(md),
numChars(numCh),
data(dt) {
if (numCh < 0)
@@ -169,6 +170,7 @@ QrSegment::QrSegment(const Mode &md, int numCh, const std::vector<bool> &dt) :
QrSegment::QrSegment(const Mode &md, int numCh, std::vector<bool> &&dt) :
mode(&md),
+ compatMode(md),
numChars(numCh),
data(std::move(dt)) {
if (numCh < 0)
diff --git a/cpp/qrcodegen.hpp b/cpp/qrcodegen.hpp
index 9448982..1592028 100644
--- a/cpp/qrcodegen.hpp
+++ b/cpp/qrcodegen.hpp
@@ -152,6 +152,7 @@ class QrSegment final {
/* The mode indicator of this segment. Accessed through getMode(). */
private: const Mode *mode;
+ private: Mode compatMode;
/* The length of this segment's unencoded data. Measured in characters for
* numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.
@@ -170,7 +171,11 @@ class QrSegment final {
* The character count (numCh) must agree with the mode and the bit buffer length,
* but the constraint isn't checked. The given bit buffer is copied and stored.
*/
+#ifndef QRCODEGEN_COMPAT
public: QrSegment(const Mode &md, int numCh, const std::vector<bool> &dt);
+#else
+ public: QrSegment(Mode md, int numCh, const std::vector<bool> &dt);
+#endif
/*
@@ -178,7 +183,11 @@ class QrSegment final {
* The character count (numCh) must agree with the mode and the bit buffer length,
* but the constraint isn't checked. The given bit buffer is moved and stored.
*/
+#ifndef QRCODEGEN_COMPAT
public: QrSegment(const Mode &md, int numCh, std::vector<bool> &&dt);
+#else
+ public: QrSegment(Mode md, int numCh, std::vector<bool> &&dt);
+#endif
/*---- Methods ----*/
@@ -369,6 +378,15 @@ class QrCode final {
public: bool getModule(int x, int y) const;
+#ifdef QRCODEGEN_COMPAT
+ /*
+ * Returns a string of SVG code for an image depicting this QR Code, with the given number
+ * of border modules. The string always uses Unix newlines (\n), regardless of the platform.
+ */
+ public: std::string toSvgString(int border) const;
+#endif
+
+
/*---- Private helper methods for constructor: Drawing function modules ----*/
diff --git a/cpp/qrcodegencpp.pc.in b/cpp/qrcodegencpp.pc.in
new file mode 100644
index 0000000..6c170ba
--- /dev/null
+++ b/cpp/qrcodegencpp.pc.in
@@ -0,0 +1,10 @@
+prefix=@prefix@
+libdir=${prefix}/@libdir@
+includedir=${prefix}/@includedir@
+
+Name: qrcodegencpp
+Description: QR Code generator library in multiple languages - C++ version
+Version: @version@
+URL: https://github.com/nayuki/QR-Code-generator
+Libs: -L${libdir} -lqrcodegencpp
+Cflags: -I${includedir}
--
2.35.1
|