c++ template lib => does not compile...

bridged with qdn.public.qnxrtp.applications
Post Reply
Miguel Simon

c++ template lib => does not compile...

Post by Miguel Simon » Fri Sep 06, 2002 1:08 am

Hi...

( IDE in rtp 6.2 - PE , native Nto environment)


Here I have an example of a small/simple template based c++ library, and
a main() c++ function that calls the library. The point is that when I
compile in the present form all is fine and all works. But notice that
I have to explicitly call the *.cc source member of the library (see
the code bellow and attached):

#include "MSlib.cc"
^^^^^^^^^^

and I have to provide the path to the source code. But if I have:

#include "MSlib.h"
^^^^^^^^^^

then the project will not compile (even when I provide the path to the
source code is present and the link to the library is provided).

I wonder why is this? Does this mean that we cannot have instantiated
objects in a library? If this is the case, then would this mean that I
cannot make an API available on a library written in c++?? Actually, I
am not sure what this means, or what it is that I am doing wrong. Any
help is most appreciated. Thanks.

Best Regards...

Miguel.


P.S. you 'can make clean; make', but you have to have the library in the
library path. Alternatively, use the IDE.



// ----------- ms.main.cc ---------------------
#include <cstdlib>
#include <iostream>

#include "MSlib.cc"

#include <stdio.h>

int main(int argc, char *argv[])
{
MSlib<int> mslib;

printf("\n name is: %s \n", mslib.name() );


return EXIT_SUCCESS;
}
// ---------------------------------------------

////////////////////////////////////////////////////////////////////////
#ifndef _MS_LIB_H_
#define _MS_LIB_H_

//... local include
#include <cstdio> //... #include <stdio.h> //...NULL, O_RDWR def's,
etc ...
#include <cstring> //... #include <string.h> //...memcpy, memset,
memmove...
#include <cstdlib> //... #include <stdlib.h>
#include <cmath> //... #include <math.h>
#include <cstddef> //... #include <stddef.h>


//... other include


/////////////////////////////////////////
// begin class declaration
/////////////////////////////////////////
template<class T>
class MSlib
{
public:
MSlib( void );
~MSlib( void );

char * name( void );

int var0;

protected:
int var1;

private:
int var2;

};
////////////////////////////////////////


/////////////////////////////////////////
/////////////////////////////////////////
#endif

////////////////////////////////////////////////////////////////////////
#ifndef _MS_LIB_CC__
#define _MS_LIB_CC__

//... local includes
#include "MSlib.h"


/////////////////////////////////////////////
/////////////////////////////////////////
// this is the file constructor
/////////////////////////////////////////
template<class T>
MSlib<T>::MSlib( void )
{


}
//////////////////////////////////////////////
// this is the file destructor
/////////////////////////////////////////////
template<class T>
MSlib<T>::~MSlib(void)
{

}
////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////
template<class T>
char * MSlib<T>::name( void )
{
return ( "ms.lib" );

} //... end getMountPoint()
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////
#endif



but

Chris McKillop

Re: c++ template lib => does not compile...

Post by Chris McKillop » Fri Sep 06, 2002 7:01 pm

It is because template classes are only declared in header files. They
don't actually exist until you make an instance of them in your code, so
there is no "library" to build or link with. Take a look at the standard C++
headers like vector and list.

chris



Miguel Simon <simon@ou.edu> wrote:
[-- text/plain, encoding 7bit, 146 lines --]

Hi...

( IDE in rtp 6.2 - PE , native Nto environment)


Here I have an example of a small/simple template based c++ library, and
a main() c++ function that calls the library. The point is that when I
compile in the present form all is fine and all works. But notice that
I have to explicitly call the *.cc source member of the library (see
the code bellow and attached):

#include "MSlib.cc"
^^^^^^^^^^

and I have to provide the path to the source code. But if I have:

#include "MSlib.h"
^^^^^^^^^^

then the project will not compile (even when I provide the path to the
source code is present and the link to the library is provided).

I wonder why is this? Does this mean that we cannot have instantiated
objects in a library? If this is the case, then would this mean that I
cannot make an API available on a library written in c++?? Actually, I
am not sure what this means, or what it is that I am doing wrong. Any
help is most appreciated. Thanks.

Best Regards...

Miguel.


P.S. you 'can make clean; make', but you have to have the library in the
library path. Alternatively, use the IDE.



// ----------- ms.main.cc ---------------------
#include <cstdlib
#include <iostream

#include "MSlib.cc"

#include <stdio.h

int main(int argc, char *argv[])
{
MSlib<int> mslib;

printf("\n name is: %s \n", mslib.name() );


return EXIT_SUCCESS;
}
// ---------------------------------------------

////////////////////////////////////////////////////////////////////////
#ifndef _MS_LIB_H_
#define _MS_LIB_H_

//... local include
#include <cstdio> //... #include <stdio.h> //...NULL, O_RDWR def's,
etc ...
#include <cstring> //... #include <string.h> //...memcpy, memset,
memmove...
#include <cstdlib> //... #include <stdlib.h
#include <cmath> //... #include <math.h
#include <cstddef> //... #include <stddef.h


//... other include


/////////////////////////////////////////
// begin class declaration
/////////////////////////////////////////
template<class T
class MSlib
{
public:
MSlib( void );
~MSlib( void );

char * name( void );

int var0;

protected:
int var1;

private:
int var2;

};
////////////////////////////////////////


/////////////////////////////////////////
/////////////////////////////////////////
#endif

////////////////////////////////////////////////////////////////////////
#ifndef _MS_LIB_CC__
#define _MS_LIB_CC__

//... local includes
#include "MSlib.h"


/////////////////////////////////////////////
/////////////////////////////////////////
// this is the file constructor
/////////////////////////////////////////
template<class T
MSlib<T>::MSlib( void )
{


}
//////////////////////////////////////////////
// this is the file destructor
/////////////////////////////////////////////
template<class T
MSlib<T>::~MSlib(void)
{

}
////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////
template<class T
char * MSlib<T>::name( void )
{
return ( "ms.lib" );

} //... end getMountPoint()
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////
#endif



but

[-- application/x-gzip, encoding base64, 41 lines, name: ms.example.tar.gz --]
--
Chris McKillop <cdm@qnx.com> "The faster I go, the behinder I get."
Software Engineer, QSSL -- Lewis Carroll --
http://qnx.wox.org/

Miguel Simon

Re: c++ template lib => does not compile...

Post by Miguel Simon » Fri Sep 06, 2002 8:08 pm

Hi Chris...

I think I get it. I had never looked into the c++ definitions all the
way down to the bottom, and you are correct . I always thought that
the class definitions was everything that was contained in those files
such as vector, but they actually define the whole thing there.

Hummm... I suppose that one way to hide all of that complexity is to
instantate those classes in a C based api, and make that into a library.
Would this be something that people commonly do, or would you
suggest another way of doing this??

Thanks for your help...

Miguel

Chris McKillop wrote:
It is because template classes are only declared in header files. They
don't actually exist until you make an instance of them in your code, so
there is no "library" to build or link with. Take a look at the standard C++
headers like vector and list.

chris



Miguel Simon <simon@ou.edu> wrote:

[-- text/plain, encoding 7bit, 146 lines --]

Hi...

( IDE in rtp 6.2 - PE , native Nto environment)


Here I have an example of a small/simple template based c++ library, and
a main() c++ function that calls the library. The point is that when I
compile in the present form all is fine and all works. But notice that
I have to explicitly call the *.cc source member of the library (see
the code bellow and attached):

#include "MSlib.cc"
^^^^^^^^^^

and I have to provide the path to the source code. But if I have:

#include "MSlib.h"
^^^^^^^^^^

then the project will not compile (even when I provide the path to the
source code is present and the link to the library is provided).

I wonder why is this? Does this mean that we cannot have instantiated
objects in a library? If this is the case, then would this mean that I
cannot make an API available on a library written in c++?? Actually, I
am not sure what this means, or what it is that I am doing wrong. Any
help is most appreciated. Thanks.

Best Regards...

Miguel.


P.S. you 'can make clean; make', but you have to have the library in the
library path. Alternatively, use the IDE.



// ----------- ms.main.cc ---------------------
#include <cstdlib
#include <iostream

#include "MSlib.cc"

#include <stdio.h

int main(int argc, char *argv[])
{
MSlib<int> mslib;

printf("\n name is: %s \n", mslib.name() );


return EXIT_SUCCESS;
}
// ---------------------------------------------

////////////////////////////////////////////////////////////////////////
#ifndef _MS_LIB_H_
#define _MS_LIB_H_

//... local include
#include <cstdio> //... #include <stdio.h> //...NULL, O_RDWR def's,
etc ...
#include <cstring> //... #include <string.h> //...memcpy, memset,
memmove...
#include <cstdlib> //... #include <stdlib.h
#include <cmath> //... #include <math.h
#include <cstddef> //... #include <stddef.h


//... other include


/////////////////////////////////////////
// begin class declaration
/////////////////////////////////////////
template<class T
class MSlib
{
public:
MSlib( void );
~MSlib( void );

char * name( void );

int var0;

protected:
int var1;

private:
int var2;

};
////////////////////////////////////////


/////////////////////////////////////////
/////////////////////////////////////////
#endif

////////////////////////////////////////////////////////////////////////
#ifndef _MS_LIB_CC__
#define _MS_LIB_CC__

//... local includes
#include "MSlib.h"


/////////////////////////////////////////////
/////////////////////////////////////////
// this is the file constructor
/////////////////////////////////////////
template<class T
MSlib<T>::MSlib( void )
{


}
//////////////////////////////////////////////
// this is the file destructor
/////////////////////////////////////////////
template<class T
MSlib<T>::~MSlib(void)
{

}
////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////
template<class T
char * MSlib<T>::name( void )
{
return ( "ms.lib" );

} //... end getMountPoint()
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////
#endif



but

[-- application/x-gzip, encoding base64, 41 lines, name: ms.example.tar.gz --]


--
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0
Arial;}{\f1\fswiss\fprq2\fcharset0 Arial;}}
\viewkind4\uc1\pard\f0\fs20 --- \par
my opinions are mine, only mine, solely mine, and they are not related
in any possible way to the institution(s) in which I study and work. \par
--- \par
\b\i\f1 Miguel Simon \par
\b0\i0\f0 Research Engineer \par
School of Aerospace and Mechanical Engineering \par
University of Oklahoma \par
http://www.amerobotics.ou.edu/ \par
http://www.saic.com\par
\par
}

Chris McKillop

Re: c++ template lib => does not compile...

Post by Chris McKillop » Fri Sep 06, 2002 8:14 pm

If you want to use templates then you have to put them in headers. That
is sort of the point of template classes - they don't actually exist
until you make an instance of them. Unfortunatly, most compilers and
linkers are also not smart enough to deal with two templates of the same
type in different files being the same code and you can end up with double
the code size for the same templates. But that really depends on the
compiler and the use of the templates.

Do you need to use a template class? If you just make it a normal class
then you are more then able to put this into a library and treat it like
one treats a C library.

chris




Miguel Simon <simon@ou.edu> wrote:
Hi Chris...

I think I get it. I had never looked into the c++ definitions all the
way down to the bottom, and you are correct . I always thought that
the class definitions was everything that was contained in those files
such as vector, but they actually define the whole thing there.

Hummm... I suppose that one way to hide all of that complexity is to
instantate those classes in a C based api, and make that into a library.
Would this be something that people commonly do, or would you
suggest another way of doing this??

Thanks for your help...

Miguel

Chris McKillop wrote:
It is because template classes are only declared in header files. They
don't actually exist until you make an instance of them in your code, so
there is no "library" to build or link with. Take a look at the standard C++
headers like vector and list.

chris



Miguel Simon <simon@ou.edu> wrote:

[-- text/plain, encoding 7bit, 146 lines --]

Hi...

( IDE in rtp 6.2 - PE , native Nto environment)


Here I have an example of a small/simple template based c++ library, and
a main() c++ function that calls the library. The point is that when I
compile in the present form all is fine and all works. But notice that
I have to explicitly call the *.cc source member of the library (see
the code bellow and attached):

#include "MSlib.cc"
^^^^^^^^^^

and I have to provide the path to the source code. But if I have:

#include "MSlib.h"
^^^^^^^^^^

then the project will not compile (even when I provide the path to the
source code is present and the link to the library is provided).

I wonder why is this? Does this mean that we cannot have instantiated
objects in a library? If this is the case, then would this mean that I
cannot make an API available on a library written in c++?? Actually, I
am not sure what this means, or what it is that I am doing wrong. Any
help is most appreciated. Thanks.

Best Regards...

Miguel.


P.S. you 'can make clean; make', but you have to have the library in the
library path. Alternatively, use the IDE.



// ----------- ms.main.cc ---------------------
#include <cstdlib
#include <iostream

#include "MSlib.cc"

#include <stdio.h

int main(int argc, char *argv[])
{
MSlib<int> mslib;

printf("\n name is: %s \n", mslib.name() );


return EXIT_SUCCESS;
}
// ---------------------------------------------

////////////////////////////////////////////////////////////////////////
#ifndef _MS_LIB_H_
#define _MS_LIB_H_

//... local include
#include <cstdio> //... #include <stdio.h> //...NULL, O_RDWR def's,
etc ...
#include <cstring> //... #include <string.h> //...memcpy, memset,
memmove...
#include <cstdlib> //... #include <stdlib.h
#include <cmath> //... #include <math.h
#include <cstddef> //... #include <stddef.h


//... other include


/////////////////////////////////////////
// begin class declaration
/////////////////////////////////////////
template<class T
class MSlib
{
public:
MSlib( void );
~MSlib( void );

char * name( void );

int var0;

protected:
int var1;

private:
int var2;

};
////////////////////////////////////////


/////////////////////////////////////////
/////////////////////////////////////////
#endif

////////////////////////////////////////////////////////////////////////
#ifndef _MS_LIB_CC__
#define _MS_LIB_CC__

//... local includes
#include "MSlib.h"


/////////////////////////////////////////////
/////////////////////////////////////////
// this is the file constructor
/////////////////////////////////////////
template<class T
MSlib<T>::MSlib( void )
{


}
//////////////////////////////////////////////
// this is the file destructor
/////////////////////////////////////////////
template<class T
MSlib<T>::~MSlib(void)
{

}
////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////
template<class T
char * MSlib<T>::name( void )
{
return ( "ms.lib" );

} //... end getMountPoint()
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////
#endif



but

[-- application/x-gzip, encoding base64, 41 lines, name: ms.example.tar.gz --]





--
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0
Arial;}{\f1\fswiss\fprq2\fcharset0 Arial;}}
\viewkind4\uc1\pard\f0\fs20 --- \par
my opinions are mine, only mine, solely mine, and they are not related
in any possible way to the institution(s) in which I study and work. \par
--- \par
\b\i\f1 Miguel Simon \par
\b0\i0\f0 Research Engineer \par
School of Aerospace and Mechanical Engineering \par
University of Oklahoma \par
http://www.amerobotics.ou.edu/ \par
http://www.saic.com\par
\par
}
--
Chris McKillop <cdm@qnx.com> "The faster I go, the behinder I get."
Software Engineer, QSSL -- Lewis Carroll --
http://qnx.wox.org/

Miguel Simon

Re: c++ template lib => does not compile...

Post by Miguel Simon » Sat Sep 07, 2002 5:00 am

Hi Chris...

Chris McKillop wrote:
If you want to use templates then you have to put them in headers. That
is sort of the point of template classes - they don't actually exist
until you make an instance of them. Unfortunatly, most compilers and
linkers are also not smart enough to deal with two templates of the same
type in different files being the same code and you can end up with double
the code size for the same templates. But that really depends on the
compiler and the use of the templates.
Yes. I have actually played with classes and templates, and have found
out what you say in here. Thanks for the confirmation.
Do you need to use a template class? If you just make it a normal class
then you are more then able to put this into a library and treat it like
one treats a C library.
No, I do not need templates in all cases. I'll go the normal class way
as you recommend. At least now I know what was the matter. Thanks.

Thanks so much for your help. I appreciate it.

Best Regards...

Miguel.
chris

Rennie Allen

Re: c++ template lib => does not compile...

Post by Rennie Allen » Wed Sep 11, 2002 9:22 am

Miguel Simon wrote:
Hummm... I suppose that one way to hide all of that complexity is to
instantate those classes in a C based api, and make that into a library.
Would this be something that people commonly do, or would you suggest
another way of doing this??
Well, any compiler that calls itself a C++ compiler (in the strict
sense) *must* support the "export" keyword which provides the
functionality you require. Unfortunately, g++ is not a C++ compiler
(the version that we have available in QNX isn't even close).

In the past, pretenders were tolerated in their claims to be C++
compilers, since there were no real C++ compilers. That has changed. I
suggest that we should now refer to g++ as simply g++. MSVC++ as MSVC++,
and only ISO conformant C++ compilers (or at least those that claim to
support all the keywords) as C++ compilers (to avoid confusion).

Rennie "It's bad enough I have to write in C++, but at least I would
like to be able to actually compile the cruft" Allen

Bill Caroselli (Q-TPS)

Re: c++ template lib => does not compile...

Post by Bill Caroselli (Q-TPS) » Wed Sep 11, 2002 9:31 pm

Hi Rennie

Is cruft a new word or a typo. I looked in three dictionaries and could not
find it.

If it is a new word, I'd like to learn it. I'm always looking for stuff
like that to throw in someone else's face.

"Rennie Allen" <rallen@csical.com> wrote in message
news:3D7F0B66.9020108@csical.com...
Rennie "It's bad enough I have to write in C++, but at least I would
like to be able to actually compile the cruft" Allen
^^^^^

Robert Krten

Re: c++ template lib => does not compile...

Post by Robert Krten » Wed Sep 11, 2002 10:44 pm

"Bill Caroselli \(Q-TPS\)" <QTPS@earthlink.net> wrote:
Hi Rennie

Is cruft a new word or a typo. I looked in three dictionaries and could not
find it.
"cruft" == "extra unneeded crap" in my experience :-)
If it is a new word, I'd like to learn it. I'm always looking for stuff
like that to throw in someone else's face.
Typically, "there's a whole whack of cruft in this code".
I've also seen it used to imply "obsolete", also in the "extra unneeded crap" vein... :-)

Cheers,
-RK

--
Robert Krten, PARSE Software Devices +1 613 599 8316.
Realtime Systems Architecture, Books, Video-based and Instructor-led
Training and Consulting at www.parse.com.
Email my initials at parse dot com.

Brian K. Hlady

Re: c++ template lib => does not compile...

Post by Brian K. Hlady » Thu Sep 12, 2002 3:06 am

That's cause you looked in the wrong ones.

http://www.tuxedo.org/~esr/jargon/html/entry/cruft.html

<quote>
[very common; back-formation from crufty] 1. n. An unpleasant substance. The
dust that gathers under your bed is cruft; the TMRC Dictionary correctly
noted that attacking it with a broom only produces more. 2. n. The results
of shoddy construction. 3. vt. [from `hand cruft', pun on `hand craft'] To
write assembler code for something normally (and better) done by a compiler
(see hand-hacking). 4. n. Excess; superfluous junk; used esp. of redundant
or superseded code. 5. [University of Wisconsin] n. Cruft is to hackers as
gaggle is to geese; that is, at UW one properly says "a cruft of hackers".

</quote>



Cheers,

-Brian

"Bill Caroselli (Q-TPS)" <QTPS@EarthLink.net> wrote in message
news:aloc1q$s4l$1@inn.qnx.com...
Hi Rennie

Is cruft a new word or a typo. I looked in three dictionaries and could
not
find it.

If it is a new word, I'd like to learn it. I'm always looking for stuff
like that to throw in someone else's face.

"Rennie Allen" <rallen@csical.com> wrote in message
news:3D7F0B66.9020108@csical.com...

Rennie "It's bad enough I have to write in C++, but at least I would
like to be able to actually compile the cruft" Allen
^^^^^


Miguel Simon

Re: c++ template lib => does not compile...

Post by Miguel Simon » Mon Sep 30, 2002 2:18 am

Hi...

answer in:

http://www.cplusplus.com/doc/tutorial/tut5-1.html

also in the sept.2002 issue of www.cju.com.

Thanks for the answers and insight.

bests...

Miguel.

Post Reply

Return to “qdn.public.qnxrtp.applications”