前提
使用Win10,Mingw64,VSCode作为开发工具
具体代码生成参考使用gSOAP生成Web Services框架代码,本文不再详述
项目最终结构
|--.vscode
|--|--c_cpp_properties.json # 配置引入依赖
|--|--launch.json # 配置启动
|--|--settings.json # 全局配置
|--|--tasks.json # 配置任务
|--main.c # 程序主入口
|--MobileCodeWSSoap.nsmap
|--soapC.c
|--soapClient.c
|--soapH.h
|--soapStub.h
|--stdsoap2.c
|--stdsoap2.h
undefined reference解决
核心在于依赖问题,VSCode只是本质只是一个编辑器,无法帮你自动绑定所有依赖,需要使用json来指定各项依赖,详见下面流程中的task.json,有解决方式
VSCode编译流程
- 创建.vscode文件夹并进入,用于配置工程
- 创建c_cpp_properties.json,用于配置引入依赖
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**" # 核心,有需求则添加
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "D:/opensource/visual studio/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
- 创建launch.json,用于启动
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gsoap-MobileCode) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": ["1827746873"], # 输入手机参数
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\opensource\\mingw64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Compile" // 调试前执行的任务,对应tasks.json
}
]
}
- 创建settings.json,全局配置
{
"files.associations": {
"soapstub.h": "c",
"stdlib.h": "c",
"stdio.h": "c",
"stdsoap2.h": "c"
},
"C_Cpp.errorSquiggles": "Disabled"
}
- 创建tasks.json,用于编译定义,有三个要点:
- label名称需对应launch.json
- 须在-g后加上所有依赖的.c文件
- 加上-lwsock32表明使用wsock32库文件,即windows的socket库
{
"tasks": [
{
"type": "cppbuild",
"label": "Compile", # 对应launch.json
"command": "D:\\opensource\\mingw64\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"${fileDirname}\\stdsoap2.c",
"${fileDirname}\\soapC.c",
"${fileDirname}\\soapClient.c",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-lwsock32"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
- 最终对main.c使用RUN AND DEBUG(Ctrl+Shift+D)即可编译启动项目,通过控制台看到输出