如何在cmake中导入zeromq库? [英] How to import zeromq libraries in cmake?

查看:60
本文介绍了如何在cmake中导入zeromq库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm just starting to learn how to work with zeromq libraries and using them in different C++ projects. The sample code that I wrote (actually copied from there tutorials)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 by building the output of this file with the command below:

g++ main.cpp -o test -lzmq

文件将毫无问题地生成。

the file will be generated with no problem.

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

The problem that I have is that I want to build the file using CMake.Thus, I've written a CMakeLists.txt file as 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})

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

The part that I thought will be enough to build the program is -lzmq but even with that piece included, 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'

我应该在CMake文件中设置另一种变量还是应该将库安装在错误的目录中?还是我应该提到的其他内容?
和我正在使用的系统女巫是Ubuntu 14.04

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

推荐答案

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

CMake doesn't include direct support for 0mq and 0mq doesn't include direct support for CMake. But, 0mq does support pkg-config and we can use this to help us. Since you mentioned that you're on Ubuntu 14.04, make sure you've done sudo apt-get install libzmq3-dev to install the 0mq libraries, headers, and the pkg-config .pc file.

然后使用以下命令 CMakeLists.txt 我已经从您的上面的版本进行了修改。 (我已在线上注释了所有更改。)

Then use the following CMakeLists.txt which I've modified from your version above. (I've commented all my changes inline.)

## 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})

现在您可以制作您的项目。

❯ 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's happening under the hood, do a verbose 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 compile line, you'll notice that


  1. C ++ 11支持是添加(带有 -std = gnu ++ 11 标志)

  2. 没有 -I / path / to / zmq 标志。那是因为Ubuntu将 zmq.hpp 转储到 / usr / include 中,而CMake足够聪明,可以知道这是默认设置路径,所以它什么也没做

  1. C++11 support was added (with the -std=gnu++11 flag)
  2. There's no -I/path/to/zmq flag. That's because Ubuntu dumps zmq.hpp in to /usr/include and CMake is smart enough to know that is a default path, so it does nothing

如果查看链接行,您会注意到libzmq.so已通过它包含完整路径。有些人不喜欢它(包括我自己),而是更喜欢 -L / lib / dir -lmylib 。使用完整路径是 CMake方式,随着CMake的发展,您会逐渐体会到它的价值,并将其用于更大和更复杂的项目(尽管您仍然可能不喜欢它。)

If you look at the link line, you'll notice that libzmq.so has been included via it's full path. Some people don't like that (myself included) and prefer -L/lib/dir -lmylib instead. Using the full path is "the CMake way", and you'll actually come to appreciate it as you grow with CMake and use it for larger and more complicated projects (you still might not like it though.)

还请注意,由于您使用的是Ubuntu,我们可能会欺骗并使用 dpkg -L libzmq3-dev 来找到我们感兴趣的文件然后将它们添加到原始的 CMAKE_CXX_FLAGS 行中,但这很容易欺骗,更重要的是,它不是可移植的。

Also note, that since you're on Ubuntu we could have cheated and used dpkg -L libzmq3-dev to find the locations of the files we're interested in then added them to your original CMAKE_CXX_FLAGS line, but that's cheating and more importantly, not portable.

这篇关于如何在cmake中导入zeromq库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆