首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在cmake中导入zeromq库?

如何在cmake中导入zeromq库?
EN

Stack Overflow用户
提问于 2016-12-20 21:22:57
回答 3查看 20.5K关注 0票数 16

我刚刚开始学习如何使用零q库并在不同的C++项目中使用它们。我编写的示例代码(实际上是从教程中复制的)如下:

代码语言:javascript
运行
复制
//  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;
}

通过使用以下命令构建该文件的输出:

代码语言:javascript
运行
复制
g++ main.cpp -o test -lzmq

文件将生成,没有任何问题。

我遇到的问题是,我想使用CMake.Thus构建文件,我编写了一个CMakeLists.txt文件如下所示:

代码语言:javascript
运行
复制
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})

我认为足以构建程序的部分是-lzmq,但即使包含了该部分,我也会得到以下错误消息:

代码语言:javascript
运行
复制
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'

是在CMake文件中设置另一种变量,还是在错误的目录中安装库?或者还有什么我应该提的?我正在研究的系统是Ubuntu14.04

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-12-20 22:48:40

CMake不包括对0mq的直接支持,0mq不包括对CMake的直接支持。但是,0mq确实支持pkg-config,我们可以使用它来帮助我们。既然您提到您在Ubuntu14.04上,请确保您已经完成了sudo apt-get install libzmq3-dev来安装0mq库、标头和pkg-config .pc文件。

然后使用下面的CMakeLists.txt,这是我从上面的版本中修改的。(我已经内联地评论了我的所有更改。)

代码语言:javascript
运行
复制
## 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 above 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})

现在您可以make您的项目。

代码语言:javascript
运行
复制
❯ make
[ 50%] Building CXX object CMakeFiles/test2.dir/main.cpp.o
[100%] Linking CXX executable test2
[100%] Built target test2

如果你想看看引擎盖下面发生了什么,做一个详细的制作。

代码语言:javascript
运行
复制
❯ 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

如果您查看编译行,您会注意到

  1. 添加了C++11支持(带有-std=gnu++11标志)
  2. 没有-I/path/to/zmq标志。这是因为Ubuntu将zmq.hpp转储到/usr/include,而CMake聪明到知道这是一条默认路径,所以它什么也不做

如果您查看链接行,您会注意到libzmq.so是通过它的完整路径被包括在内的。有些人不喜欢这个(包括我自己),而更喜欢-L/lib/dir -lmylib。使用完整的路径就是“CMake方式”,当您使用CMake并将其用于更大和更复杂的项目时,您实际上会欣赏到它(您可能仍然不喜欢它)。

还请注意,由于您在Ubuntu上,我们可以使用dpkg -L libzmq3-dev查找我们感兴趣的文件的位置,然后将它们添加到原始的CMAKE_CXX_FLAGS行中,但这是欺骗,更重要的是,不可移植。

票数 30
EN

Stack Overflow用户

发布于 2017-01-31 04:03:36

一个cmake配置文件已经添加到libzmq存储库(2017年1月7日这里 )中。

这还没有包含在最新的版本(4.2.1)中,但是我相信它应该在下一个版本中。

我使用cmake安装了head版本,然后安装了卡普兹姆,它使用find_package(ZeroMQ REQUIRED)定位libzmq。都很有魅力。

编辑: config文件包含在4.2.2版本的这里中。然后,它被移动到builds/cmake目录下的4.2.4发布版。我没有再次测试它,但是find_package(ZeroMQ REQUIRED)应该可以工作,因为ibzmq4.2.2。

票数 12
EN

Stack Overflow用户

发布于 2018-10-06 18:09:36

这也同样有效。

代码语言:javascript
运行
复制
 cmake_minimum_required(VERSION 3.6)
 project(test)     
 add_executable(c main.cpp)
 target_link_libraries(c zmq)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41251474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档