0

I have my 3 files for a sample C++ code.

When I am trying to compile using the below command, getting and error:

Compile Command:

g++ -W .\main.cpp -o hello

ERROR :

main.cpp:(.text+0x1c): undefined reference to `sample::func2()'
collect2.exe: error: ld returned 1 exit status

It works totally fine when I mention the files in the compile command:

g++ -W .\main.cpp sample.h sample.cpp -o hello

FILE :SAMPLE.H

#ifndef POSIX_SAMPLE_CLASS
#define POSIX_SAMPLE_CLASS

class sample
{
    public:
        void func1();
        void func2();
        static void* func3(void* ptr);
        static int n1;
        static int check1;

    private:
        int common_variable{0};
        

};
#endif // MACRO

FILE : SAMPLE.CPP

#include "sample.h"
#include <iostream>
#include <unistd.h>

using std::cout;
using std::cin;
using std::endl;

void sample::func1()
{
    cout <<"FUNC1.1 : Sample function called\n";
    sample::n1 = 123;
}

void sample::func2()
{
    cout <<"FUNC2.1 :Value for cv from func2 is" << common_variable<<endl;
    func1();
    cout <<"FUNC2.2 :Value for cv from func2 is" << common_variable<<endl;
}

/*
void* sample::func3(void* ptr)
{
    cout <<"FUNC3.1 :Sample function3 called\n";
    sleep(10);
    cout << "FUNC3.3 : Waking up after Sleep";
    cout <<"Changing value for check1" << check1;
    check1 = 22;
    return NULL;
}
*/

int sample::n1 = 12;
int sample::check1 = 0;

FILE :Main.cpp

#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include "sample.h"

using std::cin;
using std::cout;
using std::endl;

/*
class s2
{
    public:
    void check()
    {
        cout <<"class check\n";
    }
};
*/

int main()
{
    sample s1;
    s1.func2();
    return 0;
}

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:/Program Files/mingw-w64/mingw64/bin/g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "-Wall",
                "-std=c++17",
                "${fileDirname}\\*.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:/Program Files/mingw-w64/mingw64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: \"C:/Program Files/mingw-w64/mingw64/bin/g++.exe\""
        }
    ]
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • If `g++ -W .\main.cpp -o hello` is your compile command VSCode is not using your `tasks.json` do you have code runner enabled? – drescherjm Apr 03 '22 at 14:31
  • Your `tasks.json` file indicates that only a single cpp file is used to produce the executable, so either the `main` function or the class implementation is missing depending on which file you try to run... – fabian Apr 03 '22 at 14:32
  • `"${fileDirname}\\*.cpp",` should have compiled both files. I don't think the tasks.json is used. – drescherjm Apr 03 '22 at 14:33
  • 1
    Re: `g++ -W .\main.cpp sample.h sample.cpp -o hello` -- don't compile sample.h. That's a bunch of notes for the two CPP files to use. It's the CPP files that matter. – Pete Becker Apr 03 '22 at 14:37

2 Answers2

1

g++ -W .\main.cpp -o hello does not compile sample.cpp.

You need to compile each translation unit and then combine them in your final executable:

g++ -Wall -Wextra -Werror -c sample.cpp -o sample.o
g++ -Wall -Wextra -Werror -c main.cpp -o main.o
g++ sample.o main.o -o hello

(additional flags added as advise)

YSC
  • 38,212
  • 9
  • 96
  • 149
0

I found an easier way to perform the tasks.

Select the Main.cpp. Select the terminal option and select/perform RUN_BUILD_TASKS. This takes care of the linking and compiling and generates the main.exe.