使用Visual Studio 2019编译Telegram 源码(Windows 版)

目录

★ 重点:

★ 搜集到的编译成功的链接:

★ 编译步骤:

         一、安装Visual Studio 2019 16.5.0

二、按照官方指南下载编译所需要的第三方库

三、编译

四、在VS中构建Telegram项目


本文写于2020年8月4日22:40,此时telegram版本更新到了最新版V2.2.0

★ 重点:

    1)  Visual Studio 需要用2019版的,可以选用 VS 2019 16.5.0版本(之前用的16.6.5版本都报错,论坛里提倡使用16.5.0版本),VS各版本的下载链接为:https://docs.microsoft.com/en-us/visualstudio/releases/2019/history

    2)本文的编译步骤完全采用官方手册的指导步骤,链接为:https://github.com/telegramdesktop/tdesktop/blob/dev/docs/building-msvc.md

    3)Telegram的界面确实使用Qt写的,但是接下来的编译源码过程无需单独下载Qt,只需要按照教程一步步走下去即可。如果大家出现的错误在网上没有搜到合适答案,可以去Telegram的论坛提问:https://github.com/telegramdesktop/tdesktop/issues,特别是论坛里的这个问题可以看看:https://github.com/telegramdesktop/tdesktop/issues/7950

★ 搜集到的编译成功的链接:

1) 目前为止的官方指导教程(2020.8.4)https://github.com/telegramdesktop/tdesktop/blob/dev/docs/building-msvc.md

2) CSDN博客(2020.5.15):https://blog.csdn.net/u012088909/article/details/104486027

3) 有道云笔记(2020.3.1):https://note.youdao.com/ynoteshare1/index.html?id=8c041e143d74d7f86df755d14333e7c4&type=note

4) CSDN博客(2020.2.21):https://blog.csdn.net/robinfoxnan/article/details/104432148

5) 知乎(2018.11.13):https://zhuanlan.zhihu.com/p/42242300

        由于没有使用官方认可的VS版本(也就是VS2019 16.5.0),我尝试过上述几个链接的步骤好多遍,最后都没有编译成功,如果使用正确的VS版本,按照其官方指导步骤,其实完全能行,但是其他链接里有一些编译报错的解决方案,可以作为参考。本文使用的是链接1(官方编译指南)

★ 编译步骤:

 

一、安装Visual Studio 2019 16.5.0

1)下载链接为:https://docs.microsoft.com/en-us/visualstudio/releases/2019/history,选择16.5.0 Professional版本

2)下载后安装,工作负载选的尽量全一些,以免后期编译的时候缺少VS某工具。

3) 安装VS可能出现的问题:

      由于大多人可能是已经有了VS,然后重装为VS 2019 16.5.0版本,因此可以使用https://www.zhihu.com/question/376347545下载工具删除VS旧版本,然后再删除控制面板中程序的旧VS相关插件即可。在安装新的VS时可能会出现下列情况:

                                   

可以使用http://www.mamicode.com/info-detail-2909897.html解决该问题,如果还是解决不了,将vs installer从控制面板的程序中删除,再次双击运行vs_Professional.exe即可。

二、按照官方指南下载编译所需要的第三方库

1) 新建一个文件夹作为Telegram项目的目录,例如可以选择在D盘新建一个TBuild文件夹作为项目根目录,则 D:\TBuild 我们称其为BuildPath。在BuidPath下需要另外新建一个文件夹ThirdParty储存项目所需的第三方库,然后在ThirdParty目录下再新建下列8个空文件夹,命名和我的一样即可,这个8个文件夹就是第三方工具的安装地址。

接下来通过下面网址将各工具的安装包下载到对应的文件夹中:

     安装时只要安装路径为上述路径即可,其他为默认。

  • 2. Download NASM installer from http://www.nasm.us and install to BuildPath\ThirdParty\NASM

       

    下载后的yasm-1.3.0-win64.exe改名为yasm.exe

​​​​​​​

     下载且安装。

​​​​​​​       下载后解压到指定文件夹。

​​​​​​​       本文下载的版本是python-2.7.17.amd64.msi,在安装python的时候需要注意下图,环境变量要勾选:

​​​​​​​     本文选择的版本是cmake-3.16.4-win64-x64.msi,安装的时候注意下图:

            

       根据链接下载ninja-win.zip并解压到指定文件即可。​​​​​​​

       下载git并安装,git可以安装在其他文件目录,版本自选,只需注意下图:

           

 

 

三、编译

1) 打开x86 Native Tools Command Prompt for VS 2019.bat,进入BuildPath目录。

2) 输入以下命令:

cd ThirdParty
git clone https://github.com/desktop-app/patches.git
cd patches
git checkout 7df6fdd
cd ../

效果如图所示:

         

3) 接下来克隆的gyp文件夹是需要翻墙的(不然无法访问那个网址),因此使用git clone时需要先设置代理,设置代理的步骤如下:

git config --global http.proxy socks5://127.0.0.1:21080
git config --global https.proxy socks5://127.0.0.1:21080

上述两条命令是设置代理,后面的端口21080要根据自己电脑的实际情况设置,翻墙工具的话大家各自想办法吧。

可输入下列命令查看是否设置代理成功:

git config --global -e

会出现下图所示:

可敲命令 :q  退出该代理设置的界面。

输入以下命令下载gyp文件夹:

git clone https://chromium.googlesource.com/external/gyp
cd gyp
git checkout 9f2a7bb1
git apply ../patches/gyp.diff
cd ..\..

如图所示:

关闭代理(后面的网址访问基本不需要代理了,因此可以关闭代理),输入以下指令:

git config --global --unset http.proxy
git config --global --unset https.proxy

4) 添加GYP和NINJA的系统环境变量:

    a. 打开系统环境变量:

                      

     

                     

       b.  重启电脑,使之生效。

       c.  测试gyp和ninja的环境变量是否设置成功,可打开x86 Native Tools Command Prompt for VS 2019.bat,进入BuildPath目录,输入下列两条指令gyp -h ninja --version:效果图如下图所示

            

5) 下载其他依赖包

输入下列命令:该命令基本不会出错,就是设置临时环境变量和下载Telegram的tdesktop源码。

SET PATH=%cd%\ThirdParty\Strawberry\perl\bin;%cd%\ThirdParty\Python27;%cd%\ThirdParty\NASM;%cd%\ThirdParty\jom;%cd%\ThirdParty\cmake\bin;%cd%\ThirdParty\yasm;%PATH%

git clone --recursive https://github.com/telegramdesktop/tdesktop.git

mkdir Libraries
cd Libraries

SET LibrariesPath=%cd%

git clone https://github.com/desktop-app/patches.git
cd patches
git checkout 7df6fdd
cd ..

6) 继续输入命令:下面命令可能会在执行msbuild后出错,假如使用了VS正确版本,应该不会遇到错误,我将给出执行第一个msbuild后的效果图,会提示有一个warnning。

git clone https://github.com/desktop-app/lzma.git
cd lzma\C\Util\LzmaLib
msbuild LzmaLib.sln /property:Configuration=Debug
msbuild LzmaLib.sln /property:Configuration=Release
cd ..\..\..\..

7) 继续输入以下指令,下面命令会出现一堆信息(特别是 nmake 这一句),我将给出执行下列命令的效果图:

git clone https://github.com/openssl/openssl.git openssl_1_1_1
cd openssl_1_1_1
git checkout OpenSSL_1_1_1-stable
perl Configure no-shared no-tests debug-VC-WIN32
nmake
mkdir out32.dbg
move libcrypto.lib out32.dbg
move libssl.lib out32.dbg
move ossl_static.pdb out32.dbg\ossl_static
nmake clean
move out32.dbg\ossl_static out32.dbg\ossl_static.pdb

  

8) 上段命令是编译debug版的win32,接下来编译release版的win32,继续下列指令,效果图和7)一样,也是跳出满屏的信息,所以就不陈列效果图了。

perl Configure no-shared VC-WIN32
nmake
mkdir out32
move libcrypto.lib out32
move libssl.lib out32
move ossl_static.pdb out32
cd ..

9) 继续下列指令(下列指令基本不会出错):

git clone https://github.com/desktop-app/zlib.git
cd zlib
cd contrib\vstudio\vc14
msbuild zlibstat.vcxproj /property:Configuration=Debug
msbuild zlibstat.vcxproj /property:Configuration=ReleaseWithoutAsm
cd ..\..\..\..

10) 继续下列指令(效果图为执行cmake ... 语句和 msbuild ...语句):

git clone https://github.com/telegramdesktop/openal-soft.git
cd openal-soft
git checkout fix_capture
cd build
cmake -G "Visual Studio 16 2019" -A Win32 -D LIBTYPE:STRING=STATIC -D FORCE_STATIC_VCRT:STRING=ON ..
msbuild OpenAL.vcxproj /property:Configuration=Debug
msbuild OpenAL.vcxproj /property:Configuration=Release
cd ..\..

11) 执行下列语句,效果图为执行:gyp --no-circular-check breakpad_client.gyp --format=ninja后和执行ninja -C out/Debug common crash_generation_client exception_handler后和执行msbuild dump_syms.vcxproj /property:Configuration=Release后共三张效果图:

git clone https://github.com/google/breakpad
cd breakpad
git checkout a1dbcdcb43
git apply ../../tdesktop/Telegram/Patches/breakpad.diff
cd src
git clone https://github.com/google/googletest testing
cd client\windows
gyp --no-circular-check breakpad_client.gyp --format=ninja
cd ..\..
ninja -C out/Debug common crash_generation_client exception_handler
ninja -C out/Release common crash_generation_client exception_handler
cd tools\windows\dump_syms
gyp dump_syms.gyp
msbuild dump_syms.vcxproj /property:Configuration=Release
cd ..\..\..\..\..

12) 继续执行下列指令并贴一张执行msbuild opus.sln...后的效果图:

git clone https://github.com/telegramdesktop/opus.git
cd opus
git checkout tdesktop
cd win32\VS2015
msbuild opus.sln /property:Configuration=Debug /property:Platform="Win32"
msbuild opus.sln /property:Configuration=Release /property:Platform="Win32"

cd ..\..\..\..
SET PATH_BACKUP_=%PATH%
SET PATH=%cd%\ThirdParty\msys64\usr\bin;%PATH%
cd Libraries

13) 执行下列代码,当执行bash --login ../../tdesktop/Telegram/Patches/build_ffmpeg_win.sh可能会发生错误,如果是超时原因,那么可设置代理,这个情况极有可能是网络状况不佳的原因,此时错误参考链接为:https://blog.csdn.net/u012088909/article/details/104486027该博客会介绍这个错误的原因。

git clone https://github.com/FFmpeg/FFmpeg.git ffmpeg
cd ffmpeg
git checkout release/3.4

set CHERE_INVOKING=enabled_from_arguments
set MSYS2_PATH_TYPE=inherit
bash --login ../../tdesktop/Telegram/Patches/build_ffmpeg_win.sh

SET PATH=%PATH_BACKUP_%
cd ..

14) 执行下列代码并给出执行命令的效果图:

git clone git://code.qt.io/qt/qt5.git qt_5_12_8
cd qt_5_12_8
perl init-repository --module-subset=qtbase,qtimageformats
git checkout v5.12.8
git submodule update qtbase qtimageformats
cd qtbase
for /r %i in (..\..\patches\qtbase_5_12_8\*) do git apply %i
cd ..

15) 执行下列代码(控制台会出现一堆信息):

configure -prefix "%LibrariesPath%\Qt-5.12.8" -debug-and-release -force-debug-info -opensource -confirm-license -static -static-runtime -I "%LibrariesPath%\openssl_1_1_1\include" -no-opengl -openssl-linked OPENSSL_LIBS_DEBUG="%LibrariesPath%\openssl_1_1_1\out32.dbg\libssl.lib %LibrariesPath%\openssl_1_1_1\out32.dbg\libcrypto.lib Ws2_32.lib Gdi32.lib Advapi32.lib Crypt32.lib User32.lib" OPENSSL_LIBS_RELEASE="%LibrariesPath%\openssl_1_1_1\out32\libssl.lib %LibrariesPath%\openssl_1_1_1\out32\libcrypto.lib Ws2_32.lib Gdi32.lib Advapi32.lib Crypt32.lib User32.lib" -mp -nomake examples -nomake tests -platform win32-msvc

16) 执行下列代码:

jom -j4
jom -j4 install
cd ..

17) 构建项目,首先需要获得api_id和api_hash,可以自己使用手机号注册获得 (obtaining your api_id and api_hash),也可使用专供测试使用的值:api_id: 17349 api_hash: 344583e45741c457fe1862106095a5eb,本文使用该值。

BuildPath\tdesktop\Telegram目录下输入命令:

configure.bat  force  -D TDESKTOP_API_ID=17349 -D TDESKTOP_API_HASH=344583e45741c457fe1862106095a5eb -D DESKTOP_APP_USE_PACKAGED=OFF

这时,BuildPath\tdesktop\out\目录下会出现Telegram.sln,如图所示:

四、在VS中构建Telegram项目

1) 用VS2019打开Telegarm.sln

2) 将工程内的所有文件的警告等级改为否,操作步骤为:打开解决方案,将所有项目 右键 -.> 属性 -> 所有配置 -> C/C++ -> 将警告视为错误 改为 否。如下图:

3) 右键Telegram,点击生成,结果如下:

4) 点击“调试"——>"开始执行"(Ctrl+F5),即可运行。

 

 

 

 

以下是一个简单的 Telegram 机器人的 Python 代码示例: ```python import telegram from telegram.ext import Updater, CommandHandler, MessageHandler, Filters # 定义 /start 命令处理程序 def start(update, context): update.message.reply_text('你好!欢迎使用机器人!') # 定义 /help 命令处理程序 def help(update, context): update.message.reply_text('这是一个帮助信息') # 定义文本消息处理程序 def echo(update, context): update.message.reply_text(update.message.text) # 定义错误处理程序 def error(update, context): print(f"Update {update} caused error {context.error}") # 创建 Updater 对象并添加处理程序 updater = Updater(token='YOUR_TOKEN', use_context=True) dispatcher = updater.dispatcher dispatcher.add_handler(CommandHandler('start', start)) dispatcher.add_handler(CommandHandler('help', help)) dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo)) dispatcher.add_error_handler(error) # 启动机器人 updater.start_polling() ``` 在这个示例中,我们使用了 Python 的 `telegram` 库和 `python-telegram-bot` 库。首先,我们定义了几个命令处理程序和一个文本消息处理程序。然后,我们创建了一个 Updater 对象并将处理程序添加到 dispatcher 中。最后,我们使用 `start_polling()` 方法启动机器人。 请注意,你需要使用你自己的 Telegram bot token 替换代码中的 "YOUR_TOKEN"。 这只是一个简单的示例,你可以根据你的需求编写更复杂的机器人代码。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值