Template binding does not work on -O2, but does work on -O0

I wrote a C ++ program linked with cmake and running on a Debian machine. I am happy to say that all known major bugs have been fixed and I feel ready to rid it of

add_definitions(-Wall)
set(CMAKE_BUILD_TYPE Debug)
add_definitions("-O0 -std=c++0x")

      

before

add_definitions("-O2 -std=c++0x")

      

I did just that, and suddenly a templated class appeared in my project (naively declared in some header file and defined for all use cases in some separate .cpps) breaks with a load of "undefined" errors during connection.

Some research on this forum has yielded topics like this one: Why templates can only be implemented in a header file? I believe I can see Luke in his famous answer at: The compiler actually "writes the code" for each required version of the templated class (i.e. T = int, T = float, T = what_ever_else) and for that purpose access the actual implementation.

Fair enough. But what completely puzzles me here: this appears to be the only problem when trying to optimize compilation. With "-00" everything works fine. How can it be?

application

1.) About Duplicate Suspect: The issue here is not that I did it wrong. My sin is clear: I have defined templated functions outside the reach of the header file, which apparently is not allowed for the reasons stated above and inside the post I already quoted.

Considering the truth of the quoted post, it is obvious that my program cannot compile. I don't understand why it compiles with -O0.

2.) nos requested some obvious bugs. The ending of my last attempt using -O2 was still in my terminal buffer (sorry, it takes about an hour to compile and hence not easy to reproduce). But since it breaks during communication. Here goes what I can still hover over:

/home/kochmn/projects/free_sentinel_gl/sentinel/src/game/game.cpp:432: undefined reference to `game::Board<game::Figure>::get(QPoint)'
libqt.a(game.cpp.o): In function `game::Game::hyperspace_jump()':
/home/kochmn/projects/free_sentinel_gl/sentinel/src/game/game.cpp:950: undefined reference to `game::Board<game::Square>::get(QPoint)'

[... many more like that all concerned about the templated class "Board"
  which is declared in landscape.h, defined in landscape.cpp and
  used pretty much everywhere else ... ]

collect2: error: ld returned 1 exit status
CMakeFiles/sentinel.dir/build.make:548: recipe for target 'sentinel' failed
make[2]: *** [sentinel] Error 1
CMakeFiles/Makefile2:127: recipe for target 'CMakeFiles/sentinel.dir/all' failed
make[1]: *** [CMakeFiles/sentinel.dir/all] Error 2
Makefile:76: recipe for target 'all' failed
make: *** [all] Error 2

      

3.) Regarding the question with the comment "How are use cases defined?": Let's take advantage of the fact that it's on GitHub.

All use cases are defined here: https://github.com/kochsoft/free_sentinel_gl/blob/master/sentinel/src/game/landscape.cpp#L507 starting at line 507.

The header is here: https://github.com/kochsoft/free_sentinel_gl/blob/master/sentinel/src/include/landscape.h#L363 corresponding pass starting at line 363.

+3


source to share


1 answer


My problem was resolved and dyp was closest to his / her comment. The magic word is "explicit template establishment" and can be found here: http://www.cplusplus.com/forum/articles/14272/

I have NOT cluttered my header files with definitions. I also didn't add any code like #include "some.cpp" in the header, which in my opinion hasn't changed. Instead, I added the lines

template class Board<Figure>; 
template class Board<Square>;

      

right below the definitions in landscape.cpp and was able to compile with -O2.



According to Why templates can only be implemented in a header file? I won't be able to use anything other than shapes and squares on my boards (although that works for me).

As for why -O0 works: as pointed out in the comment above, the optimization compiler has probably removed the piece of code that under -00 was used to generate the template. Probably ... I still do not claim that all this is fully understood.

@dyp: Why don't you post a definitive answer to this question without elaborating on your previous comment?

+2


source







All Articles