Visual Studio Code clang error: linker command failed with exit code 1 on Mac

限于喜欢 提交于 2021-01-27 19:10:00

问题


I'm new to programming and wanted to try out VS Code for C++ development. I'm getting this error and I can't find a solution online how to fix:

clang: error: linker command failed with exit code 1 (use -v to see invocation) The terminal process terminated with exit code: 1

I got a cpp file with the function definitions and a header file with the class and declarations in it and also a int main test file.

So its a linker issue. VSC directed me to c_cpp_properties.json and I have no idea what to do next to fix it. I'm also on Mac btw.

Can anyone help me with this?


回答1:


I think I found my answer!

Seems like I was looking in the wrong place the whole time. You're suppose to edit the task.json option where it asks for "command" then add every single translational unit or cpp file name you're using. Like mine would be

"g++ -g main.cpp func.cpp -o main"

This tells the compiler to compile both the main.cpp and func.cpp file and then allows the linker to do its job.




回答2:


This will build all .cpp files in your current folder, here is sample settings in VS Code task.json "${fileDirname}/*.cpp". please find doc here.

    "tasks": [
    {
        "type": "shell",
        "label": "clang++ build active file",
        "command": "/usr/bin/clang++",
        "args": [
            "-g",
            "${fileDirname}/*.cpp",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],


来源:https://stackoverflow.com/questions/50900732/visual-studio-code-clang-error-linker-command-failed-with-exit-code-1-on-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!