Visual Studio 2017开发linux程序使用实例及原理简析

来源:互联网 发布:java安卓4.4.4模拟器 编辑:程序博客网 时间:2024/05/17 20:31

1.下载安装vs开发linux程序的工具包


2.创建一个工程,选择跨平台里面的linux平台


3.写一段测试代码,这里就可以包含linux的系统头文件编译,不过vs下还是现实红的

#include <cstdio>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <iconv.h>#include <sys/stat.h>#include <sys/types.h>#include <sys/file.h>#include <unistd.h>#include <sys/mman.h>#include <fcntl.h>#include <string>#include <iostream>#include <istream>#include <iosfwd>#include <sstream>#include <fstream>using namespace std;int fd_ = 0;int size_ = 0;char *buff_ = nullptr;void open_file(std::string file_name){fd_ = open(file_name.c_str(), O_RDONLY);if (fd_ == -1){size_ = 0;}else{struct stat st;int r = fstat(fd_, &st);if (r == -1){size_ = 0;close(fd_);}else{size_ = st.st_size;}}}void unmap_file(){if (fd_ != -1){close(fd_);munmap(buff_, size_);}}//if data is too big,create file spilt more file_index,mmap more times(hfrz ptr as start addr)int mmap_file(int fd, char *buff){if (fd == -1){return -1;}buff = (char *)mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0);if (buff == (void*)-1){std::ostringstream oslog;oslog << "mmap failed:" << strerror(errno);cout << oslog.str() << endl;close(fd);return -1;}return 0;}int main(){char *buff = (char *)malloc(1024);const char *str = "hello world";memcpy(buff, str, strlen(str)+1);    printf("hello from ConsoleApplication1!\n");mmap_file(23, buff);getchar();    return 0;}


然后选中调试,就弹出一个输入远程主机的信息,这里就和xshell使用ssh协议连接信息一样的输入:


4.编译调试,有个调试窗口要调出来,然后就可以看见linux控制台的输出信息了


编译输出控制台



5.登陆远程主机,可以看见启动的程序和源代码了


6.简要的分析一下原理

从vs编译的输出控制台我们可以看出编译是使用mingw32交叉编译,然后使用ssh协议将源代码拷贝到目标主机,看见gdbserver进程了,应该是vs使用ssh协议作为client与远程主机的gdbserver调试


一些配置信息也说明了这一点:


原创粉丝点击