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>:roducersNConsumers()'
/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>:roducersNConsumers()
{
}
template <class DataType>
ProducersNConsumers<DataType>::~ProducersNConsumers()
{
}
template <class DataType>
DataType
ProducersNConsumers<DataType>:op(void)
{
}
template <class DataType>
void
ProducersNConsumers<DataType>:ush(DataType data)
{
}
template<class DataType>
void
ProducersNConsumers<DataType>:rint(void)
{
scoped_lock lock(enter);
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
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>:roducersNConsumers()'
/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>:roducersNConsumers()
{
}
template <class DataType>
ProducersNConsumers<DataType>::~ProducersNConsumers()
{
}
template <class DataType>
DataType
ProducersNConsumers<DataType>:op(void)
{
}
template <class DataType>
void
ProducersNConsumers<DataType>:ush(DataType data)
{
}
template<class DataType>
void
ProducersNConsumers<DataType>:rint(void)
{
scoped_lock lock(enter);
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------