【工具使用】clang++踩坑记录

问题解决过程

起源是学习Makefile时,尝试编译一直编译不起来

相关代码👉在这里

make是一只报错找不到库:

这里用clang++编译,尝试用g++编译,能够通过,排除没有iostream的问题

考虑是否为Makefile的问题,尝试用clang++直接编译CPP文件,依然不通过,考虑为clang++配置的问题

这就需要考虑进行vscode 的一些配置了,操作后的配置文件在后面附上(不一定通用,在当前状态运行通过)

最后由tasks.json给的提示,给clang++指定一个参数:"--target=x86_64-w64-mingw"

尝试直接在CPP文件上操作,通过

那么将这条参数修改到Makefile中,这里注意不能直接加到$(CXX) -o $@ $(objects)里面,需要再定义一个CXXFLAGS:CXXFLAGS := --target=x86_64-w64-windows-gnu

通过!~~

附录:配置文件

  • c_cpp_properties.json

    c 复制代码
    //c_cpp_properties.json
    {
        "configurations": [
            {
                "name": "g++",
                "intelliSenseMode": "clang-x64",
                "compilerPath": "D:/ProgramFiles/LLVM/bin/g++.exe",
                "includePath": [
                    "${workspaceFolder}"
                ],
                "defines": [],
                "browse": {
                    "path": [
                        "${workspaceFolder}"
                    ],
                    "limitSymbolsToIncludedHeaders": true,
                    "databaseFilename": ""
                },
                "cStandard": "c11",
                "cppStandard": "c++17"
            }
        ],
        "version": 4
    }
  • tasks.json

    json 复制代码
    	// tasks.json
    {
        "version": "2.0.0",
        "command": "clang++", // 要使用的编译器
        "args": [ // 编译命令参数
            "${file}", //要编译的文件名,你也可以改成 *.cpp 表示编译当前目录所有的cpp文件
            "-o", //指定生成的程序名字
            "${file}.exe", //这是你要生成的程序名字
            "-Wall", // 开启额外警告
            "-g", // 生成和调试有关的信息
            "-static-libgcc", // 静态链接
            "-fcolor-diagnostics", //彩色信息
            "-w", //屏蔽警告
            "--target=x86_64-w64-mingw", // 默认target为msvc,不加这一条就会找不到头文件
            //以下都是链接库参数,需要链接什么库就加在这
            "-lws2_32",
            "-lIphlpapi",
            "-lgdi32"
        ],
        "tasks": [
            {
                "label": "clang++", // 任务名称,与launch.json的preLaunchTask相对应
                "type": "shell",
                "group": {
                    "kind": "build",
                    "isDefault": true // 设为false可做到一个tasks.json配置多个编译指令,需要自己修改本文件,我这里不多提
                },
                "presentation": {
                    "echo": true,
                    "reveal": "always", // 在"终端"中显示编译信息的策略,可以为always,silent,never。具体参见VSC的文档
                    "focus": false, // 设为true后可以使执行task时焦点聚集在终端,但对编译c和c++来说,设为true没有意义
                    "panel": "shared" // 不同的文件的编译信息共享一个终端面板
                }
            }
        ],
        "problemMatcher": {
            "owner": "c",
            "fileLocation": [
                "relative",
                "${workspaceRoot}"
            ],
            "pattern": {
                "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                "file": 1,
                "line": 2,
                "column": 3,
                "severity": 4,
                "message": 5
            }
        }
    }
  • launch.json

    json 复制代码
    // launch.json
    {
        // 使用 IntelliSense 了解相关属性。 
        // 悬停以查看现有属性的描述。
        // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
                "type": "cppdbg", // 配置类型,这里只能为cppdbg
                "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
                "program": "${file}.exe", // 将要进行调试的程序的路径
                "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
                "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,我一般设置为true
                "cwd": "${workspaceRoot}", // 调试程序时的工作目录
                "targetArchitecture": "x86_64", // 生成目标架构,一般为x86或x64,可以为x86, arm, arm64, mips, x64, amd64, x86_64
                "externalConsole": true,
                "internalConsoleOptions": "neverOpen",
                "MIMode": "gdb", //调试器名称
                "miDebuggerPath": "D:\\ProgramFiles\\LLVM\\bin\\gdb.exe", //调试器路径
                "preLaunchTask": "clang++", //和tasks.json的label值要相同
                "setupCommands": [
                    {
                        "description": "为 gdb 启用整齐打印",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            }
        ]
    }
  • settings.json

    json 复制代码
    // settings.json
    {
        "files.defaultLanguage": "cpp", // ctrl+N新建文件后默认的语言
        "editor.formatOnType": true, // 输入时就进行格式化,默认触发字符较少,分号可以触发
        "editor.snippetSuggestions": "top", // snippets代码优先显示补全
        "code-runner.runInTerminal": true, // 设置成false会在"输出"中输出,无法输入
        "code-runner.executorMap": {
            "c": "cd $dir && clang $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics -lws2_32 -liphlpapi -lgdi32 -w --target=x86_64-w64-mingw && $dir$fileNameWithoutExt",
            "cpp": "cd $dir && clang++ $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics -lws2_32 -liphlpapi -lgdi32 -w --target=x86_64-w64-mingw && $dir$fileNameWithoutExt"
        }, // 设置code runner的命令行,点击右上角的运行跑的就是这些代码,里面的参数啥意思看tasks.json
        "code-runner.saveFileBeforeRun": true, // run code前保存
        "code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
        "code-runner.clearPreviousOutput": true, // 每次run code前清空属于code runner的终端消息
        "C_Cpp.clang_format_sortIncludes": false, // 格式化时调整include的顺序(按字母排序),这个别开,不信以后遇到问题你就会来关了
        "C_Cpp.intelliSenseEngine": "Default", // 可以为Default或Tag Parser,后者较老,功能较简单。具体差别参考cpptools扩展文档
        "C_Cpp.errorSquiggles": "Disabled", // 因为有clang的lint,所以关掉
        "C_Cpp.autocomplete": "Disabled", // 因为有clang的补全,所以关掉
        "clang.completion.enable": true,
        "C_Cpp.dimInactiveRegions": false,
        "clang.cflags": [ // 控制c语言静态检测的参数
            "--target=x86_64-w64-mingw",
            "-std=c11",
            "-Wall"
        ],
        "clang.cxxflags": [ // 控制c++静态检测时的参数
            "--target=x86_64-w64-mingw",
            "-std=c++17",
            "-Wall"
        ],
        "files.associations": {
            "ostream": "cpp",
            "iostream": "cpp",
            "array": "cpp",
            "atomic": "cpp",
            "*.tcc": "cpp",
            "cctype": "cpp",
            "clocale": "cpp",
            "cmath": "cpp",
            "cstdarg": "cpp",
            "cstddef": "cpp",
            "cstdint": "cpp",
            "cstdio": "cpp",
            "cstdlib": "cpp",
            "cstring": "cpp",
            "cwchar": "cpp",
            "cwctype": "cpp",
            "deque": "cpp",
            "unordered_map": "cpp",
            "vector": "cpp",
            "exception": "cpp",
            "algorithm": "cpp",
            "memory": "cpp",
            "memory_resource": "cpp",
            "optional": "cpp",
            "string": "cpp",
            "string_view": "cpp",
            "system_error": "cpp",
            "tuple": "cpp",
            "type_traits": "cpp",
            "utility": "cpp",
            "fstream": "cpp",
            "initializer_list": "cpp",
            "iosfwd": "cpp",
            "istream": "cpp",
            "limits": "cpp",
            "new": "cpp",
            "sstream": "cpp",
            "stdexcept": "cpp",
            "streambuf": "cpp",
            "typeinfo": "cpp",
            "chrono": "cpp",
            "thread": "cpp",
            "winsock2.h": "c",
            "ws2tcpip.h": "c",
            "windows.h": "c",
            "stdio.h": "c",
            "ctime": "cpp",
            "iomanip": "cpp"
        } // 效果效果比cpptools要好
    }
相关推荐
冰红茶兑滴水1 小时前
Linux 线程控制
linux·c++·算法
c语言鹌鹑蛋1 小时前
C++进阶 --- 多继承中的虚表问题
开发语言·c++
MengYiKeNan2 小时前
C++二分函数lower_bound和upper_bound的用法
开发语言·c++·算法
小林熬夜学编程3 小时前
C++第五十一弹---IO流实战:高效文件读写与格式化输出
c语言·开发语言·c++·算法
月夕花晨3743 小时前
C++学习笔记(30)
c++·笔记·学习
蠢蠢的打码3 小时前
8584 循环队列的基本操作
数据结构·c++·算法·链表·图论
不是编程家3 小时前
C++ 第三讲:内存管理
java·开发语言·c++
jianglq3 小时前
C++高性能线性代数库Armadillo入门
c++·线性代数
Lenyiin5 小时前
《 C++ 修炼全景指南:十 》自平衡的艺术:深入了解 AVL 树的核心原理与实现
数据结构·c++·stl
程序猿练习生5 小时前
C++速通LeetCode中等第5题-无重复字符的最长字串
开发语言·c++·leetcode