Solved

Linker errors when compiling a DLL in VC++ 2005 EE.

Posted on 2007-03-17
8
1,161 Views
Last Modified: 2013-12-04
Using Visual C++ 2005 Express, I'm writing a multi-threaded DLL, currently in the debug configuration (/MDd).

I'm trying to create a thread using _beginthreadex().  (I was using CreateThread() before, but I discovered a couple of web pages that advised against it.)

My relevant code is:

      class  _declspec(dllimport) MyClass {
            public:
                                          MyClass();
                  virtual                        ~McPerforceHud();
            private:
                  static unsigned WINAPI            thread(void*);
                  HANDLE                              hThread;
                  unsigned*                        threadID;
      };

MyClass::MyClass() {
      hThread = (HANDLE)_beginthreadex(NULL, 0, &thread, (void*)this, THREAD_PRIORITY_NORMAL, threadID);
}

unsigned WINAPI MyClass::thread(void* param) {
}


... and the result:


error LNK2019: unresolved external symbol __imp___beginthreadex referenced in function "public: __thiscall MyClass::MyClass(void)" (??0MyClass@@QAE@XZ)
error LNK2019: unresolved external symbol "__declspec(dllimport) const MyClass::`vftable'" (__imp_??_7MyClass@@6B@) referenced in function "public: __thiscall MyClass::MyClass(void)" (??0MyClass@@QAE@XZ)


Note that when I used CreateThread(), my DLL compiled without errors.  Only when I switched to _beginthreadex() did I start getting this Linker issues.


-- M. Cooper
0
Comment
Question by:MiloDC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 18741140
Set the runtime library to 'multithreaded' (under Project Settings|C++|Code generation) and you'll get rid of that problem.
0
 

Author Comment

by:MiloDC
ID: 18741206
jkr wrote: "Set the runtime library to 'multithreaded' (under Project Settings|C++|Code generation) and you'll get rid of that problem."

I *knew* someone was going to reply with that.

As I wrote (first thing):

"Using Visual C++ 2005 Express, I'm writing a multi-threaded DLL, currently in the debug configuration (/MDd)."

/Mdd is "multi-threaded debug DLL."

If you mean that I should switch to /MT instead of /MDd, then I get even more errors when I try that.


-- M. Cooper
0
 

Author Comment

by:MiloDC
ID: 18742324
OK, I've managed to whack the second error:


error LNK2019: unresolved external symbol "__declspec(dllimport) const MyClass::`vftable'" (__imp_??_7MyClass@@6B@) referenced in function "public: __thiscall MyClass::MyClass(void)" (??0MyClass@@QAE@XZ)


Problem there was that I was declaring the destructor as a virtual function (silly me).

The first error (the one that has really given me a headache) persists.  I cannot figure out why _beginthreadex() isn't recognized by the linker.  (Yes, I have included process.h.)

I wonder if this is a mixed DLL issue?


-- M. Cooper
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:MiloDC
ID: 18742430
Further progress:

I added msvcrtd.lib to my dependency list.  That eliminated the __imp__ error.

Problem now is that msvcrtd.lib is clashing with libcmt.lib, causing "LNK2005: ... already defined in libcmt.lib" errors.

Adding "libcmt.lib" to the "Ignore Specific Library" field doesn't get me past this problem.

ARGH!


-- M. Cooper
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 18742882
I think you have done this, Just confirming....
In project settings->link tab->category slelect 'Input'->under ignore libraries->
put one of the library which is clashing.

Is this what you did?
Best Regards,
DeepuAbrahamK
0
 

Author Comment

by:MiloDC
ID: 18742918
DeepuAbrahamK: "I think you have done this, Just confirming....
In project settings->link tab->category slelect 'Input'->under ignore libraries->
put one of the library which is clashing."

Hey man, yeah, I tried that, with the result that I traded one type of error for another.  Solving a problem just seems to lead to another one, ad infinitum.

Here's the story so far:

To get rid of the __imp__ error, I added "msvcrtd.lib" to my dependencies.  That leads to a bunch of "msvcrtd.lib(MSVCR80D.dll) : error LNK2005: [something] already defined in libcmt.lib" errors.

To get rid of *these* problems, I remove libcmt.lib from my dependencies.  That, in turn, leads to several "libsupp.lib([something].obj) : error LNK2001: unresolved external symbol __iob" errors.

This is just silly.  There's got to be some way for me to resolve declarations made in multiple libraries.


-- M. Cooper
0
 

Author Comment

by:MiloDC
ID: 18742958
I give up.  Looks like msvcrt(d).lib and libcmt(d).lib are incompatible, i.e. you can have one or the other but not both.  Other code in my project apparently relies on libcmt(d).lib, so I guess I'm stuck with that.


-- M. Cooper
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 125 total points
ID: 18747595
>>>> I give up.  Looks like msvcrt(d).lib and libcmt(d).lib are incompatible
Not exactly, but they are defining the same modules so it is important which was first and which was second.

Generally, if using msvcrt you don't need libcmt. The beginthreadex is included in msvcrt as well but you need to have the multithreading option with all modules you are using. If you are using a dll or static library (from a third-party) where the multi-threading isn't set you'll get the problems as you described.

Did you use precompiled headers (PCH)? If yes, this might be the reason why your change from single-threaded to multi-threaded didn't work properly. Unfortunately precompiled header files often were not recompiled after changing the settings (or not all obejcts relying on PCH were recompiled to use the new PCH)., so that via PCH you might still have the old settings not containing the multi-threaded switch.

Anyhow, to solve the problem you first should remove the libcmt(d) lib. Then check that all projects and sub projects have the multed-threaded property in code generation both for debung and release. Note, dlls have as default single-threaded (unfortunately) when created by a wizard. After that make a clean on *all* projects and rebuild them regarding the correct dependencies. If you still get linker errors you should check the object modules that were related to the linker errors. Post the linker settings of the projects the modules belong to.


Regards, Alex
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question