简体   繁体   English

使用不同优化级别编译的不同翻译单元中的模板实例化

[英]Template instantiation in different translation units compiled with different optimization levels

Suppose I have two translation units, both of which use std::string . 假设我有两个翻译单元,都使用std::string I compile one of them with -O3 , and the other without optimizations, then link the result together. 我用-O3编译其中一个,而没有优化则编译另一个,然后将结果链接在一起。 Both object files would contain instantiated std::string , but I would expect one version to be compiled with optimizations, and the other one without. 两个目标文件都将包含实例化的std::string ,但是我希望一个版本可以进行优化编译,而另一个版本则不进行优化。 Would the linker pick only one of those during linking? 链接程序在链接过程中只会选择其中之一吗? If so, which one? 如果是这样,哪一个? Will the resulting executable be guaranteed to always run properly? 是否可以保证生成的可执行文件始终正常运行?

Update: as this looks to be implementation-specific, it would make sense to narrow this down to gcc and clang behavior, since they both implement well-defined and concrete Itanium ABI, rather than trying to approach this from the point of view of the C++ standard itself. 更新:由于这看起来是特定于实现的,因此将其缩小到gcc和clang行为是有道理的,因为它们都实现了定义明确且具体的Itanium ABI,而不是试图从实现的角度来解决这个问题。 C ++标准本身。

As far as the C++ standard goes, this is probably as way out in far left field as it possibly can. 就C ++标准而言,这可能是在最左侧的领域中可能的解决之道。 For starters, the C++ standard has nothing to say about "optimization levels", or any of that. 对于初学者来说,C ++标准没有任何关于“优化级别”的说法。

So, this falls entirely within the scope of "implementation defined". 因此,这完全属于“定义的实现”的范围。 The answer depends on the exact compiler/linker and the compiler/linker version you're using. 答案取决于确切的编译器/链接器以及您使用的编译器/链接器版本。 The correct answer for one compiler or linker will be applicable only to that compiler or linker. 一个编译器或链接器的正确答案仅适用于该编译器或链接器。 A different compiler, or even a different version of the same compiler, can produce different results. 不同的编译器,甚至同一编译器的不同版本,都可能产生不同的结果。

I will expect one of two results: 我期待以下两个结果之一:

A) The linker will complain about merging non-identical segments, or A)链接器会抱怨合并不同的段,或者

B) One or the other will be picked at random. B)一个或另一个将被随机挑选。 Probably whichever first or last translation unit is passed to the linker. 可能是第一个翻译单元或最后一个翻译单元被传递到链接器。

So, in conclusion, the only way to know the answer to this question is for you to try it with whatever compiler or linker you're using, and examine the results. 因此,总而言之,知道该问题答案的唯一方法是让您使用正在使用的任何编译器或链接器进行尝试,然后检查结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不同翻译单元中模板类静态变量的显式实例化 - explicit instantiation of static variable of a template class in different translation units 多个翻译单元中函数模板实例化的标识 - Identity of function template instantiation in multiple translation units 您可以链接使用不同优化级别编译的目标文件吗? - Can you link object files that were compiled with different optimization levels? 如果模板在不同类型的多个翻译单元中定义,则 ODR 冲突? - ODR violation if template is defined in multiple translation units for different types? 不同翻译单元中的多个定义 - Multiple definitions in different translation units 在不同的翻译单元中定义的类 - Class defined in different translation units 内联函数在不同翻译单元中的不同实现 - Different implementations of inline functions in different translation units 是否在C ++中的不同编译单元中多次编译模板类? - Is a template class compiled more than once in different compilation units in C++? 内联函数在不同的转换单元中具有不同的编译器标志未定义的行为? - inline function in different translation units with different compiler flags undefined behaviour? C ++:不同翻译单元中具有相同名称的不同类 - C++: Different classes with the same name in different translation units
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM