2016-12-20 52 views
5

我剛開始學習如何使用zeromq庫並在不同的C++項目中使用它們。 示例代碼,我寫(實際上從那裏教程複製)是這樣的:如何導入cmake中的zeromq庫?

// 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; 
} 

並通過建立該文件的輸出與下面的命令:

g++ main.cpp -o test -lzmq 

該文件將被與所生成的沒問題。

,我有是我想建立使用CMake.Thus文件的問題,我寫了一個CMakeLists.txt文件,如下:

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但即使包括那塊,我收到以下錯誤消息:

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

回答

12

CMake的不包括0mq和0mq直接支持不包括對CMake的直接支持。但是,0mq確實支持pkg-config,我們可以使用它來幫助我們。既然您提到您使用的是Ubuntu 14.04,請確保您已完成sudo apt-get install libzmq3-dev以安裝0mq庫,頭文件和pkg-config.pc文件。

然後使用以下我從上述版本修改過的CMakeLists.txt。 (我內嵌評論我的所有更改。)

## 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項目。

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

如果你想看看有什麼引擎蓋下發生,做一個詳細的化妝。

❯ 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的中/usr/include轉儲zmq.hpp和CMake的是足夠聰明,知道這是一個默認的路徑,所以它什麼都不做

如果你看一下連接線,你會發現,libzmq.so已被列入通過它的完整路徑。有些人不喜歡(包括我自己),而更喜歡-L/lib/dir -lmylib。使用完整的路徑是「CMake的方式」,隨着您與CMake一起成長,您將會體會到它,並將其用於更大和更復雜的項目(但您仍然可能不喜歡它。)

還要注意,因爲你是在Ubuntu上我們可以欺騙和使用dpkg -L libzmq3-dev找到我們感興趣的,然後將它們添加到您的原CMAKE_CXX_FLAGS行的文件的位置,但是這欺騙,更重要的是,不便攜式。

+0

我在Mac OS X和Ubuntu上遇到了一個錯誤,直到我使用'sudo apt install libzmq5' [Ubuntu]或'brew install zmq' [OS X] –

3

一個cmake配置文件已被添加到libzmq github庫最近here

這還不包括在最新版本(4.2.1)中,但我相信它應該在下一個版本中。

我已經使用cmake安裝了頭版,然後安裝了cppzmq,它使用find_package(ZeroMQ REQUIRED)來定位libzmq。所有人都像魅力一樣工作。

+0

安裝了最新版本的ZeroMQ。確實,這是如果你有能力使用HEAD或即將發佈的版本,那就去吧。如果你使用系統軟件包被困住了,那麼我以前的答案就是要走的路。它也可以外推到任何具有pkg-config支持但不支持cmake配置的庫。 – nega