Code Runner增添执行参数
背景介绍
编写一个本地Web项目,如下。使用Ctrl + F5以及点击三角运行均没有问题,但右键Run Code报错。
源码
#include <stdio.h>
#include <winsock2.h>
// #pragma comment(lib, "ws2_32.lib");
// ctrl+F5 基于项目内置的task json运行。 右键run code则基于插件内置的配置运行
int main(){
SetConsoleOutputCP(65001); // Code-Page
printf("欢迎来到本地服务器!");
WSADATA wsdata;
WSAStartup(MAKEWORD(2, 2), &wsdata);
SOCKET server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in seraddr;
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(80);
seraddr.sin_addr.s_addr = INADDR_ANY;
bind(server, &seraddr, sizeof(seraddr));
listen(server, 5);
struct sockaddr_in claddr;
int cllen = sizeof(claddr);
while(1){
// 监听每一个连接
SOCKET client = accept(server, &claddr, &cllen);
char recvdata[1024];
recv(client, recvdata, 1024, 0);
printf("%s, 接受到的数据大小为%d!\r\n", recvdata, sizeof(recvdata));
char senddata[1024] = "I Love You!";
// send(client, senddata, strlen(senddata), 0);
char *htmlfd = "E:/C++/Native_Server/404.html";
sendhtml(client, htmlfd);
closesocket(client);
}
closesocket(server);
WSACleanup();
// system("pause");
return 0;
}
void sendhtml(SOCKET s, char *filename){
FILE *fd = fopen(filename, "r"); // fopen需要一个字符串,
if(fd == NULL){
printf("文件为空!");
return;
}
char tmp[1024];
do{
fgets(tmp, 1024, fd); // buffer, size, file
send(s, tmp, strlen(tmp), 0);
}while(!feof(fd));
}
报错信息
d:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\27395\AppData\Local\Temp\ccSAx055.o:tempCodeRunnerFile.c:(.text+0x47): undefined reference to `WSAStartup@8'
d:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\27395\AppData\Local\Temp\ccSAx055.o:tempCodeRunnerFile.c:(.text+0x66): undefined reference to `socket@12'
d:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\27395\AppData\Local\Temp\ccSAx055.o:tempCodeRunnerFile.c:(.text+0x81): undefined reference to `htons@4'
d:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\27395\AppData\Local\Temp\ccSAx055.o:tempCodeRunnerFile.c:(.text+0xb2): undefined reference to `bind@12'
d:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\27395\AppData\Local\Temp\ccSAx055.o:tempCodeRunnerFile.c:(.text+0xc8): undefined reference to `listen@8'
d:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\27395\AppData\Local\Temp\ccSAx055.o:tempCodeRunnerFile.c:(.text+0xf4): undefined reference to `accept@12'
d:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\27395\AppData\Local\Temp\ccSAx055.o:tempCodeRunnerFile.c:(.text+0x11f): undefined reference to `recv@16'
d:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\27395\AppData\Local\Temp\ccSAx055.o:tempCodeRunnerFile.c:(.text+0x196): undefined reference to `closesocket@4'
d:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\27395\AppData\Local\Temp\ccSAx055.o:tempCodeRunnerFile.c:(.text+0x21d): undefined reference to `send@16'
collect2.exe: error: ld returned 1 exit status
处理思路&&具体流程
- 根据文章[1]可知,是由于执行时缺少 “-lws2_32”;
- 根据文章[2]可知,Code Runner插件执行代码的超参数,存放在
executorMap 中。
因此,在executorMap中添加"-lws2_32"即可。
在VS Code中依次打开文件、首选项、设置,并搜索Run Code,并编辑Executor Map。
原配置中,C语言的执行参数如下:
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
增加相应的超参数后:
"c": "cd $dir && gcc $fileName -lws2_32 -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
执行结果:
注:Ctrl + F5能执行成功,因为项目子目录下的task.json中的args参数中包含"-lws2_32"(Run Code算是另一套执行过程)。
参考文章
[1] collect2.exe: error: ld returned 1 exit status分析与解决
[2] VSCode下便捷编译运行C++代码——Code Runner配置及说明