0

When I try to run this simple program (https://zeromq.org/get-started/?language=cpp&library=zmqpp#)

#include <zmqpp/zmqpp.hpp>
#include <string>
#include <iostream>
#include <chrono>
#include <thread>

using namespace std;

int main(int argc, char *argv[]) {
  const string endpoint = "tcp://*:5555";

  // initialize the 0MQ context
  zmqpp::context context;

  // generate a pull socket
  zmqpp::socket_type type = zmqpp::socket_type::reply;
  zmqpp::socket socket (context, type);

  // bind to the socket
  socket.bind(endpoint);
  while (1) {
    // receive the message
    zmqpp::message message;
    // decompose the message 
    socket.receive(message);
    string text;
    message >> text;

    //Do some 'work'
    std::this_thread::sleep_for(std::chrono::seconds(1));
    cout << "Received Hello" << endl;
    socket.send("World");
  }
}

I get this error:

master/pubSub/pub.cpp:3:10: fatal error: 'zmqpp/zmqpp.hpp' file not found
#include <zmqpp/zmqpp.hpp>

Error Message

I also get this as well:

Uncaught TypeError: Callback must be a function. Received undefined

fs.js:135

Error Message 2

Obviously, this seems to emerge from the fact that I have not installed ZMQ Correctly. I tried to install it two ways:

Failed Method #1:

  1. Installed Homebrew (https://brew.sh/)
  2. Then in terminal: brew install zmq

Failed Method #2

  1. Downloaded cppzmq zip file (https://github.com/zeromq/cppzmq). Unzipped this file titled cppzmq-master
  2. Downloaded Cmake
  3. Installed Cmake in terminal: brew install cmake
  4. In terminal, changed directory to cppzmq-master
  5. mkdir build
  6. cd build
  7. cmake ..
  8. sudo make -j4 install

Not sure where I went wrong; still new to this, so feedback is much appreciated!

ZMQ Website for Reference: https://zeromq.org/

Sources

MrFreeze2017
  • 79
  • 1
  • 7
  • 1
    Did you `find_package(ZeroMQ REQUIRED)` in your cmake file? And then `target_link_libraries` to add a dependency to it? – Cory Kramer Sep 21 '21 at 15:35
  • @Cory Kramer I did not. Forgive me for my inexperience, but which Cmake file would I need to go to for this? The Cmake Targets file? Or something else? – MrFreeze2017 Sep 21 '21 at 15:39
  • Yes you create a `CMakeLists.txt` file at the root of your code directory, this specifies your source files, etc. How are you building now, visual studio, gcc, makefiles, etc? – Cory Kramer Sep 21 '21 at 15:41
  • It would probably be the CMakeLists.txt file you have in the root folder of the code for your application. if you are not using CMake to generate a project file or makefile for your application you will have to explain how you are building. You may need to setup the include directory for your compiler and add libraries to the linker settings manually. – drescherjm Sep 21 '21 at 15:42
  • @CoryKramer I am building using Atom and an add-on that lets me compile and run out of the terminal using g++ automatically by right clicking and selecting "Compile and Run." I went to the CMakeLists.txt file and I think (I may be wrong) that the lines you mentioned earlier are in there. Any suggestions from here? – MrFreeze2017 Sep 21 '21 at 15:47
  • You probably should look at the documentation on how to use a third party library with Atom. Your problem is likely not related to the installation of this libraray. I suspect that the g++ command generated does not set the include directory or link to the proper libraries. – drescherjm Sep 21 '21 at 16:52
  • @drescherjm Thank you; I will do that and let you know if I am able to solve it. – MrFreeze2017 Sep 21 '21 at 17:45

1 Answers1

0

One problem with your question is that you're asking how to instal a library but give no hint as to where you want to install it. Windows? Linux? MacOS? *BSD? Which version? If Linux, what package manager system does it use?

So I'll tell you how I came to be able to compile and run your program

  1. Open a GUI package installer and search for "zmq". I found and installed zmqpp, consistent with the name of your include file. I also have several related packages, most likely installed as dependencies enter image description here

    I'm not sure if all of them are actually necessary. By the way, I use Manjaro Linux with Arch-based packaging system.

  2. (optional step) Check if the header file you include is in the expected location

    > find /usr/include -name "zmqpp.hpp"
    /usr/include/zmqpp/zmqpp.hpp
    

    Yes, it is

  3. (optional) Chcek if binaries are located where they should be

    > find /usr/lib -name "libzmq*.*"
    /usr/lib/libzmqpp.so.4
    /usr/lib/libzmq.a
    /usr/lib/libzmqpp.so
    /usr/lib/libzmq.so.5.2.4
    /usr/lib/libzmqpp.so.4.1.2
    /usr/lib/libzmq.so
    /usr/lib/libzmq.so.5
    

    Only *.so and *.a are relevant. I can see the library is divided into two parts, libmzq and libzmqpp. I guess the former is for C bindings, while the latter - for C++ ones.

  4. Try to compile your program from the command line:

    > g++ main.cpp -lzmq -lzmqpp
    

    There's no error, the compiler has found & resolved all dependencies.

  5. If you want a Cmake script (CMakeLists.txt) that will do the same trick, here it is:

    cmake_minimum_required(VERSION 3.10) # earlier versions are also likely to work 
    project(sandbox VERSION 1.0 DESCRIPTION "sandbox" LANGUAGES CXX)
    set(CMAKE_CXX_STANDARD 20)  # optional
    add_executable(sandbox main.cpp)
    target_link_libraries(sandbox PUBLIC zmq zmqpp)
    
zkoza
  • 2,644
  • 3
  • 16
  • 24