跨平台开发之Qt开发


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

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 network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets multimedia
win32:QT += axcontainer

#QT += multimediawidgets
#DEFINES +=VIDEO_STREAM_WIDGET

DEFINES += __STDINT_MACROS  #for ffmpeg
#CONFIG  += wince  ##wince

TARGET = AppLink_HMI
TEMPLATE = app

MOC_DIR=temp/moc
RCC_DIR=temp/rcc
UI_DIR=temp/ui
OBJECTS_DIR=temp/obj
DESTDIR=bin

target.path=$$OUT_PWD/bin
INSTALLS+=target

#qtdll.path=$$OUT_PWD/bin
#qtdll.files=$$QMAKE_LIBDIR/*.dll
#INSTALLS +=qtdll



SOURCES += \
    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.cpp




INCLUDEPATH += $$PWD/   \
              $$PWD/HMI_SDK \
              $$PWD/Tools  \
              $$PWD/UI

HEADERS  += 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.h


RESOURCES += \
    UI/images.qrc

OTHER_FILES += \
    UI/LiberationSerif-Regular.ttf

INCLUDEPATH +=  $$PWD/Include/ffmpeg \
                $$PWD/Include/msp

###############################for windows
win32:!wince{
DEFINES +=WIN32
INCLUDEPATH += $$PWD/Include/pthread \
               $$PWD/Include
LIBS +=  $$PWD/Library/win32/pthread/pthreadVC2.lib
LIBS +=  $$PWD/Library/win32/pthread/pthreadVCE2.lib
LIBS +=  $$PWD/Library/win32/pthread/pthreadVSE2.lib
LIBS +=  $$PWD/Library/win32/pthread/WS2_32.Lib
#LIBS += -L$$PWD/lib/win32/ffmpeg -lavcodec -lavfilter -lavformat -lavutil -lswscale
#win32: LIBS += -L$$PWD/ffmpeg/lib/ -lavcodec
LIBS += $$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.a

pthread.path=$$OUT_PWD/bin
pthread.files=$$PWD/Library/win32/*.dll
ffmpeg.path=$$OUT_PWD/bin
ffmpeg.files=$$PWD/Library/win32/ffmpeg/*.dll  \
$$PWD/Library/win32/pthread/*.dll
INSTALLS+=pthread
INSTALLS+=ffmpeg
qt_dll.path=$$DESTDIR
qt_dll.files=$$(QT_DIR)/bin/*.dll
INSTALLS +=qt_dll
}


################################for linux
unix:!android:LIBS += -L$$PWD/Library/linux/ffmpeg -lavcodec  -lavformat -lavutil -lswscale

################################for wince
wince{
HEADERS += \
    Include/global_first.h \
    Include/unistd.h \
    Include/stdint.h
INCLUDEPATH += $$PWD/Include/pthread \
               $$PWD/Include
LIBS +=  $$PWD/Library/ce/pthread.lib
LIBS += -L$$PWD/Library/ce/ffmpeg  -lavcodec-55  -lavdevice-55 -lavfilter-3 -lavformat-55 -lavutil-52 -lswresample-0 -lswscale-2
pthread.path=$$OUT_PWD/bin
pthread.files=$$PWD/Library/ce/*.dll
ffmpeg.path=$$OUT_PWD/bin
ffmpeg.files=$$PWD/Library/ce/ffmpeg/*.dll

INSTALLS +=pthread
INSTALLS+=ffmpeg
}


################################for android
android{

#CONFIG += msc
#CONFIG += pico
CONFIG  += espeak
INCLUDEPATH +=  $$PWD/Include/msp \
                $$PWD/Include/msp/android

DEFINES +=ANDROID \
          SDL_SUPPORT_LIB \
          SDL_SUPPORT_VR

LIBS += -L$$PWD/Library/android/ffmpeg -lffmpeg
LIBS += -L$$PWD/Library/android/sdl -lsmartDeviceLinkCore
LIBS += -L$$PWD/Library/android/msp  -llib_msp_vr
ANDROID_EXTRA_LIBS = \
        $$PWD/Library/android/ffmpeg/libffmpeg.so \
        $$PWD/Library/android/sdl/libsmartDeviceLinkCore.so
msc{
DEFINES += TTS_FLY_MSC
LIBS += -L$$PWD/Library/android/msp  -lmsc
#RESOURCES += Library/android/sdl/tts/msctts.qrc
ANDROID_EXTRA_LIBS +=$$PWD/Library/android/msp/libmsc.so
}
pico{
DEFINES +=TTS_ANDROID_SELF
LIBS += -L$$PWD/Library/android/msp  -lttspico -lttscompat
ANDROID_EXTRA_LIBS +=$$PWD/Library/android/msp/libttspico.so \
       $$PWD/Library/android/msp/libttscompat.so
}
espeak{
DEFINES += TTS_ESPEAK
LIBS += -L$$PWD/Library/android/msp -lttsespeak
ANDROID_EXTRA_LIBS +=$$PWD/Library/android/msp/libttsespeak.so
}

RESOURCES += \
    Library/android/sdl/config/android.qrc \
    Config/config.qrc
}


!android{
configfile.path=$$OUT_PWD/bin/Config
configfile.files=$$PWD/Config/*
INSTALLS +=configfile
}



#ANDROID_PACKAGE_SOURCE_DIR = $$PWD/Library/android/apk



  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值