使用Eclipse Paho MQTT编译Visual C ++ Win32项目时出现LNK2019错误 [英] LNK2019 error when compiling a Visual C++ Win32 project with Eclipse Paho MQTT
问题描述
我正在尝试构建一个使用Eclipse Paho MQTT库的Visual C ++ Win32项目-请参见 https ://eclipse.org/paho/clients/c/ 该库采用2个文件的形式:
I'm trying to build a Visual C++ Win32 project which uses the Eclipse Paho MQTT library - see https://eclipse.org/paho/clients/c/ The library takes the form of 2 files:
- paho-mqtt3c.lib
- paho-mqtt3c.dll
该库最初是用C编写的-我已经尝试过使用预编译的二进制文件并在自己编译时使用.
The library is originally written in C - I've tried with the precompiled binary and when compiling it myself.
在我的客户端Visual C ++项目中,在链接器选项下,我在其他库目录"字段中包含了正确的库路径,在其他依赖项"字段中直接包含了paho-mqtt3c.lib文件.但是,当我尝试构建时,链接器会因使用的每个库函数而出错.所有错误都具有相似的格式:
In my client Visual C++ project, under the linker options, I've included the correct library path in the "Additional Library Directories" field and the paho-mqtt3c.lib file directly in the "Additional Dependencies" field. However when I try to build, the linker bins out with an error for each library function used. All errors share a similar format:
错误2错误LNK2019:未解析的外部符号"int __cdecl MQTTClient_connect(void *,struct MQTTClient_connectOptions *)"(?MQTTClient_connect @@ YAHPAXPAUMQTTClient_connectOptions @@@ Z)在函数_main c:\ Project \ scrapbook \ MQTT_Example \ MQTT_Example中引用main.obj MQTT_Example
Error 2 error LNK2019: unresolved external symbol "int __cdecl MQTTClient_connect(void *,struct MQTTClient_connectOptions *)" (?MQTTClient_connect@@YAHPAXPAUMQTTClient_connectOptions@@@Z) referenced in function _main c:\Project\scrapbook\MQTT_Example\MQTT_Example\main.obj MQTT_Example
我的代码如下:
#include <MQTTClient.h>
#include <MQTTClientPersistence.h>
: : :
// Start MQTT connection
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
// Convert CString to char*
int connectResponse;
char* szBuf = strBroker.GetBuffer(strBroker.GetLength()) ;
// Create client
MQTTClient_create(&client, szBuf, "MQTTTestClient", MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
MQTTClient_setCallbacks(client, NULL, NULL, msgarrvd, NULL);
// Connect to broker
if ((connectResponse = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
// Oops.. that didn't work!
}
有人对如何解决这个问题有任何想法吗? (我知道有数以百万计的LNK2019问题,但到目前为止我找不到能解决我问题的任何东西...)
Does anyone have any ideas about how to solve this? (I know there are millions of LNK2019 questions but I cannot find anything that has solved my problem so far...)
推荐答案
所以事实证明,问题是我试图在C ++项目中包括一个C库(具有许多C风格声明的函数).
So it turns out the problem was that I was attempting to include a C library (with lots of C style declared functions) in a C++ project.
解决方案是将函数声明(即包含头文件的位置)包装在extern"C"块中:
The solution is to wrap the function declarations (i.e. where the header files are included) in an extern "C" block:
extern "C"{
#include <MQTTClient.h>
#include <MQTTClientPersistence.h>
}
这告诉链接器链接C而不是C ++函数.
This tells the linker to link against C rather than C++ functions.
这篇关于使用Eclipse Paho MQTT编译Visual C ++ Win32项目时出现LNK2019错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!