linking problem ...

O

oguz mut

hi,

I have a linking problem while living in the following configuration:
. windows 2000
. cygwin
. g++ (GCC) 3.3.1 (cygming special)

I work with the following 3 files
(trivial.hpp, trivial.cpp, trivialMain.cpp):

/*************************************************************************
* trivial.hpp
*************************************************************************/
#ifndef __TRIVIAL__
#define __TRIVIAL__

#include<vector>

template <class T>
std::eek:stream& operator<< (std::eek:stream& os, std::vector<T>& v);

#endif


/*************************************************************************
* trivial.cpp
*************************************************************************/
#include <trivial.hpp>

template <class T>
std::eek:stream& operator<< (std::eek:stream& os, std::vector<T>& v) {
os << "<";
for (unsigned int i=0; i<v.size(); i++)
os << (i ? ", " : "") << v;
os << ">";

return os;
}

// #include "trivialMain.cpp"


/*************************************************************************
* trivialMain.cpp
*************************************************************************/
#include<iostream>
#include<vector>

#include<trivial.hpp>

int main (void) {
std::vector<int> v(3);

v[0] = 1;
v[1] = 2;
v[2] = 3;

std::cout << v << std::endl;

return 0;
}


/************************************************************************/


under these conditions, I do the followings:

bash-2.05b$ g++ -Wall -I. trivial.cpp -c
bash-2.05b$ g++ -Wall -I. trivialMain.cpp trivial.o

I expect the linker to link. however I get:

/cygdrive/c/DOCUME~1/OMUT~1.INT/LOCALS~1/Temp/cc3PPHw8.o(.text+0xc8):trivialMain
..cpp: undefined reference to `std::basic_ostream<char, std::char_traits<char> >&
operator<< <int>(std::basic_ostream<char, std::char_traits<char> >&, std::vecto
r<int, std::allocator<int> >&)'

but, when I uncomment the last line in trivial.cpp and compile
as follows I do not get any error!

bash-2.05b$ g++ -Wall -I. trivial.cpp


what is the problem;
have I coded something wrong, or have I misused g++ ?


thanks in advance!
oguz mut
 
D

Dan W.

I have a linking problem while living in the following configuration:

Not a linking problem, a compile problem
/*************************************************************************
* trivial.hpp
*************************************************************************/
#ifndef __TRIVIAL__
#define __TRIVIAL__
#include said:
#include<vector>

template <class T>
std::eek:stream& operator<< (std::eek:stream& os, std::vector<T>& v);

#endif


/*************************************************************************

Cheers!
 
R

Rob Williscroft

oguz mut wrote in
I work with the following 3 files
(trivial.hpp, trivial.cpp, trivialMain.cpp):

Since you dont use export (you compiler doesn't support it) you need
to put you template defenition's in the header file.

That is put the contents of trivial.cpp in trivial.hpp.

The declaration that is currently in trivial.hpp can be removed, as
a definition is also a declaration.

Assuming you don't have any non-template code in trivial.cpp remove it
from your build / make or stop typing it on your g++ command line.

HTH.

Rob.
 
F

Frank Schmitt

hi,

I have a linking problem while living in the following configuration:
. windows 2000
. cygwin
. g++ (GCC) 3.3.1 (cygming special)

I work with the following 3 files
(trivial.hpp, trivial.cpp, trivialMain.cpp):

/*************************************************************************
* trivial.hpp
*************************************************************************/
#ifndef __TRIVIAL__
#define __TRIVIAL__

This is illegal - identifiers containing two consecutive underscores or
starting with an underscore followed by an uppercase letter are reserved
to the implementation.
Apart from that, Rob answered your question nicely.

kind regards
frank
 
C

Chris Theis

[SNIP]
This is illegal - identifiers containing two consecutive underscores or
starting with an underscore followed by an uppercase letter are reserved
to the implementation.
[SNIP]

This is most probably knitpicking but I wouldn't go so far to say that it is
illegal. According to the standard (17.4.3.1.2) names containing two
consecutive underscores are reserved for the implementation which does not
imply that their use is illegal but their use is of course strongly
discouraged.

Chris
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top