From: Keith Busch Date: Tue, 11 Feb 2020 01:38:36 +0000 (-0800) Subject: Add c++ inclusion support X-Git-Tag: v1.0-rc0~177 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=7da106fc54cdb79f425c9112beff157409a749f2;p=users%2Fsagi%2Flibnvme.git Add c++ inclusion support extern "C" the top level header file, and add a simple c++ compile test. Signed-off-by: Keith Busch --- diff --git a/.gitignore b/.gitignore index 25a04994..8ff3e73c 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ a.out *.so.* test/test +test/cpp examples/display-tree examples/display-columnar diff --git a/configure b/configure index 88663d6b..0d6ebc6d 100755 --- a/configure +++ b/configure @@ -192,6 +192,16 @@ if [ $? -eq 0 ]; then fi print_config "systemd" "${systemd}" +########################################## +# check for c++ +cpp="no" +which g++ > /dev/null 2> /dev/null +if [ $? -eq 0 ]; then + cpp="yes" +fi +print_config "cpp" "${cpp}" + + if test "$libuuid" = "yes"; then output_sym "CONFIG_LIBUUID" echo "override LDFLAGS += -luuid" >> $config_host_mak @@ -201,3 +211,6 @@ if test "$systemd" = "yes"; then output_sym "CONFIG_SYSTEMD" echo "override LDFLAGS += -lsystemd" >> $config_host_mak fi +if test "$cpp" = "yes"; then + output_mak "CONFIG_CPLUSPLUS" "y" +fi diff --git a/examples/Makefile b/examples/Makefile index 0a70e9b8..8cb7ded5 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -11,12 +11,8 @@ all_targets += telemetry-listen display-tree display-columnar discover-loop all: $(all_targets) -test_srcs := telemetry-listen.c display-tree.c display-columnar.c discover-loop.c - -test_objs := $(patsubst %.c,%.ol,$(test_srcs)) - %: %.c $(QUIET_CC)$(CC) $(CFLAGS) -o $@ $< -lnvme clean: - @rm -f $(all_targets) $(test_objs) + rm -f $(all_targets) diff --git a/src/libnvme.h b/src/libnvme.h index 66e17d0c..2a04bf8b 100644 --- a/src/libnvme.h +++ b/src/libnvme.h @@ -1,6 +1,10 @@ #ifndef _LIBNVME_H #define _LIBNVME_H +#ifdef __cplusplus +extern "C" { +#endif + #include "nvme/types.h" #include "nvme/ioctl.h" #include "nvme/fabrics.h" @@ -8,4 +12,8 @@ #include "nvme/tree.h" #include "nvme/util.h" +#ifdef __cplusplus +} +#endif + #endif /* _LIBNVME_H */ diff --git a/test/Makefile b/test/Makefile index b21e4643..ddb80b7a 100644 --- a/test/Makefile +++ b/test/Makefile @@ -5,18 +5,28 @@ include ../Makefile.quiet ifneq ($(MAKECMDGOALS),clean) include ../config-host.mak +else +CONFIG_CPLUSPLUS=y endif -all_targets += test +c_targets += test +ifdef CONFIG_CPLUSPLUS +cpp_targets += cpp +else +cpp_targets += +endif + +all_targets += $(c_targets) $(cpp_targets) all: $(all_targets) -%: %.c - $(QUIET_CC)$(CC) $(CFLAGS) -o $@ $< -lnvme +CXXFLAGS ?= -lstdc++ -test_srs := test.c +%: %.cc + $(QUIET_CC)$(CC) $(CFLAGS) $(CXXFLAGS) -o $@ $< -lnvme -test_objs := $(patsubst %.c,%.ol,$(test_srcs)) +%: %.c + $(QUIET_CC)$(CC) $(CFLAGS) -o $@ $< -lnvme clean: - @rm -f $(all_targets) $(test_objs) + rm -f $(all_targets) diff --git a/test/cpp.cc b/test/cpp.cc new file mode 100644 index 00000000..f294d001 --- /dev/null +++ b/test/cpp.cc @@ -0,0 +1,40 @@ +#include +#include + +int main() +{ + nvme_root_t r; + nvme_subsystem_t s; + nvme_ctrl_t c; + nvme_path_t p; + nvme_ns_t n; + + r = nvme_scan(); + if (!r) + return -1; + + nvme_for_each_subsystem(r, s) { + std::cout << nvme_subsystem_get_name(s) << " - NQN=" << nvme_subsystem_get_nqn(s) << "\n"; + nvme_subsystem_for_each_ctrl(s, c) { + std::cout << " `- " << nvme_ctrl_get_name(c) << " " << + nvme_ctrl_get_transport(c) << " " << + nvme_ctrl_get_address(c) << " " << + nvme_ctrl_get_state(c) << "\n"; + nvme_ctrl_for_each_ns(c, n) + std::cout << " `- " << nvme_ns_get_name(n) << + "lba size:" << nvme_ns_get_lba_size(n) << " lba max:" << + nvme_ns_get_lba_count(n) << "\n"; + nvme_ctrl_for_each_path(c, p) + std::cout << " `- " << nvme_path_get_name(p) << " " << + nvme_path_get_ana_state(p) << "\n"; + } + nvme_subsystem_for_each_ns(s, n) { + std::cout << " `- " << nvme_ns_get_name(n) << + "lba size:" << nvme_ns_get_lba_size(n) << " lba max:" << + nvme_ns_get_lba_count(n) << "\n"; + } + } + std::cout << "\n"; + + return 0; +}