Error building project

g++ gives me an error when I try to build this project and I could not menage to find out what the problem.
It's a lot code but it is a linking problem so I don't think it is necessary actually read the code and to understand it.
I have these files:

bit_utils.h
https://pastebin.com/s6daXSRm
bit_utils.cpp
https://pastebin.com/cttDuaiu

byte_dict.h
https://pastebin.com/80s0dXpY
byte_dict.cpp
https://pastebin.com/9Qd1ACwZ

test.cpp
https://pastebin.com/yygeybEt

I compile them with
g++ -std=c++11 -c bit_utils.cpp
g++ -std=c++11 -c byte_dict.cpp
g++ -std=c++11 -c test.cpp

when I try to link them
g++ -std=c++11 test.o bit_utils.o byte_dict.o

I get this error:
byte_dict.o:byte_dict.cpp:(.text+0x86): undefined reference to `unsigned char get_byte<__gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char, std::allocator<unsigned char> > > >(__gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char, std::allocator<unsigned char> > >, int)'
collect2.exe: error: ld returned 1 exit status

Anyone can help me understand why?
Last edited on
Undefined reference can also mean that you're using functions/classes that it didn't find definitions for (via header files).

In byte_dict.cpp, you use an std::vector
std::vector<int> tempv;

Try adding #include <vector> at the top of byte_dict.cpp and see if the error goes away or changes.

-std=c++1
What is this?
Do you mean -std=c++11? The latest versions of g++ do not require you to say -std=c++11, they are already C++11 (C++14 currently) by default.
Last edited on
it's c++11, that was a typo, I'm on windows and it's not default to 11, I tried to include vector but it didn't work, even because vector is included indirectly through the byte_dict.h header.
My mistake, I didn't see the indirect include.

Oh oh oh. Your problem is templates. I'm 99% sure. You can't separate the declaration of a template from the implementation like that. Yeah templates can be a *****.

An alternative to including the template implementation inside the header file itself is to use "inline" template files.

Basically, put the template implementation code inside a "my_templates_definitions.inl" file, and then #include "my_template_definitions.inl" inside bit_utils.h, after you list the template function declarations.

https://stackoverflow.com/questions/115703/storing-c-template-function-definitions-in-a-cpp-file
Last edited on
Hello LudBee,

I would say the the include guards are working the way they should. Where by a ".cpp" file, by your OP "bit_utils.cpp" was compiled fist, causing the include guard to define "BIT_UTILS". Then when you compile the file that does need the "#include <vector>" it is not put in that ".cpp" because of the include guard.

I would suggest that you take the include files like "vector" out of the ".h" file where they do not belong and put them in the ".cpp" files that need those header files where they do belong.

The other option would be to try a different order when you compile the files and see if that helps.

If you put header files like "iostream" and "vector" in more than one ".cpp" file that is OK because they will only be included once at compile time.

Hope that helps,

Andy
Ganado is right.
Really thank you, this thing with template is pretty nasty, I didn't know it.
I resolved instantiating the template function in the cpp with the types I need.
Andy I don't think it works that way, every file is compiled separately, so the define flag are raised separately and correctly for every file so that everything is included. Then the object file are linked.
Last edited on
Topic archived. No new replies allowed.