0

Consider the following code.

** Array.cc **

#include "Array.h"

Array::Array() {}

Array.h

#ifndef __ARRAY__
#define __ARRAY__

struct Array {
  Array();

  auto Func();
};

#endif

a.cpp

#include "Array.h"

int main() {
  Array array;
  array.Func();
}

And built with the following commands, using GCC 11.1.0 -std=c++20.

g++ -c -o Array.o Array.cc
ar rcs libarray.a Array.o
g++ -c -o a.o a.cpp
g++ -o a.out libarray.a a.o

I get the following error:

a.cpp:(.text+0x1b): undefined reference to `Array::Array()'

Why isn't Array::Array() from libarray.a being picked up?

I tried bypassing the library and built it directly.

g++ -o a.out Array.o a.o

That solves the undefined reference problem but now I get a multiple definition error instead.

a.cpp:(.text+0x0): multiple definition of `Array::Func()'; Array.o:Array.cc:(.text+0x0): first defined here

I guess because the function can be deduced, it needs to be inline even though it's also a template function.

dromodel
  • 9,581
  • 12
  • 47
  • 65

0 Answers0