Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell VC++ optimizer not to merge function bodies?

Consider the following code:

#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;
}

When compiled in Release configuration in VS2013 it prints out “equal”. I have a library that depends on comparison of function addresses. You can imagine that it doesn't work quite alright in Release. Is there a way to prevent this kind of optimization in VC++? Or should I file a bug?

like image 798
facetus Avatar asked Aug 25 '14 23:08

facetus


1 Answers

This is a "feature" of Microsoft's linker, and the documentation warns you that

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.

You can turn it off by passing /opt:noicf to the linker.

like image 70
T.C. Avatar answered Nov 15 '22 23:11

T.C.