diff --git a/.cproject b/.cproject
index 5cf09d1..6e85607 100644
--- a/.cproject
+++ b/.cproject
@@ -36,6 +36,24 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -53,4 +71,5 @@
+
\ No newline at end of file
diff --git a/Makefile b/Makefile
index 3d2904b..3b20de7 100644
--- a/Makefile
+++ b/Makefile
@@ -32,13 +32,15 @@ endif
# Link against thread lib
LDFLAGS += -pthread
+export
+
# Build for distribution
.PHONY: distro
distro: shared tools $(DOC_TARGETS)
# Build everything...
.PHONY: all
-all: shared static $(DOC_TARGETS) check
+all: shared static tools examples $(DOC_TARGETS) check
# Shared libraries (versioned and unversioned)
.PHONY: shared
@@ -53,6 +55,11 @@ static: $(LIB)/libredisx.a
tools: shared
make -f tools.mk
+# Examples
+.PHONY: examples
+examples: shared
+ make -f examples.mk
+
# Run regression tests
.PHONY: test
test:
@@ -147,7 +154,7 @@ INSTALL_PROGRAM ?= install
INSTALL_DATA ?= install -m 644
.PHONY: install
-install: install-libs install-bin install-man install-headers install-html
+install: install-libs install-bin install-man install-headers install-examples install-html
.PHONY: install-libs
install-libs:
@@ -175,13 +182,18 @@ install-man:
@install -d $(DESTDIR)$(mandir)/man1
$(INSTALL_DATA) -D man/man1/* $(DESTDIR)$(mandir)/man1
-
.PHONY: install-headers
install-headers:
@echo "installing headers to $(DESTDIR)$(includedir)"
install -d $(DESTDIR)$(includedir)
$(INSTALL_DATA) -D include/* $(DESTDIR)$(includedir)/
+.PHONY: install-examples
+install-examples:
+ @echo "installing examples to $(DESTDIR)$(docdir)"
+ install -d $(DESTDIR)$(docdir)
+ $(INSTALL_DATA) -D examples/* $(DESTDIR)$(docdir)/
+
.PHONY: install-html
install-html:
ifneq ($(wildcard apidoc/html/search/*),)
@@ -211,6 +223,7 @@ help:
@echo " analyze Performs static analysis with 'cppcheck'."
@echo " all All of the above."
@echo " distro shared libs and documentation (default target)."
+ @echo " examples Build example programs."
@echo " install Install components (e.g. 'make prefix= install')"
@echo " clean Removes intermediate products."
@echo " distclean Deletes all generated files."
diff --git a/examples.mk b/examples.mk
new file mode 100644
index 0000000..9eba1b9
--- /dev/null
+++ b/examples.mk
@@ -0,0 +1,11 @@
+SRC := examples
+
+include config.mk
+
+LDFLAGS += -L$(LIB) -lredisx
+LD_LIBRARY_PATH := $(LIB):$(LD_LIBRARY_PATH)
+
+.PHONY: all
+all: $(BIN)/hello
+
+include build.mk
diff --git a/examples/hello.c b/examples/hello.c
new file mode 100644
index 0000000..d729b93
--- /dev/null
+++ b/examples/hello.c
@@ -0,0 +1,59 @@
+#include
+#include
+
+#include
+
+/*
+ * Pings a Redis server with a hello message, and prints the response, or else an error.
+ *
+ * Usage: hello [host]
+ *
+ * Options:
+ * host The Redis server host (default: "127.0.0.1")
+ *
+ * Exit status:
+ * 0 Successful completion
+ * 1 Could not connect to Redis server
+ * 2 Bad response from server
+ *
+ * Author: Attila Kovacs
+ * Version: 19 December 2024
+ */
+int main(int argc, char *argv[]) {
+ // Initialize for Redis server at 'my-server.com'.
+ Redis *redis;
+ char *host = "127.0.0.1"; // Default host
+
+ if(argc > 1) host = argv[1]; // program argument designates host
+
+ // Initialize with the designated or default host
+ redis = redisxInit(host);
+
+ // Connect to Redis / Valkey server
+ if(redis != NULL && redisxConnect(redis, FALSE) == X_SUCCESS) {
+
+ // Execute 'PING "Hello World!"' query on the server
+ RESP *reply = redisxRequest(redis, "PING", "Hello World!", NULL, NULL, NULL);
+
+ // Check that we got a response of the expected type (bulk string of any length)
+ if(redisxCheckRESP(reply, RESP_BULK_STRING, 0) == X_SUCCESS)
+ printf("%s\n", (char *) reply->value);
+ else {
+ fprintf(stderr, "ERROR! bad response\n");
+ return 2;
+ }
+
+ // Clean up
+ redisxDestroyRESP(reply);
+ redisxDisconnect(redis);
+ }
+ else {
+ perror("ERROR! could not connect");
+ return 1;
+ }
+
+ // Free up the resources used by the 'redis' instance.
+ redisxDestroy(redis);
+
+ return 0;
+}