0

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
        }
      }
    ]
  }
  • 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. – sweenish Aug 15 '21 at 02:03
  • the method **sweenish** comments is the best way to go, but does the terminal show that g++ is compiling multiple `.cpp` files – rioV8 Aug 15 '21 at 02: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. – epicninja_gamertriceratops Aug 15 '21 at 02:47

0 Answers0