c++ - 如何告诉 VC++ 优化器不要合并函数体?

标签 c++ visual-studio-2013

考虑以下代码:

#include <iostream>

struct A {};

struct B {};

int func1(A *a, B *b, int c, double *d) {
    int tmp = 0;
    tmp = tmp;

    return 1;
}

int func2(A *a, B *b, int c, double *d) {
    return 1;
}

int main(int argc, char* argv[]) {
    if (func1 == func2) {
        std::cout << "equal" << std::endl;
    } else {
        std::cout << "not equal" << std::endl;
    }

    return 0;
}

在 VS2013 的 Release 配置中编译时,它会打印出“equal”。我有一个依赖于函数地址比较的库。你可以想象它在 Release 中不能正常工作。有没有办法在 VC++ 中防止这种优化?还是我应该提交错误?

最佳答案

这是 "feature" Microsoft 的链接器,并且文档警告您

Because /OPT:ICF can cause the same address to be assigned to different functions or read-only data members (const variables compiled by using /Gy), it can break a program that depends on unique addresses for functions or read-only data members.

您可以通过将 /opt:noicf 传递给链接器来关闭它。

关于c++ - 如何告诉 VC++ 优化器不要合并函数体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25495978/

相关文章:

c++ - 依赖于模板的基本成员未正确解析

assembly - 如何在 Visual Studio 2013 中启用汇编语言支持

git - checkin TFS2013 源代码控制会触发与单独的 TFS Git 存储库关联的 CI 构建

Azure API应用程序代理生成错误

c++ - 如何强制 gcc 从库中链接未引用的静态 C++ 对象

c++ - 如何在不并行的情况下提高我的反向传播 ANN 的性能

c++ - 转换为 static_cast<unsigned char>

c++ - 我如何让一个进程在 linux 中重新加载自己?

c++ - <无法读取内存> c++

c++ - 具有嵌套模板类的模板类,g++ 编译正常,vs2013 不会编译