如何告诉编译器不优化某些代码? [英] How to tell compiler to NOT optimize certain code away?
问题描述
有没有办法告诉编译器(我的情况下是g ++)不优化某些代码,即使该代码不可达?我只需要在对象文件中的那些符号。
示例:这里是一个简单的函数,我希望这个函数被编译,即使它从未调用。 / p>
void foo(){
Foo< int& v;
}
如果没有官方编译器指令,认为它是一个重要的功能?或者至少让它认为它不能安全地被忽略?我尝试这样的:
extern bool bar;
void foo(){
if(bar){
Foo< int> v;
}
}
但似乎没有。 / p>
(如果你真的想知道为什么我在地球上会想要 - 它与这个问题,其中,而不是显式的模板实例化与模板类Foo< int>
我只想写 Foo< int> v
,因为在许多情况下,这更容易,因为它隐式实例化所有需要的函数,没有优化的调试模式...)
UPDATE:
我想做(作为一个可编译的微型示例):
foo.h (这样的文件给我, p>
模板< class T>
struct Foo {
T val_;
Foo(T val):val_(val){
//重码,长编译时间
}
};
foo-instantiation.cpp
#includefoo.h
void neverCalled(){
Foo< int& f(1);
}
//实例化的标准方法是:
// template class Foo< int>
//但在现实中,通常很难找出
//正确的是我要声明什么类型。
//用法如Foo< int> f(1);将实例化所有
//依赖类型。
foo-decl.h h)
模板< class T&
struct Foo {
T val_;
Foo(T val); //没有重的代码,可以包括任何地方并编译快速
};
main.cpp
#include< iostream>
#includefoo-decl.h
int main(int argc,char ** argv){
Foo< int> foo(1);
return 0;
}
编译(无优化)
g ++ -c main.cpp
g ++ -c foo-instantiation.cpp
g ++ main.o foo-instantiation。 oo
编译(优化)
g ++ -O2 -c main.cpp
g ++ -O2 -c foo-instantiation.cpp
g ++ main.o foo-instantiation.oo
main.o(.text + 0x13):在main函数中:
:未定义引用`Foo< int> :: Foo(int)'
collect2:ld返回1 exit status
- 我尝试了预编译头文件,但模板实例化方法更快的编译。
- 无需优化编译
foo-instantiation.cpp
不太理想,因为库代码(<$ c $ p>您正在使用单一定义规则。在一个文件中有一个定义:
模板< class T&
struct Foo {
T val_;
Foo(T val):val_(val){
//重码,长编译时间
}
};
和另一个不同的定义:
template< class T>
struct Foo {
T val_;
Foo(T val); //没有重的代码,可以包括任何地方并编译快速
};
这在C ++中是明确禁止的(只允许一个完全相同的定义)代码似乎有时似乎工作,但你实际上有的是可怕的未定义的行为 - 任何可能发生取决于月亮的阶段(但更可能是编译器在某些关键时刻的内部状态)。
基本上,你不能写这样的代码 - 对不起。
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.
You are running into the One Definition Rule. In one file you have a definition:
template<class T>
struct Foo {
T val_;
Foo(T val) : val_(val) {
// heavy code, long compile times
}
};
and in another a different definition:
template<class T>
struct Foo {
T val_;
Foo(T val); // no heavy code, can include anywhere and compile fast
};
This is explicitly not allowed in C++ (only one identical definition allowed) and if you break the rule your code may well seem to work sometimes, but what you actually have is the dreaded "undefined behaviour" - anything may happen depending on the phase of the moon (but more likely the internal state of the compiler at certain critical junctures).
Basically, you can't write code like that - sorry.
这篇关于如何告诉编译器不优化某些代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!