From 7a6c904f94e64007352f5b936eafa4101b4efcab Mon Sep 17 00:00:00 2001 From: Gudmundur Adalsteinsson Date: Sat, 10 Oct 2020 13:22:26 +0000 Subject: [PATCH] Problem: Examples are not compiled Solution: Compile the examples when tests are compiled and using C++11 or greater. --- CMakeLists.txt | 3 +++ examples/CMakeLists.txt | 23 +++++++++++++++++++++++ examples/pubsub_multithread_inproc.cpp | 10 ++++++---- 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 examples/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index c860dc4..1fdf04a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,4 +101,7 @@ option(CPPZMQ_BUILD_TESTS "Whether or not to build the tests" ON) if (CPPZMQ_BUILD_TESTS) enable_testing() add_subdirectory(tests) + if (CMAKE_CXX_STANDARD AND NOT CMAKE_CXX_STANDARD EQUAL 98 AND CMAKE_CXX_STANDARD GREATER_EQUAL 11) + add_subdirectory(examples) + endif() endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..491f3d1 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.0 FATAL_ERROR) + +project(cppzmq-examples CXX) + +# place binaries and libraries according to GNU standards + +include(GNUInstallDirs) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}) + +find_package(Threads) + +add_executable( + pubsub_multithread_inproc + pubsub_multithread_inproc.cpp +) + +target_link_libraries( + pubsub_multithread_inproc + PRIVATE cppzmq + PRIVATE ${CMAKE_THREAD_LIBS_INIT} +) diff --git a/examples/pubsub_multithread_inproc.cpp b/examples/pubsub_multithread_inproc.cpp index be2b304..57027b9 100644 --- a/examples/pubsub_multithread_inproc.cpp +++ b/examples/pubsub_multithread_inproc.cpp @@ -40,9 +40,10 @@ void SubscriberThread1(zmq::context_t *ctx) { zmq::recv_result_t result = zmq::recv_multipart(subscriber, std::back_inserter(recv_msgs)); assert(result && "recv failed"); + assert(*result == 2); - std::cout << "Thread2: [" << recv_msgs[0].to_string_view() << "] " - << recv_msgs[1].to_string_view() << std::endl; + std::cout << "Thread2: [" << recv_msgs[0].to_string() << "] " + << recv_msgs[1].to_string() << std::endl; } } @@ -60,9 +61,10 @@ void SubscriberThread2(zmq::context_t *ctx) { zmq::recv_result_t result = zmq::recv_multipart(subscriber, std::back_inserter(recv_msgs)); assert(result && "recv failed"); + assert(*result == 2); - std::cout << "Thread3: [" << recv_msgs[0].to_string_view() << "] " - << recv_msgs[1].to_string_view() << std::endl; + std::cout << "Thread3: [" << recv_msgs[0].to_string() << "] " + << recv_msgs[1].to_string() << std::endl; } }