通常, 当我们开发 Linux 程序时有两种方案:
在 Linux 上直接编写程序并进行运行测试和调试
在 Windows 或 Mac OS X 上借助工具进行远程开发
虽然我自己是在 Linux 环境上直接进行开发的, 但也有许多的人是在 Windows 环境上从事开发工作的, 如果离开自己熟悉的系统到陌生的环境上也许会影响到工作效率.
因此今天我们就来看下如何在 Windows 上使用 Visual Studio 2019 进行 Linux 远程开发以及如何避免常见的陷阱.
本文索引
Visual Studio 的跨平台开发功能简介
创建项目
配置远程项目
添加远程环境
本地编写和远程调试
避免踩坑
中文乱码
使用数学函数和第三方库
Visual Studio 的跨平台开发功能简介
从 visual studio 2017 开始微软推出了 vs 的跨平台开发功能, 你可以在 vs 中编辑代码, 随后进行跨平台编译和远程调试, 将原先我们需要手动完成的工作进行了自动化, 大幅减轻了我们的负担. 其中支持的平台包括 Android 和 Linux, 也就是我们今天要重点介绍的主角.
也许你会好奇, vs 究竟是怎样进行远程开发的, 虽然你不用了解这些知识也可以进行开发, 但我还是希望能用两分钟做个简短的解释.
vs 进行远程开发分为两步:
创建远程环境的连接, 随后让 vs 将远程环境中的系统头文件同步到本地(也可以指定其他地方的头文件, 后面会讲解),c++ 的代码补全只需要头文件即可.
当代码写好后, 选择合适的远程环境, vs 将目标文件和代码复制到远程环境的指定位置, 接着根据你的配置进行编译.
随后 vs 将会在 console 的 gdb 或 gdbserver 中运行你的程序, 在此期间你可以充分享受 vs debugger 带来的高效和便利.
经过上述步骤之后你就可以在 vs 里调试自己编写的跨平台程序了.
- #include <sys/utsname.h>
- #include <iostream>
- #include <cstdio>
- int main()
- {
- auto start = chrono::high_resolution_clock::now();
- utsname names;
- if (uname(&names) != 0) {
- std::perror("cannot get unames");
- }
- std::cout <<"Linux kernel version:" << names.release << std::endl;
- }
- #include <sys/utsname.h>
- #include <iostream>
- #include <cstdio>
- #include <string>
- int main()
- {
- utsname names;
- if (uname(&names) != 0) {
- std::perror("cannot get unames");
- }
- std::cout <<"Linux kernel version:" << names.release << std::endl;
- std::cout << "输入内容:";
- std::string input;
- std::cin>> input;
- std::cout <<"你输入了:" << input << std::endl;
- }
- #include <sys/utsname.h>
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <boost/chrono.hpp>
- int main()
- {
- namespace chrono = boost::chrono;
- auto start = chrono::high_resolution_clock::now();
- utsname names;
- if (uname(&names) != 0) {
- std::perror("cannot get unames");
- }
- std::cout <<"Linux kernel version:" << names.release << std::endl;
- std::cout << "输入内容:";
- std::string input;
- std::cin>> input;
- std::cout <<"你输入了:" << input << std::endl;
- auto counter = chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start);
- std::cout << "程序运行了:" << counter.count() << "ms\n";
- }
来源: https://www.cnblogs.com/apocelipes/p/10899484.html