Is there a way to tell the compiler (g++ in my case) to not optimize certain code away, even if that code is not reachable? I just want those symbols in the object file.
Example: Here is a simple function, and I do want this function to be compiled, even if it's never called.
void foo(){
Foo<int> v;
}
If there is no official compiler directive, is there a trick to make the compiler think that it's an important function? Or at least make it think that it can't safely be ignored? I tried something like this:
extern bool bar;
void foo(){
if(bar){
Foo<int> v;
}
}
but that didn't seem to do it.
(If you really want to know why I on earth would want that -- it has to do with this question, where, instead of explicit template instantiation with template class Foo<int>
I simply want to be able to write Foo<int> v
, since in many cases that's easier since it implicitly instantiates all functions needed, and it does work fine in debug mode without optimizations ...)
UPDATE:
Here is what I want to do (as a compilable mini example):
foo.h (such files are given to me and not changeable)
template<class T>
struct Foo {
T val_;
Foo(T val) : val_(val) {
// heavy code, long compile times
}
};
foo-instantiation.cpp
#include "foo.h"
void neverCalled() {
Foo<int> f(1);
}
// The standard way to instantiate it is this:
// template class Foo<int>;
// but in reality it is often hard to find out
// exactly what types I have to declare.
// Usage like Foo<int> f(1); will instantiate all
// dependent types if necessary.
foo-decl.h (an interface that I extracted from foo.h)
template<class T>
struct Foo {
T val_;
Foo(T val); // no heavy code, can include anywhere and compile fast
};
main.cpp
#include <iostream>
#include "foo-decl.h"
int main(int argc, char** argv){
Foo<int> foo(1);
return 0;
}
Compilation (no optimization)
g++ -c main.cpp
g++ -c foo-instantiation.cpp
g++ main.o foo-instantiation.oo
Compilation (optimization)
g++ -O2 -c main.cpp
g++ -O2 -c foo-instantiation.cpp
g++ main.o foo-instantiation.oo
main.o(.text+0x13): In function `main':
: undefined reference to `Foo<int>::Foo(int)'
collect2: ld returned 1 exit status
- I tried precompiled headers instead, but the template instantiation method makes for much faster compilation.
- Compiling
foo-instantiation.cpp
without optimization is not so ideal because then the library code (foo.h
and others) will run slower.