首页 > 作文

Vscode搭建远程c开发环境的图文教程

更新时间:2023-04-03 23:15:10 阅读: 评论:0

目录
基础环境远程开发套件建立c文件配置编译任务开始编译开始调试注意参考资料

基础环境

目标机:ubuntu20开发机:win10、mac 安装

远程开发套件

本机和目标机都需要安装。

参考 vscode 免密登录服务器编辑配置服务器信息,用vscode打开远程电脑的一个目录。

建立c文件

#include <stdio.h>#include <stdlib.h>#include <unistd.h>//系统函数#include <string.h>#include <pthread.h>//多线程库 #include "net_control_client.h" ts_tcp_client s_tcp_client;  static int connect_to_rver(void){while(connect(s_tcp_client.socket_fd, (母亲节母亲的背影图片struct sockaddr *) &(s_tcp_client.rver_socket_addr),sizeof(struct sockaddr)) != 0){//if connect error reconnect after 5 condsperror("connect");printf("***reconnect after 5s***\n");sleep(5);printf("***reconnect...***\n");}printf("***connected***\n");#if 1//test message nd    sleep(1);char test_msg[] = "this is from client";nd(s_tcp_client.socket_fd, test_msg ,sizeof(test_msg),0);#endifreturn 0;} /* * 接收socket数据函数. * client_fd - 客户端连接的socket。 */static int receive_packet(int client_fd){unsigned char buf[tcp_buffer_size];intrecvbytes;while(1){/*接收*/bzero(buf,sizeof(buf)); recvbytes = recv(client_fd,buf,tcp_buffer_size,0);printf("receive %d bytes\n",recvbytes); if (recvbytes <= 0){//receive error or disconnectedperror("recv");/*reconnect to rver*/printf("clo socket id = %d\n", s_tcp_client.socket_fd);clo(s_tcp_client.socket_fd);//关闭通道s_tcp_client.socket_fd = socket(af_inet, sock_stream, 0);if (s_tcp_client.socket_fd == -1){                printf("###socket init error###\n");perror("socket");return -1;}printf("new socket id = %d\n", s_tcp_client.socket_fd);if (connect_to_rver() != 0){printf("###connect_to_rver error###\n");return -1;}}el{//receive success#if 1//testint i;printf("***get:\n");for (i = 0; i < recvbytes; i++){printf("0x%02x  %c\n", *(buf+i), *(buf+i));}#endif}}return 0;}/*tcp clinet nd thread*/int tcp_nd_start(void){    char buf[100];    uint32_t i = 0;    while(1)    {        sleep(5);        memt(buf,0,sizeof(buf));        sprintf(buf,"***nd data:hello world : %d\n",i);        i++;        printf("%s",buf);        nd(s_tcp_client.socket_fd, buf ,strlen(buf),0);    }}/*tcp clinet running function*/int tcp_client_start(void){    pthread_t tcp_nd_thread_id;//返回的线程值printf("***connect to %s:%d....***\n", s_tcp_client.rver_ip,s_tcp_client.rver_port);/*connect to rver*/if (connect_to_rver() != 0){printf("###connect_to_rver error###\n");return -1;}        //create thread for tcp ndpthread_create(&tcp_nd_thread_id, null, (void *)tcp_nd_start, null);receive_packet(s_tcp_client.socket_fd);return 0;} /* * tcp_client initialize function * port_num -  tcp rver port number * rver_ip - tcp rver ip * */int tcp_client_init(unsigned short port_num, char *rver_ip){int res;struct in_addr test_addr;/*initialize variable*/if (port_num > 0){s_tcp_client.rver_port = port_num;}el{printf("###invalid tcp rver port:%d###\n", port_num);return -1;} if (rver_ip == null){printf("###rver_ip cannot be null###\n");return -1;}el{strcpy(s_tcp_client.rver_ip, rver_ip);绥靖主义//record ip}    printf("rver ip is: %s\r\n",s_tcp_client.rver_ip); /*建立socket描述符*/if ((s_tcp_client.socket_fd = socket(af_inet, sock_stream, 0)) == -1){perror("socket");return -1;}printf("socket id = %d\n", s_tcp_client.socket_fd); /* * 填充服务器sockaddr结构 */bzero(&(s_tcp_client.rver_socket_addr), si无憾青春zeof(struct sockaddr_in));//memts_tcp_client.rver_socket_addr.sin_family = af_inet;inet_pton(af_inet, s_tcp_client.rver_ip, &(s_tcp_client.rver_socket_addr.sin_addr.s_addr));//把ip地址转化为用于网络传输的二进制数值s_tcp_client.rver_socket_addr.sin_port= htons(s_tcp_client.rver_port);//将主机字节顺序转为网络字节顺序bzero(&(s_tcp_client.rver_socket_addr.sin_zero), 8); return 0;} int main(int argc, char *argv[]){int i = 0;int res;pthread_t tcp_thread_id;//返回的线程值char tcp_rver_port[256];char tcp_rver_ip[256];    printf("enter main\r\n");if (argc >= 2){if (strcmp(argv[1],"-v") == 0){printf("net_control_client v1.0\n");return 0;}el if (strcmp(argv[1],"-h") == 0){printf("-v for version\n");printf("-h for help\n");printf("tcp_connect <ip> <port>\n");return 0;}el if (strcmp(argv[1], "tcp_connect") == 0){/*tcp demo*/strcpy(tcp_rver_ip, argv[2]);strcpy(tcp_rver_port, argv[3]); /*initialize functions*/res = tcp_client_init((unsigned short)atoi(tcp_rver_port), tcp_rver_ip);//t tcp clinet ttingif (res == -1){printf("###tcp_rver_init error###\n");return -1;} //create thread for tcp communicationpthread_create(&tcp_thread_id, null, (void *)tcp_client_start, null);}el{printf("unknown argument %s\n",argv[1]);return -1;}}    el{        printf("plea input ip and port\r\n");        exit(0);    }while(1){if (i < 100){i++;}el{i = 0;}sleep(1);}exit(0);}

.h文件

#ifndef net_control_clinet_h_#define net_control_clinet_h_ #include <arpa/inet.h> #define tcp_buffer_size1024//max buff of receive buffer for tcp typedef struct{//tcp client classunsigned short  rver_port;char rver_ip[64];int socket_fd;//socketstruct sockaddr_in rver_socket_addr;}ts_tcp_client;  #endif /* net_control_clinet_h_ */

配置编译任务

进入 ‘main.c’文件,然后点击菜单栏 终端->配置默认生成任务,系统会自动建立一个task.json文件,如下

{"version": "2.0.0","tasks": [{"type": "cppbuild","label": "c/c++: gcc 生成活动文件","command": "/usr/bin/gcc","args": ["-fdiagnostics-color=always","-g","${file}","-o","${filedirname}/${filebanamenoextension}","-lpthread"],"options": {"cwd": "${filedirname}"},"problemmatcher": ["$gcc"],"group": {"kind": "build","isdefault": true},"detail": "编译器: /usr/bin/gcc"}]}

task.json 用来告诉vscode 怎么去编译源文件main.c

lablel 定义该任务的名称,后续调试会根据该名称调用本任务,这里名为c/c++: gcc 生成活动文args :编译时的参数,比如假设程序依赖 pthread库,,那么在这里指定,这里和手动在命令行输入 gcc xxx 命令相同cwd:指定当前运行路径 配置调试程序

仍然保证 编辑区打开的是main.c文件,然后点击菜单栏 运行->添加配置,系统会自动建立一个launch.json文件,如下

{    // 使用 intellin 了解相关属性。     // 悬停以查看现有属性的描述。    // 欲了解更多信息,请访问: /d/file/titlepic/debugging#_launch-configurations    "version": "0.2.0",    "configurations": [        {            "name": "gcc - 生成和调试活动文件",            "type": "cppdbg",            "request": "launch",            "program": "${filedirname}/${filebanamenoextension}",            "args": ["tcp_connect","192.168.1.201","8888"],//调试时传递给程序的命令行参数             "stopatentry": true,//是否停留在main函数            "cwd": "${filedirname}",//调试程序时的工作目录             "environment": [],//环境变量             "externalconsole": fal,//调试时是否显示控制台窗口             "mimode": "gdb",//指定连接的调试器,可以为gdb或lldb             "tupcommands": [            老罗英语    {                    "description": "为 gdb 启用整齐打印",                    "text": "-enable-pretty-printing",                    "ignorefailures": true                }            ],            "prelaunchtask": "c/c++: gcc 生成活动文件",//调试开始前执行的任务,一般为编译程序 对应 tasks.json中的label            "midebuggerpath": "/usr/bin/gdb"        }    ]} 
默认内容和我贴出来的略有不同,我增加了一些参数launch.json 文件用来告诉vscode怎么调用gdb去调试,指定了一些参数,prelaunchtask:调试开始前执行的任务,一般为编译程序 对应 tasks.json中的labelstopatentry”: true,//是否停留在main函数“args”: [“tcp_connect”,“192.168.1.201”,“8888”],//调试时传递给程序的命令行参数 比如我调试这个函数需要在命令行输入,也就起跑线上是标准输入 输入一些参数,那么填在这里

开始编译

仍然保证 编辑区打开的是main.c文件,

点击菜单栏终端->运行生成任务 或者 ctrl+shift+b

开始调试

打开 vscode 侧边栏 选择调试标签

观察 绿色三角箭头 右侧的名称,与launch.json中name`一致。

点击绿色箭头,或者按f5 进入调试。

注意

在生成配置,或者调试时,一定要保证当前编辑器打开的是待调试的c文件,而不是新建的launch.json或者tasks.json

参考资料

https://code.visualstudio.com/docs/cpp/config-linux

到此这篇关于vscode搭建远程c开发环境的文章就介绍到这了,更多相关vscode搭建远程内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-03 23:15:04,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/873e5bfeb18abaeb556006439797435a.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Vscode搭建远程c开发环境的图文教程.doc

本文 PDF 下载地址:Vscode搭建远程c开发环境的图文教程.pdf

标签:文件   的是   参数   函数
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图