# Problems with GNU C++ compiler and template classes

**URL:** <https://boards.straightdope.com/t/problems-with-gnu-c-compiler-and-template-classes/414272>\
**Category:** Factual Questions\
**Created:** [August 3, 2007, 12:19pm UTC](https://boards.straightdope.com/t/problems-with-gnu-c-compiler-and-template-classes/414272 "2007-08-03T12:19:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![VunderBob](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@VunderBob](https://boards.straightdope.com/u/VunderBob)\
**Post date:** [August 3, 2007, 12:19pm UTC](https://boards.straightdope.com/t/problems-with-gnu-c-compiler-and-template-classes/414272/1 "2007-08-03T12:19:50Z")

</div>

I’m using a GNU C/C++ compiler for a work project. The particular compiler is newer than what I’ve used in the past, and it’s actually a cross compiler.

I’m having multiple problems getting it to work properly. So far, everything can be chalked up to (re)learning curve and :smack: moments, but I still can’t get the stupid thing to digest STL template classes. The particular problem I’m seeing is that I include \<list\>, but \<iterator\> starts flagging nonsensical syntax errors. FWIW, I haven’t included \<iterator\>.

When I used GNU C++ in an academic environment (native mode), I had 0 problems. Similarly, the code I’m working with now was originally written in Visual C++, and it compiled just fine there, too.

I’ve spent 2 days now combing the GCC MAN pages for anything related to compiler switches or other options to indicate use of template classes, and I’ve come up empty. Help?!?

---

<div class="post-metadata">

**Author:** ![Rysto](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rysto](https://boards.straightdope.com/u/Rysto)\
**Post date:** [August 3, 2007, 12:29pm UTC](https://boards.straightdope.com/t/problems-with-gnu-c-compiler-and-template-classes/414272/2 "2007-08-03T12:29:04Z")

</div>

Are you compiling using gcc or g++? There are subtle differences between the two which can cause problems compiling C++ code with gcc. I’m not sure if that can cause problems compiling \<list\>, though.

---

<div class="post-metadata">

**Author:** ![VunderBob](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@VunderBob](https://boards.straightdope.com/u/VunderBob)\
**Post date:** [August 3, 2007, 12:31pm UTC](https://boards.straightdope.com/t/problems-with-gnu-c-compiler-and-template-classes/414272/3 "2007-08-03T12:31:04Z")

</div>

[QUOTE=Rysto]  
Are you compiling using gcc or g++? There are subtle differences between the two which can cause problems compiling C++ code with gcc. I’m not sure if that can cause problems compiling \<list\>, though.  
[/QUOTE]

G++ is the command…

---

<div class="post-metadata">

**Author:** ![Rysto](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rysto](https://boards.straightdope.com/u/Rysto)\
**Post date:** [August 3, 2007, 12:47pm UTC](https://boards.straightdope.com/t/problems-with-gnu-c-compiler-and-template-classes/414272/4 "2007-08-03T12:47:17Z")

</div>

Hm. What kind of errors are you getting? Also, you don’t have any syntax errors immediately preceding #include \<list\>, do you?

Something like this:

```auto

a
#include <list>

```

Would cause a weird error to show up in some header file.

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [August 3, 2007, 1:35pm UTC](https://boards.straightdope.com/t/problems-with-gnu-c-compiler-and-template-classes/414272/5 "2007-08-03T13:35:27Z")

</div>

Posting the smallest compilable code fragment which exhibits the problem and the exact error messages you get from trying to compile it (both within

```auto

```

tags) is usually helpful, if only because when narrowing down the problem you can sometimes find out how to solve it.

---

<div class="post-metadata">

**Author:** ![Pleonast](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/pleonast/32/1183_2.png) [@Pleonast](https://boards.straightdope.com/u/Pleonast)\
**Post date:** [August 3, 2007, 2:02pm UTC](https://boards.straightdope.com/t/problems-with-gnu-c-compiler-and-template-classes/414272/6 "2007-08-03T14:02:31Z")

</div>

Try reading the [G++ Manual](http://gcc.gnu.org/onlinedocs/). For templates in particular, read [this section](http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Template-Instantiation.html#Template-Instantiation). Templates can be nasty creatures to compile. I usually use the -frepo switch, but you need to understand what you’re code is trying to do to choose wisely.

Edit: Oh, if you do play around with the template options, be sure to delete all \*.o and \*.rpo files before recompiling. Your make file should have a clean option to do this easily.

---

<div class="post-metadata">

**Author:** ![Khadaji](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Khadaji](https://boards.straightdope.com/u/Khadaji)\
**Post date:** [August 3, 2007, 2:28pm UTC](https://boards.straightdope.com/t/problems-with-gnu-c-compiler-and-template-classes/414272/7 "2007-08-03T14:28:26Z")

</div>

[QUOTE=Rysto]  
Hm. What kind of errors are you getting? Also, you don’t have any syntax errors immediately preceding #include \<list\>, do you?

Something like this:

```auto

a
#include <list>

```

Would cause a weird error to show up in some header file.  
[/QUOTE]

Or along those lines, something in a previously included header might be conflicting with something in the \<list\> header.
