Author Topic: Yet another 'undefined reference'  (Read 5419 times)

Offline adriano91

  • Single posting newcomer
  • *
  • Posts: 4
Yet another 'undefined reference'
« on: April 14, 2011, 12:42:57 pm »
SOLVED: Read the next post

Hi,

I write a simple project in c::b (attached in the post). I've got some 'undefined reference' errors (with list.cpp/list.h library). I know it's connected with linking, but building commands seems to be ok, and I don't have any ideas why it isn't working. I don't use any external libraries (only STL and my libraries).

How to link these files?

Here's build log:
Code: [Select]
-------------- Build: Debug in Warzywniak ---------------

mingw32-g++.exe -Wall -fexceptions  -g     -c C:\Users\Adi\Documents\Warzywniak\main.cpp -o obj\Debug\main.o
mingw32-g++.exe -Wall -fexceptions  -g     -c C:\Users\Adi\Documents\Warzywniak\product.cpp -o obj\Debug\product.o
mingw32-g++.exe -Wall -fexceptions  -g     -c C:\Users\Adi\Documents\Warzywniak\warzywniak.cpp -o obj\Debug\warzywniak.o
mingw32-g++.exe -Wall -fexceptions  -g     -c C:\Users\Adi\Documents\Warzywniak\list.cpp -o obj\Debug\list.o
[b]mingw32-g++.exe  -o bin\Debug\Warzywniak.exe obj\Debug\main.o obj\Debug\product.o obj\Debug\warzywniak.o obj\Debug\list.o    [/b]
obj\Debug\warzywniak.o: In function `Z17warzywniak_createv':
C:/Users/Adi/Documents/Warzywniak/warzywniak.cpp:14: undefined reference to `LinkedList<Product>* list_create<Product>()'
obj\Debug\warzywniak.o: In function `Z5closeP10Warzywniak':
C:/Users/Adi/Documents/Warzywniak/warzywniak.cpp:19: undefined reference to `void list_clear<Product>(LinkedList<Product>*)'
obj\Debug\warzywniak.o: In function `Z11add_productP10Warzywniak':
C:/Users/Adi/Documents/Warzywniak/warzywniak.cpp:36: undefined reference to `ListElement<Product>* list_get_element<Product>(LinkedList<Product>*, int)'
obj\Debug\warzywniak.o: In function `Z12edit_productP10Warzywniak':
C:/Users/Adi/Documents/Warzywniak/warzywniak.cpp:66: undefined reference to `ListElement<Product>* list_get_element<Product>(LinkedList<Product>*, int)'
C:/Users/Adi/Documents/Warzywniak/warzywniak.cpp:68: undefined reference to `void list_element_set_data<Product>(ListElement<Product>*, Product)'
obj\Debug\warzywniak.o: In function `Z12save_to_fileP10Warzywniak':
C:/Users/Adi/Documents/Warzywniak/warzywniak.cpp:144: undefined reference to `ListElement<Product>* list_get_head<Product>(LinkedList<Product>*)'
C:/Users/Adi/Documents/Warzywniak/warzywniak.cpp:146: undefined reference to `Product* list_element_get_data<Product>(ListElement<Product>*)'
obj\Debug\warzywniak.o: In function `Z13show_productsP10Warzywniak':
C:/Users/Adi/Documents/Warzywniak/warzywniak.cpp:162: undefined reference to `ListElement<Product>* list_get_head<Product>(LinkedList<Product>*)'
C:/Users/Adi/Documents/Warzywniak/warzywniak.cpp:166: undefined reference to `Product* list_element_get_data<Product>(ListElement<Product>*)'
obj\Debug\warzywniak.o: In function `Z17add_product_afterP10Warzywniak7ProductP11ListElementIS1_E':
C:/Users/Adi/Documents/Warzywniak/warzywniak.cpp:176: undefined reference to `void list_insert_after<Product>(LinkedList<Product>*, Product, ListElement<Product>*)'
obj\Debug\warzywniak.o: In function `Z18add_product_beforeP10Warzywniak7ProductP11ListElementIS1_E':
C:/Users/Adi/Documents/Warzywniak/warzywniak.cpp:182: undefined reference to `void list_insert_before<Product>(LinkedList<Product>*, Product, ListElement<Product>*)'
obj\Debug\warzywniak.o: In function `Z16sort_asc_or_descP10WarzywniakPFbR7ProductS2_E':
C:/Users/Adi/Documents/Warzywniak/warzywniak.cpp:198: undefined reference to `void list_sort<Product>(LinkedList<Product>*, bool (*)(Product&, Product&), bool)'
C:/Users/Adi/Documents/Warzywniak/warzywniak.cpp:199: undefined reference to `void list_sort<Product>(LinkedList<Product>*, bool (*)(Product&, Product&), bool)'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 3 seconds)
13 errors, 0 warnings

--EDIT--

I've tried to assign priorities (described here http://www.codeblocks.org/docs/main_codeblocks_en3.html#x3-350001.11.14), but it hasn't changed:
Code: [Select]
-------------- Build: Debug in Warzywniak ---------------

mingw32-g++.exe -Wall -fexceptions  -g     -c C:\Users\Adi\Documents\Warzywniak\list.cpp -o obj\Debug\list.o
mingw32-g++.exe -Wall -fexceptions  -g     -c C:\Users\Adi\Documents\Warzywniak\warzywniak.cpp -o obj\Debug\warzywniak.o
mingw32-g++.exe -Wall -fexceptions  -g     -c C:\Users\Adi\Documents\Warzywniak\main.cpp -o obj\Debug\main.o
[b]mingw32-g++.exe  -o bin\Debug\Warzywniak.exe obj\Debug\list.o obj\Debug\product.o obj\Debug\warzywniak.o obj\Debug\main.o  [/b]  
obj\Debug\warzywniak.o: In function `Z17warzywniak_createv':
C:/Users/Adi/Documents/Warzywniak/warzywniak.cpp:14: undefined reference to `LinkedList<Product>* list_create<Product>()'
[...]
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
13 errors, 0 warnings
« Last Edit: April 15, 2011, 08:37:48 pm by adriano91 »

Offline adriano91

  • Single posting newcomer
  • *
  • Posts: 4
Re: Yet another 'undefined reference'
« Reply #1 on: April 15, 2011, 08:36:00 pm »
Well, I found the solution on http://www.cplusplus.com/doc/tutorial/templates/ (last paragraph):
Templates and multiple-file projects
From the point of view of the compiler, templates are not normal functions or classes. They are compiled on demand, meaning that the code of a template function is not compiled until an instantiation with specific template arguments is required. At that moment, when an instantiation is required, the compiler generates a function specifically for those arguments from the template.

When projects grow it is usual to split the code of a program in different source code files. In these cases, the interface and implementation are generally separated. Taking a library of functions as example, the interface generally consists of declarations of the prototypes of all the functions that can be called. These are generally declared in a "header file" with a .h extension, and the implementation (the definition of these functions) is in an independent file with c++ code.

Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.

Since no code is generated until a template is instantiated when required, compilers are prepared to allow the inclusion more than once of the same template file with both declarations and definitions in a project without generating linkage errors.

Adding '#include "list.cpp"' into warzywniak.h file works fine.