0

I am trying to export two functions of the same signature from a C++ dll. As I don't want names to be mangled, I am using extern "C". However, when I open a dll in depedency walker, I could see that the entry points for both of the functions are same, any idea why?

Code as below: Header.h

#pragma once

extern "C"
{
    void _cdecl TestFunc1();
    void _cdecl TestFunc2();
}

Header.cpp

#include "Header.h"

void TestFunc1()
{
    int i = 0;
}

void TestFunc2()
{
    int i = 0;
}

Module defination file:

EXPORTS
    TestFunc1
    TestFunc2

This is what I got in dependecy walker enter image description here

Neo
  • 131
  • 8
  • Either something isn't meshing with your posted code or you have a very bodacious optimizer. Does this happen *regardless* of the content of the functions? – WhozCraig Mar 22 '15 at 20:41
  • 1
    I'd try using different definitions for `TestFunc1` and `TestFunc2`, just in case there is a sneaky optimizer config that checks for dupplicates and links to the first occurence. – Frederik.L Mar 22 '15 at 20:46
  • I am using visual studio 2013 – Neo Mar 22 '15 at 20:48
  • @WhozCraig : Yes it does – Neo Mar 22 '15 at 20:49
  • possible duplicate of [Why do two functions have the same address?](http://stackoverflow.com/questions/9323273/why-do-two-functions-have-the-same-address) – Raymond Chen Mar 22 '15 at 21:05
  • looks like the compiler noticed that your two functions are the same and simply merged them. To avoid that, you could add some dummy code to try and make the compiler generate different bodies. – kuroi neko Mar 22 '15 at 21:08
  • @kuroineko : It is actually a linker who was optimising the code, I manged to solve this with linker setting as posted below in answer. Thanks. – Neo Mar 22 '15 at 21:14

1 Answers1

0

In visual studio, setting Linker -> Optimization -> Enable COMDAT Folding to No (/OPT:NOICF) worked. Here MSDN says:

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. For more information, see /Gy (Enable Function-Level Linking).

Need to read more what this linker flag does exactly.

Neo
  • 131
  • 8