samedi 23 octobre 2021

C++ MakeFile compilation problem: undefined reference to function [duplicate]

I have the following test case:

A.h

#ifndef A_H
#define A_H

#include <array>
#include <vector>

class A
{
    public:
        A();        
        virtual ~A(){} ;

    protected:
        template <typename U>
        U getFoo(int a);
};

#endif 

A.cpp

#include "A.h"

A::A()
{}

template <typename U>
U A::getFoo(int a)
{
    U v{a};
    return v;
}

b.h

#ifndef B_H
#define B_H

#include "A.h"

typedef std::array<double, 3> v3;

template< typename T>
class B
:
    public A
{
    public:
        B();
        virtual ~B(){} ;
        
        template <typename U>
        U foo(int a);
};

#endif 

B.cpp

#include "B.h"

template< typename T>
B<T>::B()
{}

template <>
template <>
v3 B<std::vector<v3>>::foo(int a)
{
    return this->getFoo<v3>(a);
}

and main.cpp

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

int main()
{
    std::cout << "StackOverFlow" << std::endl;    
    return 0;
}

My Makefile is:

CXX = g++
CXXFLAGS    := -std=c++11 -g
LFLAGS = 
OUTPUT  := run
SRC_DIR := src
OBJ_DIR := obj
BIN_DIR := bin
MAIN    := bin/main

FIXPATH = $1
RM = rm -f
MD  := mkdir -p

SRC_FILES := $(wildcard $(SRC_DIR)/*.cpp)
OBJ_FILES := $(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES))

all: $(OUTPUT) $(MAIN)
    @echo Executing 'all' complete!

$(OUTPUT):
    $(MD) $(OUTPUT)

$(MAIN): $(OBJ_FILES) 
    $(CXX) $(CXXFLAGS) -o  $@ $^  $(LFLAGS) 

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
    $(CXX) $(CXXFLAGS) -c  -o $@ $< 

.PHONY: clean
clean:
    $(RM) $(MAIN)
    $(RM) $(call FIXPATH,$(OBJ_FILES))
    @echo Cleanup complete!

runs: all
    ./$(OUTPUTMAIN)
    @echo Executing 'run: all' complete!

When compiling the code above, I get the error:

obj/B.o: In function `std::array<double, 3ul> B<std::vector<std::array<double, 3ul>, std::allocator<std::array<double, 3ul> > > >::foo<std::array<double, 3ul> >(int)':
/home/pc/Desktop/Projects/src/B.cpp:13: undefined reference to `std::array<double, 3ul> A::getFoo<std::array<double, 3ul> >(int)'

This must be some sort of linking error. I am very new to using makefiles for compiling c++ scode and would appreciate the help in identifying what am I missing!

Sorry for the long post!

Best Regards.

Aucun commentaire:

Enregistrer un commentaire