1

I have a visual c++ project in my work space which is totally depend on .lib (static library). Now i want to create a dll project using the existing code in visual c++ but it shows me following linking error:

Linking...
 msvcrt.lib(MSVCRT.dll) : error LNK2005: "public: virtual __thiscall exception::~exception(void)" (??1exception@@UAE@XZ) already defined in LIBCMTD.lib(stdexcpt.obj)
 msvcrt.lib(MSVCRT.dll) : error LNK2005: "public: __thiscall exception::exception(char const * const &)" (??0exception@@QAE@ABQBD@Z) already defined in LIBCMTD.lib(stdexcpt.obj)
 msvcrt.lib(MSVCRT.dll) : error LNK2005: _free already defined in LIBCMTD.lib(dbgheap.obj)
 msvcrt.lib(MSVCRT.dll) : error LNK2005: _malloc already defined in LIBCMTD.lib(dbgheap.obj)
 LINK : warning LNK4098: defaultlib "msvcrt.lib" conflicts with use of other libs; use   /NODEFAULTLIB:library
 Debug/finaliTest.dll : fatal error LNK1169: one or more multiply defined symbols found
 Error executing link.exe.

I am newbie in this visual C++. How should i procced?

  • code for DllMain:

    #include "stdafx.h"
    #include "IDT_DUKPT.h"
    unsigned char stringKSN[10];
    unsigned char m_nderivation_key[16];
    unsigned char m_ninitial_key[16];
    
     BOOL APIENTRY DllMain( HANDLE hModule, 
                    DWORD  ul_reason_for_call, 
                    LPVOID lpReserved
                 )
    {   
       return TRUE;
    }  
    
     void OnDecryption(){
    
       GetKey(stringKSN, m_nderivation_key, m_ninitial_key);   
       // Initialization of the method are written in `.lib` file.
    
       }
    

Where as the IDT_DUKPT.H is:

//IDT_DUKPT.h
#define _IDT_DUKPT_H_


// TDES Encryption
void TDES_Encryption(BYTE *Data, BYTE *Key, BYTE *InitalVector, int Length);

// TDES Decryption
void TDES(BYTE *Data, BYTE *Key, BYTE *InitalVector, int Length);

// Get the Initial Key
void GetKey(BYTE *KSN, BYTE *BDK, BYTE *InitialKey);

I also put the IDT_DUKPT.lib in my project folder and add the .lib link to the project setting.

My main aim is to create a dll, So that i can use the methods from my java code using JNA.

`

Amit Pal
  • 10,604
  • 26
  • 80
  • 160

2 Answers2

2

It sounds like you're mixing objects that were compiled with different options for C run-times. Is IDT_DUKPT.lib a static library (a collection of object files rather than an import library for a separate DLL)? If it is, I'd guess one was compiled using /MTd while the other is using the /MD option.

See http://msdn.microsoft.com/en-us/library/abx4dbyh(v=vs.80).aspx for details.

There are a few ways you could fix this. The easiest is probably to change the compiler flags for your app to use whichever of /MDd or /MTd it wasn't already using:

  1. Right click on the relevant csproj in Solution Explorer and select Properties
  2. In the dialog that appears, expand C/C++ then select Command Line
  3. In the options box on the right of the dialog, add /MTd or /MDd
  4. For completeness, change the configuration to Release and add /MT or /MD to its compiler options
simonc
  • 41,632
  • 12
  • 85
  • 103
  • Yes you are right .lib file is a static library. Would you please tell me how to change the compiler? – Amit Pal Dec 19 '12 at 11:33
  • @AmitPal I've updated my answer with notes on this. I'm not a big user of the visual studio IDE so there may be other, easier, routes to achieve the same result – simonc Dec 19 '12 at 11:40
  • @simoc : Actually i am on Microsoft visual c++ and you are talking about visual studio 2010 – Amit Pal Dec 19 '12 at 11:45
  • @AmitPal I don't have any other versions of msvc available to offer instructions. If you hunt through project properties menu options you should find a comparable option under something like "compiler flags", "cflags" or "command line" – simonc Dec 19 '12 at 11:48
  • Maybe the options have been used the other way round. Can you try changing to use the /MD option (debug, single threaded) in your app please? – simonc Dec 19 '12 at 12:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21386/discussion-between-simonc-and-amit-pal) – simonc Dec 19 '12 at 13:00
0

Have you tried creating the new DLL, then add each file in the hierarchy, after each addition, compile?

Remember, when you are adding a DLL, the exporting comes into picture. You will not be able directly add like that.

Naresh
  • 658
  • 1
  • 7
  • 22
  • Yeh i did that but it was showing me errors and when i searched for them it tell that i have already created a dynamic and now i am adding a static library. not sure – Amit Pal Dec 19 '12 at 11:35
  • Try mentioning LIBCMTD.lib under linker->Input-> 'Ignore Specific Default Libraries' – Naresh Dec 19 '12 at 11:42
  • @Naresh Introducing a second C run-time is likely to increase the number of linker errors - every standard C call will end up generating a duplicate symbol error – simonc Dec 19 '12 at 12:15