How to import zeromq libraries in cmake?

I'm just starting to learn how to work with zeromq libraries and use them in different C ++ projects. The sample code I wrote (actually copied from this tutorial) is this:

// file: main.cpp // Hello World client in C++ // Connects REQ socket to tcp://localhost:5555 // Sends "Hello" to server, expects "World" back // #include <zmq.hpp> #include <string> #include <iostream> int main () { // Prepare our context and socket zmq::context_t context (1); zmq::socket_t socket (context, ZMQ_REQ); std::cout << "Connecting to hello world server…" << std::endl; socket.connect ("tcp://localhost:5555"); // Do 10 requests, waiting each time for a response for (int request_nbr = 0; request_nbr != 10; request_nbr++) { zmq::message_t request (5); memcpy (request.data (), "Hello", 5); std::cout << "Sending Hello " << request_nbr << "…" << std::endl; socket.send (request); // Get the reply. zmq::message_t reply; socket.recv (&reply); std::cout << "Received World " << request_nbr << std::endl; } return 0; } 

and creating the output of this file with the following command:

 g++ main.cpp -o test -lzmq 

The file will be generated without problems.

The problem I have is that I want to create a file using CMake. So I wrote a CMakeLists.txt file as shown below:

 cmake_minimum_required(VERSION 3.6) project(test2) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lzmq") set(SOURCE_FILES main.cpp) add_executable(test2 ${SOURCE_FILES}) 

The part that I thought would be sufficient to create the program is -lzmq , but even with the part turned on, I get the following error messages:

 CMakeFiles/test2.dir/main.cpp.o: In function `zmq::error_t::error_t()': /usr/include/zmq.hpp:62: undefined reference to `zmq_errno' CMakeFiles/test2.dir/main.cpp.o: In function `zmq::error_t::what() const': /usr/include/zmq.hpp:66: undefined reference to `zmq_strerror' CMakeFiles/test2.dir/main.cpp.o: In function `zmq::message_t::message_t()': /usr/include/zmq.hpp:107: undefined reference to `zmq_msg_init' CMakeFiles/test2.dir/main.cpp.o: In function `zmq::message_t::message_t(unsigned long)': /usr/include/zmq.hpp:114: undefined reference to `zmq_msg_init_size' CMakeFiles/test2.dir/main.cpp.o: In function `zmq::message_t::~message_t()': /usr/include/zmq.hpp:129: undefined reference to `zmq_msg_close' CMakeFiles/test2.dir/main.cpp.o: In function `zmq::message_t::data()': /usr/include/zmq.hpp:180: undefined reference to `zmq_msg_data' CMakeFiles/test2.dir/main.cpp.o: In function `zmq::context_t::context_t(int)': /usr/include/zmq.hpp:204: undefined reference to `zmq_init' CMakeFiles/test2.dir/main.cpp.o: In function `zmq::context_t::~context_t()': /usr/include/zmq.hpp:225: undefined reference to `zmq_term' CMakeFiles/test2.dir/main.cpp.o: In function `zmq::socket_t::socket_t(zmq::context_t&, int)': /usr/include/zmq.hpp:251: undefined reference to `zmq_socket' CMakeFiles/test2.dir/main.cpp.o: In function `zmq::socket_t::close()': /usr/include/zmq.hpp:283: undefined reference to `zmq_close' CMakeFiles/test2.dir/main.cpp.o: In function `zmq::socket_t::connect(char const*)': /usr/include/zmq.hpp:314: undefined reference to `zmq_connect' CMakeFiles/test2.dir/main.cpp.o: In function `zmq::socket_t::send(zmq::message_t&, int)': /usr/include/zmq.hpp:321: undefined reference to `zmq_send' /usr/include/zmq.hpp:324: undefined reference to `zmq_errno' CMakeFiles/test2.dir/main.cpp.o: In function `zmq::socket_t::recv(zmq::message_t*, int)': /usr/include/zmq.hpp:331: undefined reference to `zmq_recv' /usr/include/zmq.hpp:334: undefined reference to `zmq_errno' 

Should I set another variable in the CMake file or set the library in the wrong directory? or is there anything else I should have mentioned? and the system witch I'm working on is Ubuntu 14.04

+8
source share
3 answers

CMake does not include direct support for 0mq, and 0mq does not include direct support for CMake. But 0mq supports pkg-config , and we can use this to help us. Since you mentioned that you are on Ubuntu 14.04, make sure you do sudo apt-get install libzmq3-dev to install the 0mq libraries, headers, and the pkg-config .pc .

Then use the following CMakeLists.txt , which I changed from your version above. (I commented on all my inline changes.)

 ## i have cmake 3.5 cmake_minimum_required(VERSION 3.5) project(test2) ## use this to globally use C++11 with in our project set(CMAKE_CXX_STANDARD 11) ## load in pkg-config support find_package(PkgConfig) ## use pkg-config to get hints for 0mq locations pkg_check_modules(PC_ZeroMQ QUIET zmq) ## use the hint from above to find where 'zmq.hpp' is located find_path(ZeroMQ_INCLUDE_DIR NAMES zmq.hpp PATHS ${PC_ZeroMQ_INCLUDE_DIRS} ) ## use the hint from about to find the location of libzmq find_library(ZeroMQ_LIBRARY NAMES zmq PATHS ${PC_ZeroMQ_LIBRARY_DIRS} ) set(SOURCE_FILES main.cpp) add_executable(test2 ${SOURCE_FILES}) ## add the include directory to our compile directives target_include_directories(test2 PUBLIC ${ZeroMQ_INCLUDE_DIR}) ## at the 0mq library to our link directive target_link_libraries(test2 PUBLIC ${ZeroMQ_LIBRARY}) 

Now you can make complete your project.

 ❯ make [ 50%] Building CXX object CMakeFiles/test2.dir/main.cpp.o [100%] Linking CXX executable test2 [100%] Built target test2 

If you want to see what happens under the hood, do a detailed make.

 ❯ make VERBOSE=1 /usr/bin/cmake -H/home/nega/foo -B/home/nega/foo/build --check-build-system CMakeFiles/Makefile.cmake 0 /usr/bin/cmake -E cmake_progress_start /home/nega/foo/build/CMakeFiles /home/nega/foo/build/CMakeFiles/progress.marks make -f CMakeFiles/Makefile2 all make[1]: Entering directory '/home/nega/foo/build' make -f CMakeFiles/test2.dir/build.make CMakeFiles/test2.dir/depend make[2]: Entering directory '/home/nega/foo/build' cd /home/nega/foo/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/nega/foo /home/nega/foo /home/nega/foo/build /home/nega/foo/build /home/nega/foo/build/CMakeFiles/test2.dir/DependInfo.cmake --color= make[2]: Leaving directory '/home/nega/foo/build' make -f CMakeFiles/test2.dir/build.make CMakeFiles/test2.dir/build make[2]: Entering directory '/home/nega/foo/build' [ 50%] Building CXX object CMakeFiles/test2.dir/main.cpp.o /usr/bin/c++ -std=gnu++11 -o CMakeFiles/test2.dir/main.cpp.o -c /home/nega/foo/main.cpp [100%] Linking CXX executable test2 /usr/bin/cmake -E cmake_link_script CMakeFiles/test2.dir/link.txt --verbose=1 /usr/bin/c++ CMakeFiles/test2.dir/main.cpp.o -o test2 /usr/lib/x86_64-linux-gnu/libzmq.so make[2]: Leaving directory '/home/nega/foo/build' [100%] Built target test2 make[1]: Leaving directory '/home/nega/foo/build' /usr/bin/cmake -E cmake_progress_start /home/nega/foo/build/CMakeFiles 0 

If you look at the compilation line, you will notice that

  • Added support for C ++ 11 (with the flag -std=gnu++11 )
  • There is no flag -I/path/to/zmq . This is because Ubuntu resets zmq.hpp to /usr/include and CMake is smart enough to know that this is the default path, so it does nothing

If you look at the link line, you will notice that libzmq.so has been enabled through the full path. Some people don't like this (including me) and prefer -L/lib/dir -lmylib . Using the full path is the “CMake path”, and you really appreciate it when you grow with CMake and use it for larger and more complex projects (you might not like it anyway).

Also note that since you are on Ubuntu, we could trick and use dpkg -L libzmq3-dev to find the locations of the files we are interested in, and then add them to the original CMAKE_CXX_FLAGS line, but this is a trick and much more, and do not tolerate.

+17
source

The cmake configuration file was added to the libzmq github repository on January 7, 2017 here .

This is not yet included in the latest release (4.2.1), but I believe that it should be in the next release.

I installed the head version using cmake and then installed cppzmq which uses find_package(ZeroMQ REQUIRED) to find libzmq. Everything worked like a charm.

EDIT: cmake configuration file is included in release 4.2.2 here . Then it was moved to the builds/cmake directory in release 4.2.4 . I did not check this again, but find_package(ZeroMQ REQUIRED) should just work with ibzmq 4.2.2.

+7
source

It also works

  cmake_minimum_required(VERSION 3.6) project(test) add_executable(c main.cpp) target_link_libraries(c zmq) 
0
source

Source: https://habr.com/ru/post/1261601/


All Articles