[dpdk-dev] [RFC 2/4] examples: add c++ example application

Declan Doherty declan.doherty at intel.com
Tue Aug 2 22:37:47 CEST 2016


From: Anatoly Burakov <anatoly.burakov at intel.com>

Example application for C++ compilation

Signed-off-by: Declan Doherty <declan.doherty at intel.com>
---
 examples/Makefile                    |  1 +
 examples/helloworld-cpp/Makefile     | 51 +++++++++++++++++++++
 examples/helloworld-cpp/c_code.c     |  5 ++
 examples/helloworld-cpp/c_header.h   |  6 +++
 examples/helloworld-cpp/cpp_code.cpp | 89 ++++++++++++++++++++++++++++++++++++
 5 files changed, 152 insertions(+)
 create mode 100644 examples/helloworld-cpp/Makefile
 create mode 100644 examples/helloworld-cpp/c_code.c
 create mode 100644 examples/helloworld-cpp/c_header.h
 create mode 100644 examples/helloworld-cpp/cpp_code.cpp

diff --git a/examples/Makefile b/examples/Makefile
index 18b41b9..cb81c6c 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -46,6 +46,7 @@ endif
 DIRS-y += ethtool
 DIRS-y += exception_path
 DIRS-y += helloworld
+DIRS-y += helloworld-cpp
 DIRS-$(CONFIG_RTE_LIBRTE_PIPELINE) += ip_pipeline
 ifeq ($(CONFIG_RTE_LIBRTE_LPM),y)
 DIRS-$(CONFIG_RTE_IP_FRAG) += ip_reassembly
diff --git a/examples/helloworld-cpp/Makefile b/examples/helloworld-cpp/Makefile
new file mode 100644
index 0000000..f8f281c
--- /dev/null
+++ b/examples/helloworld-cpp/Makefile
@@ -0,0 +1,51 @@
+#   BSD LICENSE
+#
+#   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+#   All rights reserved.
+#
+#   Redistribution and use in source and binary forms, with or without
+#   modification, are permitted provided that the following conditions
+#   are met:
+#
+#     * Redistributions of source code must retain the above copyright
+#       notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above copyright
+#       notice, this list of conditions and the following disclaimer in
+#       the documentation and/or other materials provided with the
+#       distribution.
+#     * Neither the name of Intel Corporation nor the names of its
+#       contributors may be used to endorse or promote products derived
+#       from this software without specific prior written permission.
+#
+#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+#   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+#   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+#   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+#   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+#   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+#   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+#   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ifeq ($(RTE_SDK),)
+$(error "Please define RTE_SDK environment variable")
+endif
+
+# Default target, can be overriden by command line or environment
+RTE_TARGET ?= x86_64-native-linuxapp-gcc
+
+include $(RTE_SDK)/mk/rte.vars.mk
+
+# binary name
+APP = helloworld-cpp
+
+# all source are stored in SRCS-y
+SRCS-y := c_code.c cpp_code.cpp
+
+CFLAGS += -O3
+CFLAGS += $(WERROR_FLAGS)
+LDFLAGS += -lstdc++
+
+include $(RTE_SDK)/mk/rte.extapp.mk
diff --git a/examples/helloworld-cpp/c_code.c b/examples/helloworld-cpp/c_code.c
new file mode 100644
index 0000000..d349a3b
--- /dev/null
+++ b/examples/helloworld-cpp/c_code.c
@@ -0,0 +1,5 @@
+#include "c_header.h"
+
+int func(void) {
+	return 100500;
+}
diff --git a/examples/helloworld-cpp/c_header.h b/examples/helloworld-cpp/c_header.h
new file mode 100644
index 0000000..3668b60
--- /dev/null
+++ b/examples/helloworld-cpp/c_header.h
@@ -0,0 +1,6 @@
+#ifndef HEADER_H
+#define HEADER_H
+
+int func(void);
+
+#endif
diff --git a/examples/helloworld-cpp/cpp_code.cpp b/examples/helloworld-cpp/cpp_code.cpp
new file mode 100644
index 0000000..6919048
--- /dev/null
+++ b/examples/helloworld-cpp/cpp_code.cpp
@@ -0,0 +1,89 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+#include <sys/queue.h>
+
+#include <rte_memory.h>
+#include <rte_memzone.h>
+#include <rte_launch.h>
+#include <rte_eal.h>
+#include <rte_per_lcore.h>
+#include <rte_lcore.h>
+#include <rte_debug.h>
+
+#include <iostream>
+
+extern "C" {
+#include "c_header.h"
+};
+
+using namespace std;
+
+static int
+lcore_hello(__attribute__((unused)) void *arg)
+{
+	unsigned lcore_id;
+	lcore_id = rte_lcore_id();
+	printf("hello from core %u\n", lcore_id);
+	return 0;
+}
+
+int
+main(int argc, char **argv)
+{
+	int ret;
+	unsigned lcore_id;
+
+	cout << "Hellow world from C++" << endl;
+
+	cout << "Output from C code: " << func() << endl;
+
+	ret = rte_eal_init(argc, argv);
+	if (ret < 0)
+		rte_panic("Cannot init EAL\n");
+
+	/* call lcore_hello() on every slave lcore */
+	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
+		rte_eal_remote_launch(lcore_hello, NULL, lcore_id);
+	}
+
+	/* call it on master lcore too */
+	lcore_hello(NULL);
+
+	rte_eal_mp_wait_lcore();
+	return 0;
+}
-- 
2.5.5



More information about the dev mailing list