Click here to Skip to main content
15,890,438 members
Articles / Programming Languages / C++
Alternative
Tip/Trick

How to build an image (DLL/EXE) when two of its included libraries have the same symbol (say function/variable) using VC++

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
24 Jan 2012CPOL 9.1K   1   1
If the .lib files you're linking to represent some .dll files somewhere else, you can do the following:void main(){ FunB(); // from external_1.lib FunC(); // from external_2.lib // FunA(); // from external_1.lib / external_2.lib ???? typedef void (WINAPI *...
If the .lib files you're linking to represent some .dll files somewhere else, you can do the following:

C++
void main()
{
   FunB(); // from external_1.lib
   FunC();  // from external_2.lib
  
  // FunA();  // from external_1.lib / external_2.lib ????
   typedef void (WINAPI * funa_ptr)(void);
   funa_ptr pfunA = ::GetProcAddress(::GetModuleHandle(_T("external_1.dll")), _T("FunA"));
   if (NULL == pfunA)
   {
     // TODO: Handle the error.
   }
   pfunA();
}



  • The linker should not complain, since the conflicting functions are not statically linked.
  • ::GetModuleHandle should work, since both DLLs were statically linked (and loaded before the .exe where main() is defined)


If the .lib files are static libraries, you can encapsulate one of them within a DLL of your own, replacing the functions by renamed versions:

C++
// In MyDll.cpp: link to external_1.lib
void MyFunA()
{
   FunA();
}
void MyFunB()
{
   FunB();
}

You may link your executable to external_2.lib, and your DLL to external_1.lib, thus avoiding name clashes.
Hope this helps.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Israel Israel
Pablo writes code for a living, in C++, C#, and SQL.

To make all that work easier, he uses some C++ libraries: STL, ATL & WTL (to write Windows applications), and code generation.

Pablo was born in 1963, got married in 1998, and is the proud father of two wonderful girls.

Favorite quotes:
"Accident: An inevitable occurrence due to the action of immutable natural laws." (Ambrose Bierce, "The Devil's Dictionary", published in several newspapers between 1881 and 1906).
"You are to act in the light of experience as guided by intelligence" (Rex Stout, "In the Best Families", 1950).

Comments and Discussions

 
GeneralThanks for the elaboration. I too have suggested the same so... Pin
Lakamraju Raghuram24-Jan-12 0:47
Lakamraju Raghuram24-Jan-12 0:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.