Sign in to follow this  
felipedrl

Template linking problem...

Recommended Posts

Hi, I´ve finished a graphics library and want to release it without source code for now. So I created a .a The problem is that it uses templates. So in order to link it must see the definition .cpp and the client .cpp at the same time. I´ve looked to some solutions over the net, especially in C++ FAQ LITE, but all of them end giving the source code. I was wondering how STL do it, then I discovered that it uses export just like me but instead of including a .cpp it includes a .tcc which seems to be a hidden .cpp How is the best way to do it? Should I put it on a .tcc an give the .h, .tcc and .a ? Does anyone know another solution? Thanks in advance.

Share this post


Link to post
Share on other sites
Remember, templates are not code - they are instructions to the compiler for how to generate code.

You have two options: conceal the templates from the library interface so that external code does not see them, which should eliminate the need to distribute the template code; or distribute the template code with the static library.

Share this post


Link to post
Share on other sites
Visual C++ 8.0's version of the SC++L, as far as I can tell, is entirely source code (at least when dealing with templates). I've never personally seen export used in any way at all. I'd speculate that the .tcc stuff is compiler-specific, and thus your library wouldn't be too portable if that's the method you used.

I'd personally either just give up on the closed-source requirement, or if possible forward declare template instantiations on types that you know will be used. What are the templates used for anyway? Sometimes, you can provide template instantiations for float and double, for example, and that's all you need. Very few people would need to use anything else (custom number class or something). You'll have to be careful to avoid bloating the size of the library, however. But the only way to make the library fully useable is to actually provide the compiled code necessary to perform the work.

Speaking of that problem, I know that sometimes you can get your templates to use almost all the same code, regardless of type. For example, I've heard of std::vector<void> being used to implement all of the real functionally (using a char buffer for storage, and no constructor/destructor calls on elements), and then inheriting all the other std::vector<???> types from std::vector<void>, implementing most of the functions using little more than a reinterpret_cast<???> call or two. This means that only one instantiation of the code is needed for most functionality, and all those reinterpret_cast<???> calls for each new templated type don't really take up too much code space. It's possible you could use this method in your own library.

Share this post


Link to post
Share on other sites
Yep, you´re right. I´ll really need to give the code anyway.
I can´t provide some template instantiation because I only receives user defined types in it, not int, double, char, that primitive defined types.

Quote:

regardless of type. For example, I've heard of std::vector<void> being used to implement all of the real functionally (using a char buffer for storage, and no constructor/destructor calls on elements), and then inheriting all the other std::vector<???> types from std::vector<void>, implementing most of the functions using little more than a reinterpret_cast<???> call or two. This means that only one instantiation of the code is needed for most functionality, and all those reinterpret_cast<???> calls for each new templated type don't really take up too much code space. It's possible you could use this method in your own library.


It seems to be the C++ to void* C. I dind´t know that. Pretty trick but it won´t work for me anyway...

Thank you. I´ll stick with .tcc that suits me best for now.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this