MESSAGE
DATE | 2016-11-05 |
FROM | ruben safir
|
SUBJECT | Subject: [Learn] Fwd: Template Library files and Header linking troubles
|
From learn-bounces-at-nylxs.com Sat Nov 5 19:35:04 2016 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: from www.mrbrklyn.com (www.mrbrklyn.com [96.57.23.82]) by mrbrklyn.com (Postfix) with ESMTP id 179E1161312; Sat, 5 Nov 2016 19:35:04 -0400 (EDT) X-Original-To: learn-at-nylxs.com Delivered-To: learn-at-nylxs.com Received: from [10.0.0.62] (flatbush.mrbrklyn.com [10.0.0.62]) by mrbrklyn.com (Postfix) with ESMTP id A0506160E77 for ; Sat, 5 Nov 2016 19:34:54 -0400 (EDT) References: <87wpghenr0.fsf-at-bsb.me.uk> <8fd740fc-af36-42d1-b0aa-96efe8fb8f44-at-googlegroups.com> <3uadnWRenf4OyYPFnZ2dnUU78KnNnZ2d-at-giganews.com> To: learn-at-nylxs.com From: ruben safir X-Forwarded-Message-Id: <87wpghenr0.fsf-at-bsb.me.uk> <8fd740fc-af36-42d1-b0aa-96efe8fb8f44-at-googlegroups.com> <3uadnWRenf4OyYPFnZ2dnUU78KnNnZ2d-at-giganews.com> Message-ID: <03c47384-c262-5205-74ad-fe66ce352834-at-mrbrklyn.com> Date: Sat, 5 Nov 2016 19:34:54 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <3uadnWRenf4OyYPFnZ2dnUU78KnNnZ2d-at-giganews.com> Content-Type: multipart/mixed; boundary="------------A18A84B2A89E41FFC2077E56" Subject: [Learn] Fwd: Template Library files and Header linking troubles X-BeenThere: learn-at-nylxs.com X-Mailman-Version: 2.1.17 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
This is a multi-part message in MIME format. --------------A18A84B2A89E41FFC2077E56 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit
--------------A18A84B2A89E41FFC2077E56 Content-Type: message/rfc822; name="Template Library files and Header linking troubles.eml" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="Template Library files and Header linking troubles.eml"
Path: reader2.panix.com!panix!not-for-mail From: Popping mad Newsgroups: comp.lang.c++ Subject: Template Library files and Header linking troubles Date: Sat, 5 Nov 2016 15:41:25 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader2.panix.com 1478360485 13949 96.57.23.82 (5 Nov 2016 15:41:25 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Sat, 5 Nov 2016 15:41:25 +0000 (UTC) User-Agent: Pan/0.140 (Chocolate Salty Balls; GIT b8fc14e git.gnome.org/git/pan2) Xref: panix comp.lang.c++:1125027
I've been trying to create some custom system libraries with templating and it surprised me when I starting to get linking problems. Generally I create a .h file and a .cpp file and develop objects which can later be developed into shared libraries. Years ago I did quite a bit of work with C++ and libraries and I don't recall having this problem, but looking back at the code, I see I was cramming a lot of class definitions directly into the .h file. So I must have hit this snag before but I don't remember it.
You'll have to excuse me if I sound a little vague on this problem I am having, but it is very complicated and I'm slow at getting my head around this issue. And it seems to affected somehow by name space.
I'm looking over the text by Vandevoorde and Josuttis, "C++ Templates The complete guild, 12th printing in June of 2010 and copyrighted in 2003. They seem to sum up the problem fairly well, although there explanation is a bit cryptic to me. Section 6.1.1 and 6.1.2 on pages 63 after show some sample code of a 3 file break out of C++ code they write:
A C++ compiler will most likely accept this program without any problems, but the linker will probably report an error, implying that there is no definition for the function print_typeof()...
(adlib) for a common solution... We include the defintions of a template in the header file that declares that template.. For our example, we can do this by adding
#include "myfirst.cpp"
at the end of myfirst.cpp in every do-C file that uses the template. A third way, or course, is to do away with myfirst.cpp and rewrite myfirst.hpp so that it contains all template declarations and template definitions.
Ugg. So I'm looking over old code that I wrote and it seems that namespace influences the result of this, at least with gcc on GNU.
In this file here, I seem to be having no problem creating templated objects in a .cpp file, but everything is being dumped the std namespace
http://www.nylxs.com/docs/workshops/linklist_templates/linklist.cpp.html
1 #include "linklist.h" 2 #include 3 4 using namespace std; 5 6 template 7 NODE::~NODE(){ 8 //cout << "List Item Destroyed" << endl; 9 10 } 11 12 13 14 15 template 16 void LIST::remove_front(){ 17 if(_at_front == 0){ 18 cout << "No List" << endl; 19 return; 20 } 21 NODE * tmp = _at_front; 22 down_size(); 23 _at_front = tmp->next(); 24 if(tmp->next() == NULL) 25 _at_end = 0; //need to clear _at_end as well once you delete the last node 26 27 28 delete tmp; 29 return; 30 } 31
The code I'm working on currently is less fortunate. I created a namespace for it
~~nodes.cpp
#include "nodes.h" #include using namespace std; template tree::NODE::NODE( unk states, NODE *cl, NODE *cr, NODE *p ){ cout << "Im here" << endl; }
template tree::NODE::~NODE(){ }; ~~~~
|| g++ -Wall -ggdb -c nodes.cpp nodes.cpp|5 col 1| error: ‘tree’ does not name a type || tree::NODE::NODE( unk states, NODE *cl, NODE *cr, NODE *p ){ || ^~~~ nodes.cpp|10 col 1| error: ‘tree’ does not name a type || tree::NODE::~NODE(){ || ^~~~ make: *** [makefile|8| nodes.o] Error 1
before this, it was saying it couldn't find definitions of my templates in this file. I did a circular thing and included, as per what it said in the text,
#include node.cpp
in file node.h
/* * ===================================================================================== * * Filename: nodes.h * * Description: description of phylogentic nodes as per * Fitch and Sannkoff as describd by Felenstein * * Version: 1.0 * Created: Nov 4 21:15:49 EDT 2016 * Revision: none * Compiler: gcc * * Author: Ruben Safir, * Company: LIU Thesis * * ===================================================================================== */ #ifndef NODES_H #define NODES_H #include #include #include #include "nodes.cpp" namespace tree {
/* ============================================================================== * Class NODE - * Description - This is a node in the tree that must undergo parsimony evaluation * ================================================================================ */
Now the namespace issue is cropping up.
--------------A18A84B2A89E41FFC2077E56 Content-Type: message/rfc822; name="Re: Template Library files and Header linking troubles.eml" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename*0="Re: Template Library files and Header linking troubles.eml"
Path: reader2.panix.com!panix!goblin3!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "Alf P. Steinbach" Newsgroups: comp.lang.c++ Subject: Re: Template Library files and Header linking troubles Date: Sat, 5 Nov 2016 18:51:33 +0100 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 5 Nov 2016 17:53:22 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="53e181cd2d791ebebf3babae72450b90"; logging-data="887"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX1/ufBOsf1MKIeKONJWvFNVg" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 In-Reply-To: Cancel-Lock: sha1:1a3nljj4yhfeN8rziANmw0jMqoQ= Xref: panix comp.lang.c++:1125031
On 05.11.2016 16:41, Popping mad wrote: > > I've been trying to create some custom system libraries with templating > and it surprised me when I starting to get linking problems. Generally I > create a .h file and a .cpp file and develop objects which can later be > developed into shared libraries. Years ago I did quite a bit of work > with C++ and libraries and I don't recall having this problem, but looking > back at the code, I see I was cramming a lot of class definitions directly > into the .h file. So I must have hit this snag before but I don't > remember it. [snip] > > Now the namespace issue is cropping up. >
You don't have a namespace-related problem, or if you have one it's unrelated.
The problem is simply that in order to instantiate a template with given template arguments, the full textual definition must be available to the compiler.
If you compile the template definitions separately, then you can only instantiate them in the translation unit where you define them (because here the textual definitions are available), and this means instantiating them for a fixed number of possible types. They can be used with these arguments in other TUs. But any other arguments, and the linker will complain of lacking instantiations.
So usually templates are fully defined in headers.
E.g. much of Boost are template based header-only sub-libraries.
• • •
C++98 and C++03 had an `export` feature to deal with separate compilation of template definitions.
Unfortunately, as I recall it was only ever implemented by Comeau and (with an undocumented option to activate it) in the Intel compiler.
`export` was removed in C++11.
• • •
Rumours have long been that this will be fixed again “when” C++ gets modules, and that's also rumoured to fix the extreme build times and much else. E.g. Microsoft dismissed my suggestion of how to improve build times because, hey, modules will fix all that. And Visual C++ does have experimental module support already, but I don't know whether it fixes build times, or deals with templates, or what.
Cheers & hth.,
- Alf
--------------A18A84B2A89E41FFC2077E56 Content-Type: message/rfc822; name="Re: Template Library files and Header linking troubles.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="Re: Template Library files and Header linking troubles.eml"
Path: reader2.panix.com!panix!not-for-mail From: ruben safir Newsgroups: comp.lang.c++ Subject: Re: Template Library files and Header linking troubles Date: Sat, 5 Nov 2016 14:49:22 -0400 Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: References: NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: reader2.panix.com 1478371763 15672 96.57.23.82 (5 Nov 2016 18:49:23 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Sat, 5 Nov 2016 18:49:23 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 In-Reply-To: Xref: panix comp.lang.c++:1125035
On 11/05/2016 01:51 PM, Alf P. Steinbach wrote: > translation unit
what is that...exactly.
--------------A18A84B2A89E41FFC2077E56 Content-Type: message/rfc822; name="Re: Template Library files and Header linking troubles.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="Re: Template Library files and Header linking troubles.eml"
Path: reader2.panix.com!panix!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.c++ Subject: Re: Template Library files and Header linking troubles Date: Sat, 05 Nov 2016 19:21:39 +0000 Organization: A noiseless patient Spider Message-ID: <87wpghenr0.fsf-at-bsb.me.uk> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="2e1e84f92aba0f307ca14cc117644e08"; logging-data="32752"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX19nDIzYAv49QyN6/xJik6uhaZz6/+p98hU=" Cancel-Lock: sha1:L1JyKVtQjRSmP2pGNbQm9EHB0Mc= sha1:eWsLag8NjmrkP3VdtOyqtPVTAZc= X-BSB-Auth: 1.dbb02969d7fb5aff009b.20161105192139GMT.87wpghenr0.fsf-at-bsb.me.uk Xref: panix comp.lang.c++:1125038
ruben safir writes:
> On 11/05/2016 01:51 PM, Alf P. Steinbach wrote: >> translation unit > > what is that...exactly.
"The text of the program is kept in units called source files in this International Standard. A source file together with all the headers (17.6.1.2) and source files included (16.2) via the preprocessing directive #include, less any source lines skipped by any of the conditional inclusion (16.1) preprocessing directives, is called a translation unit."
-- Ben.
--------------A18A84B2A89E41FFC2077E56 Content-Type: message/rfc822; name="Re: Template Library files and Header linking troubles.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="Re: Template Library files and Header linking troubles.eml"
Path: reader2.panix.com!panix!not-for-mail From: Popping mad Newsgroups: comp.lang.c++ Subject: Re: Template Library files and Header linking troubles Date: Sat, 5 Nov 2016 20:28:05 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: References: <87wpghenr0.fsf-at-bsb.me.uk> NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader2.panix.com 1478377685 16127 96.57.23.82 (5 Nov 2016 20:28:05 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Sat, 5 Nov 2016 20:28:05 +0000 (UTC) User-Agent: Pan/0.140 (Chocolate Salty Balls; GIT b8fc14e git.gnome.org/git/pan2) Xref: panix comp.lang.c++:1125043
On Sat, 05 Nov 2016 19:21:39 +0000, Ben Bacarisse wrote:
> ruben safir writes: > >> On 11/05/2016 01:51 PM, Alf P. Steinbach wrote: >>> translation unit >> >> what is that...exactly. > > "The text of the program is kept in units called source files in this > International Standard. A source file together with all the headers > (17.6.1.2) and source files included (16.2) via the preprocessing > directive #include, less any source lines skipped by any of the > conditional inclusion (16.1) preprocessing directives, is called a > translation unit."
By this definition, I'm not understanding what Mr Steinbach is saying. These files are connected by #includes and should be visible to each other at compile time, and yet when a template is created in a namespace in the .h file, that namespace is not even being translated as a namespace scope in the .cpp file even when they are cross #included to each other.
I don't know what it is doing or why.
--------------A18A84B2A89E41FFC2077E56 Content-Type: message/rfc822; name="Re: Template Library files and Header linking troubles.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="Re: Template Library files and Header linking troubles.eml"
X-Received: by 10.13.254.71 with SMTP id o68mr9984ywf.156.1478383012964; Sat, 05 Nov 2016 14:56:52 -0700 (PDT) X-Received: by 10.157.45.170 with SMTP id g39mr14490otb.16.1478383012924; Sat, 05 Nov 2016 14:56:52 -0700 (PDT) Path: reader2.panix.com!panix!bloom-beacon.mit.edu!bloom-beacon.mit.edu!168.235.88.217.MISMATCH!feeder.erje.net!2.us.feeder.erje.net!weretis.net!feeder6.news.weretis.net!news.glorb.com!n6no1240162qtd.0!news-out.google.com!c26ni1212itd.0!nntp.google.com!q124no543060itd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.c++ Date: Sat, 5 Nov 2016 14:56:52 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse-at-google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=82.131.68.184; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK NNTP-Posting-Host: 82.131.68.184 References: <87wpghenr0.fsf-at-bsb.me.uk> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <8fd740fc-af36-42d1-b0aa-96efe8fb8f44-at-googlegroups.com> Subject: Re: Template Library files and Header linking troubles From: =?UTF-8?B?w5bDtiBUaWli?= Injection-Date: Sat, 05 Nov 2016 21:56:52 +0000 Content-Type: text/plain; charset=UTF-8 Xref: panix comp.lang.c++:1125046
On Saturday, 5 November 2016 22:28:13 UTC+2, Popping mad wrote: > On Sat, 05 Nov 2016 19:21:39 +0000, Ben Bacarisse wrote: > > > ruben safir writes: > > > >> On 11/05/2016 01:51 PM, Alf P. Steinbach wrote: > >>> translation unit > >> > >> what is that...exactly. > > > > "The text of the program is kept in units called source files in this > > International Standard. A source file together with all the headers > > (17.6.1.2) and source files included (16.2) via the preprocessing > > directive #include, less any source lines skipped by any of the > > conditional inclusion (16.1) preprocessing directives, is called a > > translation unit." > > By this definition, I'm not understanding what Mr Steinbach is saying. > These files are connected by #includes and should be visible to each > other at compile time, and yet when a template is created in a namespace > in the .h file, that namespace is not even being translated as a namespace > scope in the .cpp file even when they are cross #included to each other. > > I don't know what it is doing or why.
You are too much describing how you are confused with something and your feelings about it and so too few what you expect and what really happens. Perhaps you can make a minimal full example that really does behave in unexpected manner for you?
--------------A18A84B2A89E41FFC2077E56 Content-Type: message/rfc822; name="Re: Template Library files and Header linking troubles.eml" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename*0="Re: Template Library files and Header linking troubles.eml"
Path: reader2.panix.com!panix!goblin2!goblin1!goblin.stu.neva.ru!border1.nntp.ams1.giganews.com!border2.nntp.ams1.giganews.com!nntp.giganews.com!buffer2.nntp.ams1.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 05 Nov 2016 16:52:19 -0500 Date: Sat, 05 Nov 2016 23:52:14 +0200 From: Paavo Helde User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.8) Gecko/20151117 FossaMail/25.1.9 MIME-Version: 1.0 Newsgroups: comp.lang.c++ Subject: Re: Template Library files and Header linking troubles References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Message-ID: <3uadnWRenf4OyYPFnZ2dnUU78KnNnZ2d-at-giganews.com> X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-RPKmPdcEZGufb6gRHV/oxZ/HSAqYnw0KWYpGUIEVjVH3qIx3zPXwEI7bjtogCrqWcrGZf7rmqMx5fc8!QUZmBLYQVcPn8OxJu1Rn/6NkOFNcP4iutnmewdppM2TJIA62xF/LkAYKRFamEzEe36oeooOfvow= X-Complaints-To: abuse-at-giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2179 Xref: panix comp.lang.c++:1125045
On 5.11.2016 17:41, Popping mad wrote: > The code I'm working on currently is less fortunate. I created a > namespace for it > > > ~~nodes.cpp > > #include "nodes.h" > #include > using namespace std; > template > tree::NODE::NODE( unk states, NODE *cl, NODE *cr, NODE > *p ){ > cout << "Im here" << endl; > } > > template > tree::NODE::~NODE(){ > };
> || g++ -Wall -ggdb -c nodes.cpp > nodes.cpp|5 col 1| error: ‘tree’ does not name a type > || tree::NODE::NODE( unk states, NODE *cl, NODE *cr, > NODE *p ){ > || ^~~~
This is not a linker error.
Is the namespace called "tree"? If so, change this code to
#include "nodes.h" #include using namespace std;
namespace tree {
template NODE::NODE( unk states, NODE *cl, NODE *cr, NODE *p ){ cout << "Im here" << endl; }
template NODE::~NODE(){ };
}
--------------A18A84B2A89E41FFC2077E56 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline
_______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
--------------A18A84B2A89E41FFC2077E56--
|
|