By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

linking problem ...

P: n/a
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::ostream& operator<< (std::ostream& os, std::vector<T>& v);

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

template <class T>
std::ostream& operator<< (std::ostream& os, std::vector<T>& v) {
os << "<";
for (unsigned int i=0; i<v.size(); i++)
os << (i ? ", " : "") << v[i];
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 3 Dec 2003 02:56:50 -0800, mu*****@yahoo.com (oguz mut) wrote:
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<iostream> // this might help#include<vector>

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

#endif
/************************************************** ***********************


Cheers!

Jul 22 '05 #2

P: n/a
oguz mut wrote in news:4a**************************@posting.google.c om:

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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

P: n/a
mu*****@yahoo.com (oguz mut) writes:
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

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
Jul 22 '05 #4

P: n/a
"Frank Schmitt" <in*****@see-signature.info> wrote in message
news:4c************@scxw21.4sc...
[SNIP]
#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.

[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
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.