### CMake Build Configuration for cppzmq Examples Source: https://github.com/zeromq/cppzmq/blob/master/examples/CMakeLists.txt This CMake script sets up the build environment for cppzmq examples. It defines project details, installation directories according to GNU standards, and finds the necessary cppzmq and Threads libraries. It then configures multiple executables, linking them with cppzmq and thread libraries. ```cmake cmake_minimum_required(VERSION 3.11 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) find_package(cppzmq) 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} ) ``` -------------------------------- ### CMake Test and Example Subdirectory Configuration Source: https://github.com/zeromq/cppzmq/blob/master/CMakeLists.txt Configures whether to build tests and examples based on the 'CPPZMQ_BUILD_TESTS' option. If enabled, it adds the 'tests' subdirectory and, if the C++ standard is C++11 or higher, also adds the 'examples' subdirectory. This is managed via CMake. ```cmake 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() ``` -------------------------------- ### Build cppzmq with vcpkg Source: https://github.com/zeromq/cppzmq/blob/master/README.md Instructions for building and installing cppzmq using the vcpkg package manager. This includes bootstrapping vcpkg, integrating it with the system, and then installing the cppzmq package. ```bash git clone https://github.com/Microsoft/vcpkg.git cd vcpkg ./bootstrap-vcpkg.sh # or ./bootstrap-vcpkg.bat for Powershell ./vcpkg integrate install ./vcpkg install cppzmq ``` -------------------------------- ### CMake Package Configuration and Installation Source: https://github.com/zeromq/cppzmq/blob/master/CMakeLists.txt Handles the installation of the cppzmq library, headers, and CMake configuration files. It configures package files like `cppzmqConfig.cmake` and `cppzmqConfigVersion.cmake`, and also installs the `FindZeroMQ.cmake` module. This is all done using CMake scripting. ```cmake include(GNUInstallDirs) include(CMakePackageConfigHelpers) install(TARGETS cppzmq cppzmq-static EXPORT ${PROJECT_NAME}-targets) install (FILES ${CPPZMQ_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) # GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share". set(CPPZMQ_CMAKECONFIG_INSTALL_DIR "share/cmake/${PROJECT_NAME}" CACHE STRING "install path for cppzmqConfig.cmake") configure_file(libzmq-pkg-config/FindZeroMQ.cmake libzmq-pkg-config/FindZeroMQ.cmake COPYONLY) export(EXPORT ${PROJECT_NAME}-targets FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake") configure_package_config_file(${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION ${CPPZMQ_CMAKECONFIG_INSTALL_DIR}) # Workaround until ARCH_INDEPENDENT flag can be used with cmake 3.14. # The ConigVersion.cmake file contains checks for the architecture is was # generated on, which can cause problems for header only libraries # used with e.g. the Conan package manager. Since it is header only we # can/should omit those checks. set(CPPZMQ_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P}) set(CMAKE_SIZEOF_VOID_P "") # a simple unset is not sufficient write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake VERSION ${CPPZMQ_VERSION} COMPATIBILITY AnyNewerVersion) set(CMAKE_SIZEOF_VOID_P ${CPPZMQ_SIZEOF_VOID_P}) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cppzmq.pc.in ${CMAKE_CURRENT_BINARY_DIR}/cppzmq.pc @ONLY) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cppzmq.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) install(EXPORT ${PROJECT_NAME}-targets FILE ${PROJECT_NAME}Targets.cmake DESTINATION ${CPPZMQ_CMAKECONFIG_INSTALL_DIR}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake DESTINATION ${CPPZMQ_CMAKECONFIG_INSTALL_DIR}) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/libzmq-pkg-config/FindZeroMQ.cmake DESTINATION ${CPPZMQ_CMAKECONFIG_INSTALL_DIR}/libzmq-pkg-config) ``` -------------------------------- ### C++ Example: Sending and Receiving Multi-part Messages with TCP Source: https://github.com/zeromq/cppzmq/blob/master/README.md This C++ example showcases sending and receiving multi-part messages over TCP using cppzmq. It involves creating two sockets (push and pull), binding one, connecting the other using the last endpoint, and utilizing `zmq::send_multipart` and `zmq::recv_multipart`. This example requires zmq_addon.hpp for multipart functions. ```cpp #include #include 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 send_msgs = { zmq::str_buffer("foo"), zmq::str_buffer("bar!") }; if (!zmq::send_multipart(sock1, send_msgs)) return 1; std::vector 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; } ``` -------------------------------- ### Build cppzmq with CMake Source: https://github.com/zeromq/cppzmq/blob/master/README.md Instructions for building the cppzmq library using CMake. This process involves cloning the repository, creating a build directory, configuring with CMake (optionally disabling tests), and installing. ```bash git clone https://github.com/zeromq/cppzmq.git cd cppzmq mkdir build cd build cmake .. # or cmake -DCPPZMQ_BUILD_TESTS=OFF .. sudo make -j4 install ``` -------------------------------- ### CMake Project Setup and ZeroMQ Detection Source: https://github.com/zeromq/cppzmq/blob/master/CMakeLists.txt Configures the CMake minimum version, module path, and project version. It includes logic to find the ZeroMQ library, first as a CMake package and then falling back to pkg-config if necessary. If ZeroMQ is not found, the build fails. This snippet is written in CMake. ```cmake cmake_minimum_required(VERSION 3.11) list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) include (DetectCPPZMQVersion) project(cppzmq VERSION ${DETECTED_CPPZMQ_VERSION}) if (NOT TARGET libzmq AND NOT TARGET libzmq-static) find_package(ZeroMQ QUIET) # libzmq autotools install: fallback to pkg-config if(NOT ZeroMQ_FOUND) message(STATUS "CMake libzmq package not found, trying again with pkg-config (normal install of zeromq)") list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/libzmq-pkg-config) find_package(ZeroMQ REQUIRED) endif() # TODO "REQUIRED" above should already cause a fatal failure if not found, but this doesn't seem to work if(NOT ZeroMQ_FOUND) message(FATAL_ERROR "ZeroMQ was not found, neither as a CMake package nor via pkg-config") endif() if (ZeroMQ_FOUND AND NOT (TARGET libzmq OR TARGET libzmq-static)) message(FATAL_ERROR "ZeroMQ version not supported!") endif() endif() message(STATUS "cppzmq v${cppzmq_VERSION}") ``` -------------------------------- ### Build libzmq with CMake Source: https://github.com/zeromq/cppzmq/blob/master/README.md Instructions for building the core libzmq library using CMake. This is a prerequisite for building cppzmq. It involves cloning the libzmq repository, creating a build directory, configuring with CMake, and installing. ```bash git clone https://github.com/zeromq/libzmq.git cd libzmq mkdir build cd build cmake .. sudo make -j4 install ``` -------------------------------- ### Integrate cppzmq with CMake Source: https://github.com/zeromq/cppzmq/blob/master/README.md Example CMakeLists.txt configuration to find and link the cppzmq library (and its dependency libzmq) in your project. It demonstrates how to use find_package and target_link_libraries for both shared and static versions of the library. ```cmake #find cppzmq wrapper, installed by make of cppzmq find_package(cppzmq) target_link_libraries(*Your Project Name* cppzmq) # Or use static library to link target_link_libraries(*Your Project Name* cppzmq-static) ``` -------------------------------- ### CMake Interface Library Definition and Installation Source: https://github.com/zeromq/cppzmq/blob/master/CMakeLists.txt Defines interface libraries for 'cppzmq' and 'cppzmq-static', specifying include directories for build and installation. It then links the ZeroMQ library to these targets. This configuration is managed using CMake. ```cmake set(CPPZMQ_HEADERS zmq.hpp zmq_addon.hpp ) foreach (target cppzmq cppzmq-static) add_library(${target} INTERFACE) target_include_directories(${target} INTERFACE $ $) endforeach() target_link_libraries(cppzmq INTERFACE libzmq) target_link_libraries(cppzmq-static INTERFACE libzmq-static) ``` -------------------------------- ### Basic ZeroMQ Push Socket Send Example in C++ Source: https://github.com/zeromq/cppzmq/blob/master/README.md This C++ code snippet demonstrates the fundamental usage of cppzmq to create a ZeroMQ context, a push socket, bind it to an in-process address, and send a simple message. It requires the zmq.hpp header and uses RAII for resource management. ```cpp #include 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); } ``` -------------------------------- ### Configure Catch2 Test Framework Source: https://github.com/zeromq/cppzmq/blob/master/tests/CMakeLists.txt This snippet configures the Catch2 testing framework for the project. It includes fetching Catch2 if not found and making it available for use in tests. Dependencies include Catch2 itself. ```cmake find_package(Catch2 QUIET) if (NOT Catch2_FOUND) include(FetchContent) FetchContent_Declare( Catch2 GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_TAG v3.5.3) FetchContent_MakeAvailable(Catch2) list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/contrib) endif() ``` -------------------------------- ### Define Unit Test Executable and Link Libraries Source: https://github.com/zeromq/cppzmq/blob/master/tests/CMakeLists.txt This snippet defines the 'unit_tests' executable, lists its source files, and links necessary libraries. It depends on Catch2, cppzmq, and pthreads. Output is the compiled test executable. ```cmake add_executable( unit_tests buffer.cpp message.cpp context.cpp socket.cpp socket_ref.cpp poller.cpp active_poller.cpp multipart.cpp recv_multipart.cpp send_multipart.cpp codec_multipart.cpp monitor.cpp utilities.cpp timers.cpp ) target_include_directories(unit_tests PUBLIC ${CATCH_MODULE_PATH}) target_link_libraries( unit_tests PRIVATE Catch2::Catch2WithMain PRIVATE cppzmq PRIVATE ${CMAKE_THREAD_LIBS_INIT} ) ``` -------------------------------- ### Discover and Run Catch2 Tests Source: https://github.com/zeromq/cppzmq/blob/master/tests/CMakeLists.txt This snippet integrates with CTest to discover and run tests written using the Catch2 framework. It includes necessary CMake modules for CTest and Catch2 integration. The output is the execution of discovered tests. ```cmake include(CTest) include(Catch) catch_discover_tests(unit_tests) ``` -------------------------------- ### Enable Code Coverage for Unit Tests Source: https://github.com/zeromq/cppzmq/blob/master/tests/CMakeLists.txt This snippet enables code coverage for the unit tests. When the COVERAGE option is set to ON, it adds '--coverage' flags to compile and link options for the 'unit_tests' target. This is useful for analyzing test coverage with tools like lcov. ```cmake OPTION (COVERAGE "Enable gcda file generation needed by lcov" OFF) if (COVERAGE) target_compile_options(unit_tests PRIVATE --coverage) target_link_options(unit_tests PRIVATE --coverage) message(STATUS "Coverage enabled") endif() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.