Home > Enterprise >  Undefined Reference Compiler error for functions defined In #include file
Undefined Reference Compiler error for functions defined In #include file

Time:04-04

I have my 3 files for a sample C code. When I am trying to compile using 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\""
        }
    ]
}

CodePudding user response:

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)

CodePudding user response:

You need to link with SAMPLE.CPP.

When you #include a header file you only get the declarations of functions/classes. You need to link with the implementation of them.

  • Related