Linker error when dividing source code into multiple files

Y

yatko

Hi all;

I have written a class, and it works fine when all the definitions are
in one main.cpp file. But whenever I divide source code in several
files(main.cpp, class.h, class.cpp), it is compiled with no errors,
but linker gives the following error. I could not find what the
problem is.

Thanks

yatko
----
**** Build of configuration Debug for project test ****

make all
Building target: test
Invoking: GCC C++ Linker
g++ -o"test" ./ProducersNConsumers.o ./main.o -lboost_thread-gcc41-
mt-1_34_1
../main.o: In function `main':
/home/oktay/workspace/test/Debug/../main.cpp:135: undefined reference
to `ProducersNConsumers<int>::producersNConsumers()'
/home/oktay/workspace/test/Debug/../main.cpp:151: undefined reference
to `ProducersNConsumers<int>::~ProducersNConsumers()'
collect2: ld returned 1 exit status
make: *** [test] Error 1
-----

main.cpp
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <boost/thread/thread.hpp>
#include <iostream>

#include "ProducersNConsumers.h"


boost::mutex io_mutex;
const int BUFFER_SIZE = 1024;
const int ITERS = 100;

volatile bool start = false;



int
main()
{
ProducersNConsumers<int> outgoing;



return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------



ProducersNConsumers.h
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
#ifndef PRODUCERSNCONSUMERS_H_
#define PRODUCERSNCONSUMERS_H_

#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <list>

extern boost::mutex io_mutex;
extern const int BUFFER_SIZE;
extern const int ITERS;

template <class DataType>
class ProducersNConsumers
{

public:

typedef boost::mutex::scoped_lock scoped_lock;

ProducersNConsumers();

~ProducersNConsumers();

DataType Pop(void);

void Push(DataType);

void Print(void);

private:

struct Node {
DataType data;
bool valid;

Node(DataType, bool);
};

int itemCount;

std::list<Node> buffer;

typename std::list <Node>::iterator popIterator;
typename std::list <Node>::iterator pushIterator;

boost::mutex enter;
boost::condition full;
boost::condition empty;

};


#endif /*PRODUCERSNCONSUMERS_H_*/
--------------------------------------------------------------------------------------------------------------------------------------------------------------------



ProducersNConsumers.cpp
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>

#include "ProducersNConsumers.h"

template <class DataType>
ProducersNConsumers<DataType>::Node::Node(DataType data = 0, bool
valid = false)
{
}

template <class DataType>
ProducersNConsumers<DataType>::producersNConsumers()
{
}

template <class DataType>
ProducersNConsumers<DataType>::~ProducersNConsumers()
{

}

template <class DataType>
DataType
ProducersNConsumers<DataType>::pop(void)
{

}


template <class DataType>
void
ProducersNConsumers<DataType>::push(DataType data)
{

}

template<class DataType>
void
ProducersNConsumers<DataType>::print(void)
{
scoped_lock lock(enter);



}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
I

Ian Collins

yatko said:
Hi all;

I have written a class, and it works fine when all the definitions are
in one main.cpp file. But whenever I divide source code in several
files(main.cpp, class.h, class.cpp), it is compiled with no errors,
but linker gives the following error. I could not find what the
problem is.
gcc doesn't support separation of declarations and definitions of
templates, they have to be included inline.
 
K

Keith Halligan

Hi all;

I have written a class, and it works fine when all the definitions are
in one main.cpp file. But whenever I divide source code in several
files(main.cpp, class.h, class.cpp), it is compiled with no errors,
but linker gives the following error. I could not find what the
problem is.

Thanks

yatko
----
**** Build of configuration Debug for project test ****

make all
Building target: test
Invoking: GCC C++ Linker
g++ -o"test" ./ProducersNConsumers.o ./main.o -lboost_thread-gcc41-
mt-1_34_1
./main.o: In function `main':
/home/oktay/workspace/test/Debug/../main.cpp:135: undefined reference
to `ProducersNConsumers<int>::producersNConsumers()'
/home/oktay/workspace/test/Debug/../main.cpp:151: undefined reference
to `ProducersNConsumers<int>::~ProducersNConsumers()'
collect2: ld returned 1 exit status
make: *** [test] Error 1

As was already stated gcc/++ does not support separation of templates
between the header and cpp file. Very few compilers actually support
this feature (the only compiler I know of being Comeau).

There are tricks around this, see the entry in the C++ FAQ below:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top