我想在C++項目中使用zmq。 當我想構建一個簡單的示例時,我非常困惑。鏈接zmq C++頭時出錯
爲此,我已經爲VisualC++ 2010分別使用.sln構建了libzmq v3.2.3。然後我下載了zmq.hpp並將其包含在內。 的例子是ZMQ指南的第一個:
//
// Hello World server in C++
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <unistd.h>
int main() {
// Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REP);
socket.bind ("tcp://*:5555");
while (true) {
zmq::message_t request;
// Wait for next request from client
socket.recv (&request);
std::cout << "Received Hello" << std::endl;
// Do some 'work'
sleep (1);
// Send reply back to client
zmq::message_t reply (5);
memcpy ((void *) reply.data(), "World", 5);
socket.send (reply);
}
return 0;
}
編譯是好的,但連接不是:我得到這樣一個錯誤:
error LNK2019: external symbol unresolved __imp__zmq_term refrenced in function "public: void __thiscall zmq::context_t::close(void)" ([email protected][email protected]@@QAEXXZ)
路徑ZMQ似乎是好;我只是不明白爲什麼VC++無法找到zmq.hpp裏面的小方法的代碼
你看到我忘記了什麼嗎?
編輯: 這裏是輸出窗口的內容(對不起,它的法語版本,但我認爲這是非常相似):
1>------ Début de la génération : Projet : libzmq, Configuration : Debug Win32 ------
1> 1 fichier(s) copié(s).
1> libzmq.vcxproj -> <...>\..\Ouptut\Debug\libzmq_d.lib
2>------ Début de la génération : Projet : HelloServeur, Configuration : Debug Win32 ------
2> HelloServeur.cpp
2>HelloServeur.obj : error LNK2019: symbole externe non résolu __imp__zmq_init référencé dans la fonction "public: __thiscall zmq::context_t::context_t(int)" ([email protected]@@[email protected]@Z)
2>HelloServeur.obj : error LNK2019: symbole externe non résolu __imp__zmq_errno référencé dans la fonction "public: __thiscall zmq::error_t::error_t(void)" ([email protected]@@[email protected])
2>HelloServeur.obj : error LNK2019: symbole externe non résolu __imp__zmq_strerror référencé dans la fonction "public: virtual char const * __thiscall zmq::error_t::what(void)const " ([email protected][email protected]@@UBEPBDXZ)
2>HelloServeur.obj : error LNK2019: symbole externe non résolu __imp__zmq_term référencé dans la fonction "public: void __thiscall zmq::context_t::close(void)" ([email protected][email protected]@@QAEXXZ)
2><...>\..\Ouptut\Debug\HelloServeur.exe : fatal error LNK1120: 4 externes non résolus
========== Génération : 1 a réussi, 1 a échoué, 0 mis à jour, 0 a été ignoré ==========
頭文件不鏈接,庫鏈接。您通常需要包含一個頭文件,以便編譯器理解您的代碼與頭文件所代表的庫相關,但是您仍然必須將該庫包含在鏈接中。 – mah
對不起,我的壞。鏈接main()的代碼時,表示鏈接錯誤。 lib在鏈接過程中被正確添加,但所有鏈接錯誤都是關於包裝內部的方法zmq.hpp – Bentoy13
您收到的錯誤直接與您認爲「lib已正確添加」的信念相沖突;)也許發佈實際的鏈接命令MSVC正在使用(即,從構建日誌複製它粘貼在這裏)。 – mah