From e1efb1fd2efbadfa0a2b53662f2757c728e119ab Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sat, 3 Aug 2019 11:00:40 +0100 Subject: [PATCH 04/11] Fix compilation with C++. We lacked the appropriate extern "C" section in the header, so compilation would fail if you were using C++. This also adds a simple regression test. --- .gitignore | 1 + configure.ac | 11 +++++++++ generator/generator | 8 +++++++ tests/Makefile.am | 12 ++++++++++ tests/compile-cxx.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+) create mode 100644 tests/compile-cxx.cpp diff --git a/.gitignore b/.gitignore index e69d243..9558577 100644 --- a/.gitignore +++ b/.gitignore @@ -111,6 +111,7 @@ Makefile.in /tests/closure-lifetimes /tests/compile /tests/compile-ansi-c +/tests/compile-cxx /tests/compile-header-only /tests/connect-tcp /tests/connect-tls-certs diff --git a/configure.ac b/configure.ac index 30d7f90..8c47e61 100644 --- a/configure.ac +++ b/configure.ac @@ -44,6 +44,17 @@ AM_PROG_CC_C_O AX_PTHREAD +dnl Check for C++ (optional, we just use this to test the header +dnl can be included from C++ code). +AC_PROG_CXX + +dnl The C++ compiler test is pretty useless because even if it fails +dnl it sets CXX=g++. So test the compiler actually works. +AC_MSG_CHECKING([if the C++ compiler really really works]) +AS_IF([$CXX --version >&AS_MESSAGE_LOG_FD 2>&1],[have_cxx=yes],[have_cxx=no]) +AC_MSG_RESULT([$have_cxx]) +AM_CONDITIONAL([HAVE_CXX], [test "$have_cxx" = "yes"]) + AC_ARG_ENABLE([gcc-warnings], [AS_HELP_STRING([--enable-gcc-warnings], [turn on lots of GCC warnings (for developers)])], diff --git a/generator/generator b/generator/generator index 62ca99c..0d10214 100755 --- a/generator/generator +++ b/generator/generator @@ -3332,6 +3332,10 @@ let generate_include_libnbd_h () = pr "#include \n"; pr "#include \n"; pr "\n"; + pr "#ifdef __cplusplus\n"; + pr "extern \"C\" {\n"; + pr "#endif\n"; + pr "\n"; pr "struct nbd_handle;\n"; pr "\n"; pr "typedef void (*nbd_close_callback) (void *user_data);\n"; @@ -3361,6 +3365,10 @@ let generate_include_libnbd_h () = fun (ns, ctxts) -> print_ns ns ctxts ) metadata_namespaces; pr "\n"; + pr "#ifdef __cplusplus\n"; + pr "}\n"; + pr "#endif\n"; + pr "\n"; pr "#endif /* LIBNBD_H */\n" let generate_lib_unlocked_h () = diff --git a/tests/Makefile.am b/tests/Makefile.am index 59318b4..3064301 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -109,6 +109,18 @@ export_name_CPPFLAGS = -I$(top_srcdir)/include export_name_CFLAGS = $(WARNINGS_CFLAGS) export_name_LDADD = $(top_builddir)/lib/libnbd.la +if HAVE_CXX + +check_PROGRAMS += compile-cxx +TESTS += compile-cxx + +compile_cxx_SOURCES = compile-cxx.cpp +compile_cxx_CPPFLAGS = -I$(top_srcdir)/include +compile_cxx_CXXFLAGS = $(WARNINGS_CFLAGS) +compile_cxx_LDADD = $(top_builddir)/lib/libnbd.la + +endif HAVE_CXX + #---------------------------------------------------------------------- # The following tests require nbdkit as an NBD server to test against. diff --git a/tests/compile-cxx.cpp b/tests/compile-cxx.cpp new file mode 100644 index 0000000..4c8447c --- /dev/null +++ b/tests/compile-cxx.cpp @@ -0,0 +1,52 @@ +/* NBD client library in userspace + * Copyright (C) 2013-2019 Red Hat Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* Test compilation with C++. */ + +#ifndef __cplusplus +#error "this test should be compiled with a C++ compiler" +#endif + +#include + +#include +#include + +#include + +using namespace std; + +int +main () +{ + struct nbd_handle *nbd; + + nbd = nbd_create (); + if (nbd == NULL) { + cerr << nbd_get_error () << endl; + exit (EXIT_FAILURE); + } + + cout << nbd_get_package_name (nbd) + << " " + << nbd_get_version (nbd) + << endl; + + nbd_close (nbd); + exit (EXIT_SUCCESS); +} -- 2.22.0