问题
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