VSCode开发gsoap项目中解决undefined reference

前提

使用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)即可编译启动项目,通过控制台看到输出
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值