找回密码
 立即注册
收起左侧

隐式调用C++库

8
回复
3886
查看
[复制链接]
累计签到:6 天
连续签到:2 天
来源: 2018-10-31 20:29:58 显示全部楼层 |阅读模式
1Qter豆
我的做法:将.h,.lib 复制到工程目录下;
                右键工程添加现有文件,选择.h文件;
                右键添加库,选择.lib文件;
                在.cpp文件中,#include ".h头文件",直接用dll里面的函数。
但是会报错:LNK2019无法解析这个函数,为什么呀

回复

使用道具 举报

累计签到:6 天
连续签到:2 天
2018-10-31 23:26:14 显示全部楼层
补充:
(1)pro中,最后添加了 LIBS +=-LD:/study/QT_Test/Test/DLLTest/Dll/-l库名(没有后缀名)
(2)//Enhance 是我库中文件
//__declspec(dllexport) bool Enhance(unsigned char *pInput, unsigned char *pOutput, int nWidth, int nHeight, float fMin = 0.01f, float fMax = 0.01f);
我的调用:
void MainWindow:n_pushButton_clicked()
{
    unsigned char *p=(unsigned char *)malloc(sizeof (unsigned char));
    unsigned char *q=(unsigned char *)malloc(sizeof (unsigned char));
    qDebug()<<Enhance(p,q,350,200,0.01f,0.01f);
}

     出现的问题:(1)mainwindow.obj:-1: error: LNK2019: �޷��������ⲿ���� "bool __cdecl Enhance(unsigned char *,unsigned char *,int,int,float,float)" (?Enhance@@YA_NPEAE0HHMM@Z)���÷����ں��� "private: void __cdecl MainWindow:n_pushButton_clicked(void)" (?on_pushButton_clicked@MainWindow@@AEAAXXZ) �б�����
                (2)debug\Dll.exe:-1: error: LNK1120: 1 ���޷��������ⲿ����
        请问各位大神这些问题的原因是什么?怎样改正

点评

能把你的pro贴到附件里吗?从帖子里看,-l前面没有空格,应该要加个空格吧。再就是debug版/release版本问题。链接时用同版本。  详情 回复 发表于 2018-11-1 09:56
回复

使用道具 举报

累计签到:41 天
连续签到:1 天
2018-11-1 09:56:24 显示全部楼层
都忘了 发表于 2018-10-31 23:26
补充:
(1)pro中,最后添加了 LIBS +=-LD:/study/QT_Test/Test/DLLTest/Dll/-l库名(没有后缀名)
(2)/ ...

能把你的pro贴到附件里吗?从帖子里看,-l前面没有空格,应该要加个空格吧。再就是debug版/release版本问题。链接时用同版本。
回复

使用道具 举报

累计签到:6 天
连续签到:2 天
2018-11-1 20:34:40 显示全部楼层
baizy77 发表于 2018-11-1 09:56
能把你的pro贴到附件里吗?从帖子里看,-l前面没有空格,应该要加个空格吧。再就是debug版/release版本问 ...

大神您好,下面是我的pro文件:
#-------------------------------------------------
#
# Project created by QtCreator 2018-10-31T21:38:53
#
#-------------------------------------------------

QT       += core gui widgets

TARGET = Dll
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h \
        Enhancement.h

FORMS += \
        mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

LIBS +=-LD:/study/QT_Test/Test/DLLTest/Dll/ -lEnhancement
您说的debug/release版本我没理解,我调用的dll库不是QT写的

点评

Enhancement.h方便贴一下吗?另外,如果您编译的是debug版本,而调用的dll是release版本可能也不行,或者release链接debug版的lib。  详情 回复 发表于 2018-11-2 08:01
回复

使用道具 举报

累计签到:41 天
连续签到:1 天
2018-11-2 08:01:37 显示全部楼层
都忘了 发表于 2018-11-1 20:34
大神您好,下面是我的pro文件:
#-------------------------------------------------
#

Enhancement.h方便贴一下吗?另外,如果您编译的是debug版本,而调用的dll是release版本可能也不行,或者release链接debug版的lib。
回复

使用道具 举报

累计签到:6 天
连续签到:2 天
2018-11-2 08:22:43 显示全部楼层
baizy77 发表于 2018-11-2 08:01
Enhancement.h方便贴一下吗?另外,如果您编译的是debug版本,而调用的dll是release版本可能也不行,或者 ...

Enhancement.h:
#pragma once
/*
函数:
本函数用于对图像进行对比度增强
参数说明:
pInput:指向输入图像数据的指针
pOutput:指向输出图像数据的指针 (pOutput的大小必须与pInput一样大,即输入图像和输出图像大高宽一样)
nWidth:输入输出图像的宽度
nHeight:输入输出图像的高度
fMin:最小阈值,在0-1之间,值越大,对比度增强越明显,但是会损失图像信息,建议设置为0.01
fMax:最大阈值,在0-1之间,值越大,对比度增强越明显,但是会损失图像信息,建议设置为0.01
返回值:
成功返回true,失败返回false.
*/
__declspec(dllexport) bool Enhance(unsigned char *pInput, unsigned char *pOutput, int nWidth, int nHeight, float fMin = 0.01f, float fMax = 0.01f);

我写的函数调用:
void MainWindow:n_pushButton_clicked()
{
    bool a=false;
    unsigned char *p=new unsigned char;
    p=imgScaled->bits();
    unsigned char *q=new unsigned char;
    q=p;
    a=Enhance(p,q,350,200,0.01f,0.01f);
    qDebug()<<a;
    delete  p;
    delete  q;
}
现在的问题是程序异常关闭,我debug 和release 都运行过了,不可以,请大神指导!
回复

使用道具 举报

累计签到:6 天
连续签到:2 天
2018-11-2 08:49:56 显示全部楼层
都忘了 发表于 2018-11-2 08:22
Enhancement.h:
#pragma once
/*

谢谢大神!我现在可以调用这个函数了,在函数参数方面还有问题:参数是unsigned char *q,想要在label中显示q指向的图片,但是QT中,label显示图片的参数QImage,QPicture这样的,请问怎样转换呀,求大神指导

点评

加我QQ吧, 1309547563,或者加群:"Qt入门与提高:GUI产品开发”:868216990。加群时请注明:Qt开源社区。  详情 回复 发表于 2018-11-2 09:48
回复

使用道具 举报

累计签到:41 天
连续签到:1 天
2018-11-2 09:48:38 显示全部楼层
本帖最后由 baizy77 于 2018-11-2 10:01 编辑
都忘了 发表于 2018-11-2 08:49
谢谢大神!我现在可以调用这个函数了,在函数参数方面还有问题:参数是unsigned char *q,想要在label中显 ...

你的代码有问题,有内存泄漏。unsigned char *q=new unsigned char;
    q=p;
应该写成:
unsigned char *q = p;
加我QQ吧, 1309547563,或者加群:"Qt入门与提高:GUI产品开发”:868216990。加群时请注明:Qt开源社区。


回复

使用道具 举报

累计签到:6 天
连续签到:2 天
2018-11-2 10:04:17 显示全部楼层
baizy77 发表于 2018-11-2 09:48
你的代码有问题,有内存泄漏。unsigned char *q=new unsigned char;
    q=p;
应该写成:

谢谢大神,QQ已提出申请
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了