1

I have a function called MyFunc which is declared like this

void MyFunc(const std::vector<std::uint64_t> &data);

I am calling MyFunc from somewhere else.

void ParentFunc(const std::vector<std::uint64_t> &data)
{
   ....
   MyFunc(data);
   ....
}

However, the above code (where I am calling MyFunc) is resulting in compilation error with a message that reads undefined reference to `MyFunc(std::vector<unsigned long, std::allocator > const&)'

I don't understand why it is looking for an argument of type std::vector<unsigned long, std::allocator >. Where is the std::allocator coming from, and more importantly, what can I do to fix this error?

I am using g++ 7 as compiler.

  • Note that you are not asking how to solve your problem - only `Where is the std::allocator coming from?`. Are you asking XY question? – KamilCuk Jun 06 '21 at 19:54
  • 1
    [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – IWonderWhatThisAPIDoes Jun 06 '21 at 19:55
  • `std::vector` has two template arguments, one of which is defaulting to `std::allocator`, see [cppreference](https://en.cppreference.com/w/cpp/container/vector). – Aconcagua Jun 06 '21 at 19:57
  • @KamilCuk Yes I want to know how to fix the error as well. Should have been more clear about that. Thanks. – supersonic_528 Jun 06 '21 at 19:59
  • I assume the two functions are residing in two different source files? How did you compile these? In most simple case you'd add all the source files to one single command (like `g++ -o programme A.cpp B.cpp`), cleaner would be *only* compiling (GCC: -c flag) them separately and linking them together afterwards. You might want to write your own make file for this purpose (or you use an IDE to help you facilitate compilation). – Aconcagua Jun 06 '21 at 20:06
  • Please share how are you invoking your g++ command, with all compiler flags and command line options. Are you using and IDE? If so, which one? – Emile Cormier Jun 06 '21 at 20:10
  • @Aconcagua Thank you for pointing me in the right direction. Yes they are in different files and it turns out it just cannot find the definition. I was distracted by the difference in the vector parameter, so it didn't occur to me that could actually be the case. I ran an experiment passing no parameter and still got similar error message. I am passing the signature of MyFunc to the other file (that has ParentFunc definition) through an 'include' file, but I'll check what could possibly be going wrong. – supersonic_528 Jun 06 '21 at 20:20
  • `#include`ing just solves the problem that the compiler needs to be aware how functions look like – however it just leaves place holders in the object code for the addresses of the functions. These are completed by the linker. The latter needs to have available the compiled object codes of those files that contain the functions you are in need of. – Aconcagua Jun 06 '21 at 20:29
  • @EmileCormier Unfortunately, the build system is a bit elaborate and I probably can't post it here, but thanks for asking. – supersonic_528 Jun 06 '21 at 20:36
  • @EmileCormier `extern` is default for functions anyway, so that won't help. – Aconcagua Jun 06 '21 at 20:50
  • @Aconcagua I had just looked that up, and you're right of course. `extern` is an old habit I picked up in my early C programming days. – Emile Cormier Jun 06 '21 at 20:55
  • Check if this solves your liker error: https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc – Emile Cormier Jun 06 '21 at 21:01
  • It turns out that I forgot adding the scope resolution operator in front of the function name while defining it in the other file. Not a great feeling to lose 4 hours on a Sunday afternoon on something so trivial :( – supersonic_528 Jun 06 '21 at 23:04

1 Answers1

1

Where is the std::allocator coming from?

std::vector has two template arguments and the second one defaults to std::allocator. From cppreference std::vector it is:

template<
    class T,
    class Allocator = std::allocator<T>
> class vector;

The second template argument is used in case you want to make std::vector allocate memory from your own pool or memory region - in such case, a custom allocator class is written and provided as second template argument.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111