473,388 Members | 1,322 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Linking code with templates and template libraries

I have a code which uses external library with templates. The separate
compiling of project files gets definition of the same symbols in
different object files, so linking fails.

Actually, library uses templates only internally, and each
recompilation does not add anything new.

How should I fix it? Should I drop the templates from library?

May 6 '07 #1
3 1338
ba********@gmail.com wrote:
I have a code which uses external library with templates. The separate
compiling of project files gets definition of the same symbols in
different object files, so linking fails.

Actually, library uses templates only internally, and each
recompilation does not add anything new.

How should I fix it? Should I drop the templates from library?
Either

1) Drop templates.

2) Put template code in header files.

If you want a library of templates, 2 is the ONLY option. It's how the
STL works.

john
May 7 '07 #2
probably you can use explicit intantiation technique.

e.g.

If you have a template function declared in a.h as

template<typename T>
void print(const T&);

and defined in a.c as

template<typename T>
void print(const T&)
{
......
}

Keep prepocessor guards in all the file which have to be #included
(a.h,a.c in this case).

Then you can have separate .h file say b.h in which you can explicitly
instantiate it as

//b.h
#include "a.c"

template void print<double/*Or whatever as per your implementation*/
>(const double&);
Link it with your test file.
//test.cpp

#include "a.h"

int main()
{
double d = 0.0;
print(d);
}

This method can also be used for class templates,member
functions,Template member functions.

Regards,
Siddhu

May 7 '07 #3
Sorry..Few corrections..

On May 7, 10:28 am, siddhu <siddharth....@gmail.comwrote:
probably you can use explicit intantiation technique.

e.g.

If you have a template function declared in a.h as

template<typename T>
void print(const T&);

and defined in a.c as

template<typename T>
void print(const T&)
{
.....

}

Keep prepocessor guards in all the file which have to be #included
(a.h,a.c in this case).

Then you can have separate .h file say b.c in which you can explicitly
instantiate it as

//b.c
#include "a.c"

template void print<double/*Or whatever as per your implementation*/>(const double&);

Link b.c with your test file.

//test.cpp

#include "a.h"

int main()
{
double d = 0.0;
print(d);

}

This method can also be used for class templates,member
functions,Template member functions.

Regards,
Siddhu

May 7 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martman | last post by:
Can someone point me to a good resource explaining how and why to use templates in php. I am new to php and wondering if these so called templates are just include files or what. And I have read...
1
by: Tobias Langner | last post by:
I try to program a smart-pointer using policies. I rely heavyly on templates during this. The program compiles fine - but I get a linker error: test.o(.text+0x3e): In function `testSmartPtr()':...
2
by: Avi Uziel | last post by:
Hi All, I have a linkage problem that I believe related to template instantiation. My environment is Solaris 5.6, Compiler WorkShop 5. I'm building a shared library which use templates. During...
15
by: Rob Ratcliff | last post by:
I'm compiling the latest version of a CORBA ORB called MICO on a Cray X1. It makes heavy use of templates and namespaces. Up until the link step, the C++ source code compiled flawlessly. But, when...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
5
by: Christian Christmann | last post by:
Hi, I want to implement an graph using templates. In my header file I define the templates node and edge: template <class NODE> class GNode { NODE *info; public: GraphNode();
15
by: Improving | last post by:
I have a template class that has static members, so in the .cpp file I have defined them with templated definitions. Now when in a different ..cpp file that includes the header file for the...
2
by: pssraju | last post by:
Hi, At present application was built on solaris 9 using sun studio 9 (Sun C++ 5.6) & rouguewave sorce pro 5. We are planning to port the same application onto SuSE Linux 9.5.0 using GCC 3.3.3 & RW...
5
by: ciccio | last post by:
Hi, I have a problem with my code that the compiler does not find any inline functions which are static. The simple code example is written below, this is what the compiler throws at me. ]...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.