In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the process of building a remote c development environment for Vscode. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
Basic environment
Target machine: ubuntu20
Development machine: win10, mac installation
Remote development kit
Both the local and target machines need to be installed.
Refer to the vscode secret-free login server to edit the configuration server information and use vscode to open a directory on the remote computer.
Create c file # include # include # include / / system function # include # include / / Multi-thread library # include "net_control_client.h" ts_tcp_client s_tcp_client Static int connect_to_server (void) {while (connect (s_tcp_client.socket_fd, (struct sockaddr *) & (s_tcp_client.server_socket_addr), sizeof (struct sockaddr))! = 0) {/ / if connect error reconnect after 5 seconds perror ("connect"); printf ("* * reconnect after 5slots *\ n") Sleep (5); printf ("* reconnect...***\ n");} printf ("* connected***\ n"); # if 1//test message send sleep (1); char test_msg [] = "This is from client"; send (s_tcp_client.socket_fd, test_msg, sizeof (test_msg), 0) # endif return 0;} / * * receives socket data functions. * client_fd-socket of the client connection. * / static int receive_packet (int client_fd) {unsigned char buf [BUFFER_SIZE]; int recvbytes; while (1) {/ * receive * / bzero (buf,sizeof (buf)); recvbytes = recv (client_fd,buf,TCP_BUFFER_SIZE,0) Printf ("Receive% d bytes\ n", recvbytes); if (recvbytes 0) {s_tcp_client.server_port = port_num;} else {printf ("# invalid tcp server port:%d###\ n", port_num); return-1 } if (server_ip = = NULL) {printf ("# server_ip cannot be NULL###\ n"); return-1;} else {strcpy (s_tcp_client.server_ip, server_ip); / / record ip} printf ("server ip is:% s\ r\ n", s_tcp_client.server_ip) / * create the socket descriptor * / 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) / * * populate the server sockaddr structure * / bzero (& (s_tcp_client.server_socket_addr), sizeof (struct sockaddr_in)); / / memset s_tcp_client.server_socket_addr.sin_family = AF_INET Inet_pton (AF_INET, s_tcp_client.server_ip, & (s_tcp_client.server_socket_addr.sin_addr.s_addr)); / / convert the ip address to a binary value for network transmission s_tcp_client.server_socket_addr.sin_port = htons (s_tcp_client.server_port) / / convert host byte order to network byte order bzero (& (s_tcp_client.server_socket_addr.sin_zero), 8); return 0;} int main (int argc, char * argv []) {int I = 0; thread value returned by int res; pthread_t tcp_thread_id;// char tcp_server_port [256]; char tcp_server_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 } else if (strcmp (argv [1], "- h") = = 0) {printf ("- v for version\ n"); printf ("- h for help\ n"); printf ("tcp_connect\ n"); return 0 } else if (strcmp (argv [1], "tcp_connect") = = 0) {/ * tcp demo*/ strcpy (tcp_server_ip, argv [2]); strcpy (tcp_server_port, argv [3]) / * initialize functions*/ res = tcp_client_init ((unsigned short) atoi (tcp_server_port), tcp_server_ip); / / set tcp clinet setting if (res =-1) {printf ("# tcp_server_init error###\ n") Return-1;} / create thread for TCP communication pthread_create (& tcp_thread_id, NULL, (void *) tcp_client_start, NULL);} else {printf ("Unknown argument% s\ n", argv [1]) Return-1;}} else {printf ("please input ip and port\ r\ n"); exit (0);} while (1) {if (I)
< 100){ i++; }else{ i = 0; } sleep(1); } exit(0);} .h文件 #ifndef NET_CONTROL_CLINET_H_#define NET_CONTROL_CLINET_H_ #include #define TCP_BUFFER_SIZE 1024//max buff of receive buffer for tcp typedef struct{//tcp client class unsigned short server_port; char server_ip[64]; int socket_fd;//socket struct sockaddr_in server_socket_addr;}ts_tcp_client; #endif /* NET_CONTROL_CLINET_H_ */配置编译任务 进入 'main.c'文件,然后点击菜单栏 终端->Configure the default build task, and the system will automatically create a task.json file, as follows
{"version": "2.0.0", "tasks": [{"type": "cppbuild", "label": "command Clippers: gcc generates active files", "command": "/ usr/bin/gcc" "args": ["- fdiagnostics-color=always", "- g", "${file}", "- o" "${fileDirname} / ${fileBasenameNoExtension}", "- lpthread"], "options": {"cwd": "${fileDirname}"} "problemMatcher": ["$gcc"], "group": {"kind": "build", "isDefault": true} "detail": "compiler: / usr/bin/gcc"}]}
Task.json is used to tell vscode how to compile the source file main.c
Lablel defines the name of the task, and subsequent debuggers will call this task according to that name. Here, it is called C _ gcc to generate the active text.
Args: compile-time parameters, such as assuming that the program depends on the pthread library, then specify it here, which is the same as manually entering the gcc xxx command on the command line
Cwd: specify the current running path to configure the debugger
Still make sure that the main.c file is open in the editing area, and then click the menu bar to run-> add configuration, and the system will automatically create a launch.json file, as follows
{/ / use IntelliSense to learn about the relevant properties. / / hover to see a description of an existing property. / / for more information Please visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [{"name": "gcc-generate and debug active files", "type": "cppdbg", "request": "launch", "program": "${fileDirname} / ${fileBasenameNoExtension}" "args": ["tcp_connect", "192.168.1.201", "8888"], / / the command line parameter "stopAtEntry" passed to the program when debugging: whether true,// stays in the main function "cwd": "${fileDirname}", / / the working directory "environment" when debugging the program: [] / / environment variable "externalConsole": whether the console window "MIMode": "gdb" is displayed when false,// debugging, / / specify the debugger of the connection Can be gdb or lldb "setupCommands": [{"description": "enable neat printing for gdb", "text": "- enable-pretty-printing", "ignoreFailures": true}] "preLaunchTask": "gcc generated active files", / / tasks executed before debugging starts Generally, the compiler corresponds to the label "miDebuggerPath": "/ usr/bin/gdb"}]} in tasks.json.
The default content is slightly different from what I posted. I added some parameters.
The launch.json file is used to tell vscode how to call gdb to debug, specifying some parameters
PreLaunchTask: the task performed before debugging starts, usually the label in the tasks.json corresponding to the compiler
StopAtEntry ": whether true,// stays in the main function
"args": ["tcp_connect", "192.168.1.201", "8888"], / / the command line parameters passed to the program during debugging, for example, if I debug this function, I need to enter some parameters on the command line, that is, standard input, then fill in here.
Start compilation
Still make sure that the main.c file is open in the editing area
Click the menu bar terminal-> run the build task or ctrl+shift+b
Start debugging
Open the vscode sidebar and select the debug tab
Observe the name to the right of the green triangular arrow, which is the same as the name` in launch.json.
Click the green arrow, or press F5 to enter debugging.
Be careful
When you generate a configuration or debug, make sure that the current editor opens the c file to be debugged, not the new launch.json or tasks.json
The above is the process of building a remote c development environment for Vscode shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.