1.C
QLocale::C 1 The "C" locale is identical in behavior to English/UnitedStates. “C”的语言环境就是英语/美国语言
extern "C" TEST_COMMON_DLLSPEC QWidget* creatWidet1();
extern C 总结: http://www.cnblogs.com/graphics/archive/2010/12/24/1916343.html
extern C 怎么用 http://bbs.csdn.net/topics/80206083
上面借个帖子,总结得:
1.extern "C"是C++特有的,为的是用C的编译方式编译{...}的代码;
2.extern "C" double sqrt( double );链接指示符不能出现在函数内;
3.extern "FORTRAN" 等等 可以调用其他的语言;
2. 官网上创建动态链接库, 静态链接库,linux,window两个平台的翻译 与 实践
官网地址http://qt-project.org/wiki/How_to_create_a_library_with_Qt_and_use_it_in_an_application
1.Creating a shared library
test.pro
TEMPLATE = lib
#CONFIG += staticlib #若不是定义静态库 则注释
# Input
SOURCES += test.cpp
HEADERS+= test.h
DEFINES += TEST #若不是定义静态库 则不注释
test.h
#ifndef TEST_H
#define TEST_H
#include <QWidget>
#ifdef TEST
#define TEST_COMMON_DLLSPEC Q_DECL_EXPORT
#else
#define TEST_COMMON_DLLSPEC Q_DECL_IMPORT
#endif
class TEST_COMMON_DLLSPEC Widget : public QWidget
{
Q_OBJECT
public:
Widget();
};
#endif
test.cpp
#include "test.h"
Widget::Widget() : QWidget()
{}
以上三个文件,build即可,在debug文件里可以看到相应的东西
In windows, MinGW will output.a and .dll, MSVC2010 will ouput .lib and .dll. In linux, MinGW will output .so, .so.1, .so.1.0 and .so.1.0.0 – .lib, .a and .so are import libraries. They help link your code to the library and is needed when you build your file(.a files not all the time).
2.Linking your application against the shared library
说明:
最好直接copy 【Qt 5.3.2 或其他高版本】在.pro 文件里,右键->添加库->外部库->动态链接->选中路径,即可;
可能的话需要修改【路径】;
头文件的话,可以直接INCLUDEPATH 原头文件 即可;
有些版本右键只能添加.lib库文件,只能从能用的 creator里copy了。
loadTestLib.pro
TEMPLATE = app
TARGET = x
# Input
SOURCES += main.cpp
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/release/ -ltest
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/debug/ -ltest
else:unix: LIBS += -L$$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/ -ltest
INCLUDEPATH += $$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/debug
DEPENDPATH += $$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/debug
INCLUDEPATH += ../test
main.cpp
#include <QtGui>
#include "test.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.resize(100,100);
w.show();
return a.exec();
}
3.Using QLibrary to load the shared library
qlibraryLibrary.pro
TEMPLATE = lib
HEADERS += widget.h
SOURCES += widget.cpp
DEFINES += TEST
widget.h
TEMPLATE = lib
HEADERS += widget.h
SOURCES += widget.cpp
DEFINES += TEST
widget.cpp
#include <QtGui>
#include "widget.h"
QWidget* createWidget1()
{
QWidget *wid = new QWidget();
wid->resize(100,100);
return wid;
}
三个文件时生成 相应的库文件
3.1 Load the library using QLibrary
loadlibrary.pro
QT += core gui
TARGET = loadLibrary
TEMPLATE = app
SOURCES += main.cpp
main.cpp
#include <QtGui>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QLibrary library("qlibraryLibrary.dll");
if (!library.load())
qDebug() << library.errorString();
if (library.load())
qDebug() << "library loaded";
typedef QWidget*(*CreateWidgetFunction)(void);
CreateWidgetFunction cwf = (CreateWidgetFunction)library.resolve("createWidget1");
if (cwf) {
QWidget* wid = cwf();
if (wid)
wid->show();
} else {
qDebug() << "Could not show widget from the loaded library";
}
return app.exec();
}
如此即可调用dll文件, 和调用外部链接库不一样,这儿不需要应用头文件就可以直接使用
4.Creating a static library
test.pro
TEMPLATE = lib
CONFIG += staticlib #若不是定义静态库 则注释
# Input
SOURCES += test.cpp
HEADERS+= test.h
#DEFINES += TEST #若不是定义静态库 则不注释
修改pro文件,如此即可, 其他test.h test.cpp不需要做修改
5.Using the static library in your application
useStaticLib.pro
TEMPLATE = app
TARGET =
CONFIG+= console
# Input
SOURCES += main.cpp
#INCLUDEPATH += ../test
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/release/ -ltest
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/debug/ -ltest
else:unix: LIBS += -L$$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/ -ltest
INCLUDEPATH += $$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/debug
DEPENDPATH += $$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/debug
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/release/libtest.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/debug/libtest.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/release/test.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/debug/test.lib
else:unix: PRE_TARGETDEPS += $$PWD/../test-build-desktop-Qt_4_7_4__4_7_4____/libtest.a
这儿的添加静态库,同样需要creator 里右键 添加
main.cpp
#include <QtGui>
#include "test.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.resize(100,100);
w.show();
return a.exec();
}