mirror of
https://github.com/zeromq/cppzmq.git
synced 2024-12-12 10:33:52 +01:00
Merge pull request #450 from gummif/gfa/build-examples
Problem: Examples are not compiled
This commit is contained in:
commit
05ff657b1a
@ -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()
|
||||
|
16
README.md
16
README.md
@ -53,7 +53,7 @@ int main()
|
||||
sock.send(zmq::str_buffer("Hello, world"), zmq::send_flags::dontwait);
|
||||
}
|
||||
```
|
||||
This a more complex example where we send and receive multi-part messages.
|
||||
This a more complex example where we send and receive multi-part messages over TCP with a wildcard port.
|
||||
```c++
|
||||
#include <iostream>
|
||||
#include <zmq_addon.hpp>
|
||||
@ -61,10 +61,14 @@ This a more complex example where we send and receive multi-part messages.
|
||||
int main()
|
||||
{
|
||||
zmq::context_t ctx;
|
||||
zmq::socket_t sock1(ctx, zmq::socket_type::pair);
|
||||
zmq::socket_t sock2(ctx, zmq::socket_type::pair);
|
||||
sock1.bind("inproc://test");
|
||||
sock2.connect("inproc://test");
|
||||
zmq::socket_t sock1(ctx, zmq::socket_type::push);
|
||||
zmq::socket_t sock2(ctx, zmq::socket_type::pull);
|
||||
sock1.bind("tcp://127.0.0.1:*");
|
||||
const std::string last_endpoint =
|
||||
sock1.get(zmq::sockopt::last_endpoint);
|
||||
std::cout << "Connecting to "
|
||||
<< last_endpoint << std::endl;
|
||||
sock2.connect(last_endpoint);
|
||||
|
||||
std::array<zmq::const_buffer, 2> send_msgs = {
|
||||
zmq::str_buffer("foo"),
|
||||
@ -84,6 +88,8 @@ int main()
|
||||
}
|
||||
```
|
||||
|
||||
See the `examples` directory for more examples. When the project is compiled with tests enabled, each example gets compiled to an executable.
|
||||
|
||||
Compatibility Guidelines
|
||||
========================
|
||||
|
||||
|
39
examples/CMakeLists.txt
Normal file
39
examples/CMakeLists.txt
Normal file
@ -0,0 +1,39 @@
|
||||
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 ${CMAKE_THREAD_LIBS_INIT}
|
||||
)
|
||||
|
||||
add_executable(
|
||||
hello_world
|
||||
hello_world.cpp
|
||||
)
|
||||
target_link_libraries(
|
||||
hello_world
|
||||
PRIVATE cppzmq ${CMAKE_THREAD_LIBS_INIT}
|
||||
)
|
||||
|
||||
add_executable(
|
||||
multipart_messages
|
||||
multipart_messages.cpp
|
||||
)
|
||||
target_link_libraries(
|
||||
multipart_messages
|
||||
PRIVATE cppzmq ${CMAKE_THREAD_LIBS_INIT}
|
||||
)
|
9
examples/hello_world.cpp
Normal file
9
examples/hello_world.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
#include <zmq.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
zmq::context_t ctx;
|
||||
zmq::socket_t sock(ctx, zmq::socket_type::push);
|
||||
sock.bind("inproc://test");
|
||||
sock.send(zmq::str_buffer("Hello, world"), zmq::send_flags::dontwait);
|
||||
}
|
31
examples/multipart_messages.cpp
Normal file
31
examples/multipart_messages.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
#include <zmq_addon.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
zmq::context_t ctx;
|
||||
zmq::socket_t sock1(ctx, zmq::socket_type::push);
|
||||
zmq::socket_t sock2(ctx, zmq::socket_type::pull);
|
||||
sock1.bind("tcp://127.0.0.1:*");
|
||||
const std::string last_endpoint =
|
||||
sock1.get(zmq::sockopt::last_endpoint);
|
||||
std::cout << "Connecting to "
|
||||
<< last_endpoint << std::endl;
|
||||
sock2.connect(last_endpoint);
|
||||
|
||||
std::array<zmq::const_buffer, 2> send_msgs = {
|
||||
zmq::str_buffer("foo"),
|
||||
zmq::str_buffer("bar!")
|
||||
};
|
||||
if (!zmq::send_multipart(sock1, send_msgs))
|
||||
return 1;
|
||||
|
||||
std::vector<zmq::message_t> recv_msgs;
|
||||
const auto ret = zmq::recv_multipart(
|
||||
sock2, std::back_inserter(recv_msgs));
|
||||
if (!ret)
|
||||
return 1;
|
||||
std::cout << "Got " << *ret
|
||||
<< " messages" << std::endl;
|
||||
return 0;
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user