5

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?

Momo N
  • 119
  • 1
  • 1
  • 7

5 Answers5

5

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}"
        ],
Vu Anh
  • 955
  • 1
  • 18
  • 29
Song
  • 593
  • 9
  • 21
3

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.

Momo N
  • 119
  • 1
  • 1
  • 7
0

I know this might sound odd for anyone else, but regardless of the tasks.json or c_cpp_properties.json I couldn't get it to reconcile. Turns out, for some odd reason inside VSCode (I'm on OSx), that when I was trying to compile on a copied version of my Person.h file the linker kept saying:

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

I copied & pasted from Person.h to a new Person1.h and updated the header files inside my max.cpp & Person.cpp, #include "Person1.h"and it worked.

// Wouldn't work with Person.h copied over, but did work once Person1.h was configured
g++ -g -I./Header ./Source/max.cpp ./Source/Person.cpp
kevin_theinfinityfund
  • 1,631
  • 17
  • 18
0

also this way solved it too.

// a.hpp file

#pragma once

class a {

    public:

    a();
    ~a();
};

#include "a.cpp"

// a.cpp file

#include "a.hpp"

a::a() {}

a::~a() {}
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
0

I had this problem too, I had realized that my terminal on VS Code was in the wrong directory, so I clicked -ls to see which directory it was in and in the terminal on VS, I had just changed the directory to the correct one by typing -cd [directory_name] and it ran perfectly fine

suki
  • 1
  • 1