我正在尝试在C++程序中使用C库。我想将库libcoap.a链接到我的程序。我已经用Qt做了一个很大的程序,所以它不能用make代替qmake来解决。
作为一个简单的例子,我编写了以下代码。
connection.h:
#include <string>
#include "coap.h"
void sendData(std::string);connection.cpp:
#include "connection.h"
#include "coap.h"
coap_uri_t uri;
void sendData(std::string ip){
coap_split_uri((unsigned char *)ip.c_str(), strlen(ip.c_str()), &uri );
}main.cpp:
#include "connection.h"
using namespace std;
int main()
{
sendData("[aaaa::c30c:0:0:2]/sen/lt");
return 0;
} coaptester.pro:
TEMPLATE = app
CONFIG += console
CONFIG -= qt
unix:!macx:!symbian: LIBS += -L$$PWD/../commTest/commTest/libcoap-4.0.1/ -lcoap
INCLUDEPATH += $$PWD/../commTest/commTest/libcoap-4.0.1
DEPENDPATH += $$PWD/../commTest/commTest/libcoap-4.0.1
unix:!macx:!symbian: PRE_TARGETDEPS += $$PWD/../commTest/commTest/libcoap-4.0.1/libcoap.a
SOURCES += main.cpp \
connection.cpp
HEADERS += \
connection.h我总是得到相同的错误:
undefined reference to `coap_split_uri(unsigned char*, unsigned int, coap_uri_t*)'
collect2: ld returned 1 exit status提前谢谢。
发布于 2013-04-10 20:32:05
尝试使用(在connection.h中)
extern "C" {
#include "coap.h"
}此外,在connection.h和connection.cpp中都包含coap.h检查是多余的(如果在coap.h中没有包含检查,则可执行文件的维度将无用地更大),因此将其从.cpp中删除。
https://stackoverflow.com/questions/15925503
复制相似问题