1

Following the instructions from this link on configuring c++ with vscode, I set up my tasks.json file to compile multiple files in one directory. However, I always get this error:

/usr/bin/ld: /tmp/ccXey2PG.o: in function `main':
bigone.cpp:(.text+0x9): undefined reference to `swag()'
collect2: error: ld returned 1 exit status

From what I understand, this error basically means that my function definition file swag.cpp isn't getting included in compilation. I've tried the solutiosn from this post, but nothing seems to work.

Also, if I just compile and run the files directly in the terminal through g++ swag.cpp bigone.cpp and ./a.out, everything works fine and dandy.

Any suggestions? I should probably just be using make files, but the fact that I can't figure this out is getting my knickers in a knot. Thanks for the help, as always.

Here's my code:

bigone.cpp:

#include <iostream>
#include "swag.h"

int main()
{
    swag();
    return 0;
}

swag.h:

#pragma once
void swag();

swag.cpp:

#include <iostream>
void swag()
{
    std::cout << "swag" << "\n";
}

And here's my tasks.json file:

{
    "version": "2.0.0",
    "tasks": [
      {
        "type": "shell",
        "label": "g++ build active file",
        "command": "/usr/bin/g++",
        "args": ["-g", "${fileDirname}/*.cpp", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
        "options": {
          "cwd": "/usr/bin"
        },
        "problemMatcher": ["$gcc"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }
4
  • By the time multiple source files come into play, getting yourself a build system is better. cmake with its extension, or the built-in make support. While it's possible to make tasks.json do this, it's hacky. Commented Aug 15, 2021 at 2:03
  • the method sweenish comments is the best way to go, but does the terminal show that g++ is compiling multiple .cpp files Commented Aug 15, 2021 at 2:35
  • Could you clarify what you mean by show? I'm not sure if this counts, but here's what's going on in my terminal: ~/Desktop/software/test$ g++ bigone.cpp swag.cpp ~/Desktop/software/test$ ./a.out swag And I'll look into cmake. Maybe that is just the best route. Commented Aug 15, 2021 at 2:47
  • I have the same problem, it just doesn't work :( Waste a whole day and nobody has the right answer. Commented Feb 24, 2024 at 20:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.