gpt4 book ai didi

c++ - Qt C++ 中对库方法的 undefined reference

转载 作者:太空宇宙 更新时间:2023-11-04 02:57:22 25 4
gpt4 key购买 nike

我正在尝试在 C++ 程序中使用 C 库。我想将库 libcoap.a 链接到我的程序。我已经在 Qt 中制作了一个巨大的程序,所以它不能通过使用 make 而不是 qmake 来解决。

作为一个简单的例子,我编写了以下代码。

连接.h:

#include <string>
#include "coap.h"
void sendData(std::string);

连接.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 );
}

主要.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

提前致谢。

最佳答案

尝试使用(在connection.h中)

extern "C" {
#include "coap.h"
}

此外,在 connection.hconnection.cpp 中都包含 coap.h 是多余的(如果在 coap.h 没有 include 检查你的可执行文件的尺寸会无用地更大),所以从 .cpp 中删除它。

关于c++ - Qt C++ 中对库方法的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15925503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com