跨平台开发之Qt开发

来源:互联网 发布:苹果4数据恢复 编辑:程序博客网 时间:2024/04/27 19:24


任何一种开发工具编译都至少包含两个步骤:

1、Build

2、run

看起来是不是很简单,但是实际上底层可能比我们想象的要复杂的多。这主要是因为开发工具在底层帮我们完成了一些工作,比如make是有依赖文件的,Unix系列平台主要是makefile这种脚本文件,Window上vs有它自带的vcxproj, sln文件。window上其他开发工具也有的用makefile。因为makefile是脚本语言,平台通用的。

如果进行细分的话,应该包括以下步骤:

1、 生成编译依赖文件,或者手写

2、Build

3、linker

4、install

反正就是make一定有一个依赖文件,他不会直接根据源文件来编译的,因为他不知道用什么编译器,gcc还是msvc,还是其他交叉工具链。另外他也不知道要编译哪些文件。他还不知道是编译成动态库静态库还是执行文件,当然他也不知道他依赖的外部库和头文件在哪里找。

今天主要讲一下Qt的跨平台开发是怎么解决以上问题的。

先回答一下Qt是怎么解决以上几个问题的。pro文件也不例外需要解决上面几个问题,那么是怎么解决的呢?

1) 指定编译器,工具链。

这个可以到Tools->Options->Build&Run,然后选择qmake,编译器,调试工具等!


需要自己安装编译器,会自动出现在这里,如果没有可以自己添加。

关于Android编译器在另外一篇里已经谈到。所以C++ 程序员在window上是可以直接用Qt开发Android的,同样像Qt一样的语法。

2)编译哪些文件,下面有些东西可以很好管理代码这就是pro文件的功能了,用pro文件可以很好的管理代码。

如果只习惯使用qt自动生成的pro,容易缺少代码管理的习惯。

如果有子工程:可以

SUBDIR += 子工程目录,里面一定也要用pro文件

平台控制:

windows平台可以写成:

win32:!wince{ #任意}

这样就可以与其他平台区分了。

wince:可能在qmake里增加 CONFIG +=wince然后,

wince{}

Linux:unix{}

Android:android{}

当然,如果你想增加自己的控制,也可以在CONFIG +=来增加,比如CONFIG +=msc,那么就可以

msc{}

来控制。

增加宏定义:DEFINES +=ANDROID

这个宏定义是用于作为在代码里进行编译开关的。跟在代码里写的#define一样。但是不觉得写到pro里对代码的管理能力更强吗。

其他的还有equal,contains等条件语句。具体可以到源码里找。

外部库文件:

LIBS +=-L路径

在代码里有时候用 #include "dir1/dir2/dir3/lib.h"等可能比较长,我们可以这样做:

INCLUDEPATH += dir1/dir2/dir3/

然后在代码里可以写成:#include <lib.h>或#include “lib.h"

下面两个就是你自己的代码了。

源文件:

SOURCES +=

头文件:

HEADERS += 

Install 主要是编译和链接完成后,打包的过程。

可以把依赖的库文件,配置文件通过Install打包到编译目录去。具体过程,

比如pthread的dll文件:

thread.path=$$PWD_PATH/bin

//这个是目标目录

thread.files +=后面写文件所在的目录/*.dll,可以多个。

参考后面的例子当然实现Install需要在Projects里增加一个make

步骤,如下:


下面看一个例子。

#-------------------------------------------------## Project created by QtCreator 2015-05-05T16:04:10##-------------------------------------------------QT       += core gui networkgreaterThan(QT_MAJOR_VERSION, 4): QT += widgets multimediawin32:QT += axcontainer#QT += multimediawidgets#DEFINES +=VIDEO_STREAM_WIDGETDEFINES += __STDINT_MACROS  #for ffmpeg#CONFIG  += wince  ##winceTARGET = AppLink_HMITEMPLATE = appMOC_DIR=temp/mocRCC_DIR=temp/rccUI_DIR=temp/uiOBJECTS_DIR=temp/objDESTDIR=bintarget.path=$$OUT_PWD/binINSTALLS+=target#qtdll.path=$$OUT_PWD/bin#qtdll.files=$$QMAKE_LIBDIR/*.dll#INSTALLS +=qtdllSOURCES += \    main.cpp \    HMI_SDK/Connect/SDLConnector.cpp \    HMI_SDK/Connect/Channel.cpp \    Tools/json/json_writer.cpp \    Tools/json/json_valueiterator.inl \    Tools/json/json_value.cpp \    Tools/json/json_reader.cpp \    HMI_SDK/AppData/AppList.cpp \    HMI_SDK/AppData/AppData.cpp \    UI/Alert/AlertUI.cpp \    UI/AudioPassThru/AudioPassThru.cpp \    UI/Choiceset/ChoicesetVR.cpp \    UI/Choiceset/Choiceset.cpp \    UI/Command/Command.cpp \    UI/Common/ScrollBar.cpp \    UI/Common/Button.cpp \    UI/Common/MainMenu.cpp \    UI/Common/BaseWidght.cpp \    UI/Common/AppBase.cpp \    UI/Common/AppItemWidget.cpp \    UI/Config/Config.cpp \    UI/ScrollableMessage/ScrollMsg.cpp \    UI/Show/Show.cpp \    UI/UIManager.cpp \    UI/Slider/Slider.cpp \      UI/Notify/Notify.cpp \    UI/Common/Background.cpp \    UI/VideoStream/VideoStream.cpp \    UI/TextSpeech/textspeech.cpp \    UI/AudioTrans/AudioInput.cpp \    UI/AudioTrans/MspVRAudio.cpp \    UI/TextSpeech/TextToSpeech.cpp \    UI/AudioTrans/AudioOutput.cpp \    AppManager.cpp \    UI/Common/AppListWidget.cpp \    UI/Common/MenuButton.cpp \    UI/AppLinkMenu.cpp \    UI/Common/CAppButton.cpp  \    HMI_SDK/Connect/BasicCommunication.cpp \    HMI_SDK/Connect/Buttons.cpp \    HMI_SDK/Connect/Navigation.cpp \    HMI_SDK/Connect/SocketsToSDL.cpp \    HMI_SDK/Connect/TTS.cpp \    HMI_SDK/Connect/UI.cpp \    HMI_SDK/Connect/VehicleInfo.cpp \    HMI_SDK/Connect/VR.cpp \    UI/AppList/AppListUI.cppINCLUDEPATH += $$PWD/   \              $$PWD/HMI_SDK \              $$PWD/Tools  \              $$PWD/UIHEADERS  += HMI_SDK/AppData/AppListInterface.h \    HMI_SDK/AppData/AppDataInterface.h \    HMI_SDK/AppData/AppData.h \    HMI_SDK/AppData/AppList.h \    HMI_SDK/Connect/SDLConnector.h \    HMI_SDK/Connect/Channel.h \    HMI_SDK/Connect/IMessageInterface.h \    HMI_SDK/Connect/SDLConnector.h \    HMI_SDK/Connect/ISocketManager.h \    Tools/json/version.h.in \    Tools/json/json_tool.h \    Tools/json/writer.h \    Tools/json/version.h \    Tools/json/value.h \    Tools/json/reader.h \    Tools/json/json.h \    Tools/json/forwards.h \    Tools/json/features.h \    Tools/json/config.h \    Tools/json/autolink.h \    Tools/json/assertions.h \    UI/Common/MainMenu.h \    UI/Common/BaseWidght.h \    UI/Common/AppBase.h \    UI/UIInterface.h \    UI/UIManager.h \    UI/Alert/AlertUI.h \    UI/AudioPassThru/AudioPassThru.h \    UI/Choiceset/ChoicesetVR.h \    UI/Choiceset/Choiceset.h \    UI/Command/Command.h \    UI/Common/ScrollBar.h \    UI/Common/Button.h \    UI/Common/AppItemWidget.h \    UI/Config/Config.h \    UI/ScrollableMessage/ScrollMsg.h \    UI/Show/Show.h \    UI/Slider/Slider.h \    UI/Notify/Notify.h \    UI/Common/Background.h \    UI/VideoStream/VideoStream.h \    UI/TextSpeech/textspeech.h \    UI/AudioTrans/AudioInput.h \    UI/AudioTrans/MspVRAudio.h \    UI/TextSpeech/TextToSpeech.h \    UI/Common/MenuButton.h \    UI/AppLinkMenu.h \    UI/Common/CAppButton.h \    UI/AudioTrans/AudioOutput.h \    AppManager.h \    HMI_SDK/AppData/AppCommon.h \    HMI_SDK/Connect/BasicCommunication.h \    HMI_SDK/Connect/Buttons.h \    HMI_SDK/Connect/Navigation.h \    HMI_SDK/Connect/SocketsToSDL.h \    HMI_SDK/Connect/TTS.h \    HMI_SDK/Connect/UI.h \    HMI_SDK/Connect/VehicleInfo.h \    HMI_SDK/Connect/VR.h \    Include/ProtocolDefines.h \    UI/Common/AppListWidget.h \    UI/UIInclude.h \    UI/AppList/AppListUI.hRESOURCES += \    UI/images.qrcOTHER_FILES += \    UI/LiberationSerif-Regular.ttfINCLUDEPATH +=  $$PWD/Include/ffmpeg \                $$PWD/Include/msp###############################for windowswin32:!wince{DEFINES +=WIN32INCLUDEPATH += $$PWD/Include/pthread \               $$PWD/IncludeLIBS +=  $$PWD/Library/win32/pthread/pthreadVC2.libLIBS +=  $$PWD/Library/win32/pthread/pthreadVCE2.libLIBS +=  $$PWD/Library/win32/pthread/pthreadVSE2.libLIBS +=  $$PWD/Library/win32/pthread/WS2_32.Lib#LIBS += -L$$PWD/lib/win32/ffmpeg -lavcodec -lavfilter -lavformat -lavutil -lswscale#win32: LIBS += -L$$PWD/ffmpeg/lib/ -lavcodecLIBS += $$PWD/Library/win32/ffmpeg/libavcodec.a  \$$PWD/Library/win32/ffmpeg/libavfilter.a  \$$PWD/Library/win32/ffmpeg/libavformat.a  \$$PWD/Library/win32/ffmpeg/libavutil.a  \$$PWD/Library/win32/ffmpeg/libswscale.apthread.path=$$OUT_PWD/binpthread.files=$$PWD/Library/win32/*.dllffmpeg.path=$$OUT_PWD/binffmpeg.files=$$PWD/Library/win32/ffmpeg/*.dll  \$$PWD/Library/win32/pthread/*.dllINSTALLS+=pthreadINSTALLS+=ffmpegqt_dll.path=$$DESTDIRqt_dll.files=$$(QT_DIR)/bin/*.dllINSTALLS +=qt_dll}################################for linuxunix:!android:LIBS += -L$$PWD/Library/linux/ffmpeg -lavcodec  -lavformat -lavutil -lswscale################################for wincewince{HEADERS += \    Include/global_first.h \    Include/unistd.h \    Include/stdint.hINCLUDEPATH += $$PWD/Include/pthread \               $$PWD/IncludeLIBS +=  $$PWD/Library/ce/pthread.libLIBS += -L$$PWD/Library/ce/ffmpeg  -lavcodec-55  -lavdevice-55 -lavfilter-3 -lavformat-55 -lavutil-52 -lswresample-0 -lswscale-2pthread.path=$$OUT_PWD/binpthread.files=$$PWD/Library/ce/*.dllffmpeg.path=$$OUT_PWD/binffmpeg.files=$$PWD/Library/ce/ffmpeg/*.dllINSTALLS +=pthreadINSTALLS+=ffmpeg}################################for androidandroid{#CONFIG += msc#CONFIG += picoCONFIG  += espeakINCLUDEPATH +=  $$PWD/Include/msp \                $$PWD/Include/msp/androidDEFINES +=ANDROID \          SDL_SUPPORT_LIB \          SDL_SUPPORT_VRLIBS += -L$$PWD/Library/android/ffmpeg -lffmpegLIBS += -L$$PWD/Library/android/sdl -lsmartDeviceLinkCoreLIBS += -L$$PWD/Library/android/msp  -llib_msp_vrANDROID_EXTRA_LIBS = \        $$PWD/Library/android/ffmpeg/libffmpeg.so \        $$PWD/Library/android/sdl/libsmartDeviceLinkCore.somsc{DEFINES += TTS_FLY_MSCLIBS += -L$$PWD/Library/android/msp  -lmsc#RESOURCES += Library/android/sdl/tts/msctts.qrcANDROID_EXTRA_LIBS +=$$PWD/Library/android/msp/libmsc.so}pico{DEFINES +=TTS_ANDROID_SELFLIBS += -L$$PWD/Library/android/msp  -lttspico -lttscompatANDROID_EXTRA_LIBS +=$$PWD/Library/android/msp/libttspico.so \       $$PWD/Library/android/msp/libttscompat.so}espeak{DEFINES += TTS_ESPEAKLIBS += -L$$PWD/Library/android/msp -lttsespeakANDROID_EXTRA_LIBS +=$$PWD/Library/android/msp/libttsespeak.so}RESOURCES += \    Library/android/sdl/config/android.qrc \    Config/config.qrc}!android{configfile.path=$$OUT_PWD/bin/Configconfigfile.files=$$PWD/Config/*INSTALLS +=configfile}#ANDROID_PACKAGE_SOURCE_DIR = $$PWD/Library/android/apk



0 0