[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Problems linking MFC with an external C lib

Posted on 1998-02-19
Medium Priority
Last Modified: 2013-11-20
Using the  AppWiz in VC++ 4.2  I have generated program using only the default settings.
(This program links and runs fine)

The only change I made to the default project setting was to add the name of the external lib to the link section of the project settings.

When I try to use a function from this external library I get the following link errors:

LIBC.lib(crt0dat.obj) : error LNK2005: _exit already defined in msvcrtd.lib(MSVCRTD.dll)
LIBC.lib(crt0dat.obj) : error LNK2005: __exit already defined in msvcrtd.lib(MSVCRTD.dll)
LINK : warning LNK4098: defaultlib "LIBC" conflicts with use of other libs; use /NODEFAULTLIB:library

If I change project settings to ignore libc.lib I get the following link errors:

protoolkit.lib(apfutil.obj) : error LNK2001: unresolved external symbol _errno
protoolkit.lib(session.obj) : error LNK2001: unresolved external symbol _errno
protoolkit.lib(genutil1.obj) : error LNK2001: unresolved external symbol __pctype
protoolkit.lib(ctsysutil.obj) : error LNK2001: unresolved external symbol __pctype
protoolkit.lib(windows_32.obj) : error LNK2001: unresolved external symbol __pctype
protoolkit.lib(genutil1.obj) : error LNK2001: unresolved external symbol ___mb_cur_max
protoolkit.lib(cu_appid.obj) : error LNK2001: unresolved external symbol ___mb_cur_max
protoolkit.lib(rpctool.obj) : error LNK2001: unresolved external symbol __sys_nerr
Debug/test1.exe : fatal error LNK1120: 4 unresolved externals
Error executing link.exe.
test1.exe - 27 error(s), 0 warning(s)

The external lib works fine if I use a console app with no MFC calls.

The vender (PTC) says this should work fine but has not tried it??????

Any ideas?


Question by:nielsew
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
LVL 15

Expert Comment

by:Tommy Hui
ID: 1316401
The external library you're linking with was compiled with static libraries as the option. With MFC, you are using DLL model, which is causing the conflict. You have two solutions. Use static for everything or you can use the linker options to ignore the LIBC.LIB.

Author Comment

ID: 1316402
Thanks for your suggestion but...

When I switch to "Use MFC in a Static Library". The project settings switches a compiler flag to /ML which includes libc.lib.
This results in the same types of conflicts.  

Also, I tried the dll version of the external libs with about the same results.

I found I can compile and link if I ignore msvcrtd.lib but I loose access to very basic fuction like strtok .. etc.


Expert Comment

ID: 1316403
Does the external library have an associate header file, and if so, does it contain lines that look something like:

#ifndef _yourlibname
#define _yourlibname

#endif _yourlibname

a header file containing this type of code is usualy used to solve conflicts of the type you describe.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1316404
When the LIBC.lib was generated, either "single-threaded" or "multithreaded libraries" was selected. If the selection in "Build"-> "Setting"->"C/C++"->"Code generation" in your project is not  the right one,  it may cause the problems.  


Accepted Solution

Beatachon earned 200 total points
ID: 1316405
Got this from VC++ 5.0 help files:

Linker Tools Warning LNK4098
defaultlib "library" conflicts with use of other libs; use /NODEFAULTLIB:library

You are trying to link with incompatible libraries.

Important The run-time libraries now contain directives to prevent mixing different types. You'll receive this warning if you try to use different types or debug and non-debug versions of the run-time library in the same program. For example, if you compiled one file to use one kind of run-time library and another file to use another kind (for example, single-threaded versus multithreaded) and tried to link them, you'll get this warning. You should compile all source files to use the same run-time library. See the Use Run-Time Library (MD, /ML, /MT, /LD) compiler options for more information.

You can use the linker's /VERBOSE:LIB switch to determine which libraries the linker is searching. If you receive LNK4098 and want to create an executable file that uses, for example, the single-threaded, non-debug run-time libraries, use the /VERBOSE:LIB option to find out which libraries the linker is searching. The linker should print LIBC.LIB and not LIBCMT.LIB, MSVCRT.LIB, LIBCD.LIB, LIBCMTD.LIB, or MSVCRTD.LIB as the libraries searched. You can tell the linker to ignore the the incorrect run-time libraries by typing the incorrect libraries in the Ignore Libraries text box on the Link tab of the Settings dialog box in Developer's Studio or by using the /NODEFAULTLIB:library option with LINK for each library you want to ignore. See the Ignore Libraries (/NODEFAULTLIB) linker option for more information.

The table below shows which libraries should be ignored depending on which run-time library you want to use.

To use this run-time library
 Ignore these libraries

Single-threaded (libc.lib)
 libcmt.lib, msvcrt.lib, libcd.lib, libcmtd.lib, msvcrtd.lib
Multithreaded (libcmt.lib)
 libc.lib, msvcrt.lib, libcd.lib, libcmtd.lib, msvcrtd.lib
Multithreaded using DLL (msvcrt.lib)
 libc.lib, libcmt.lib, libcd.lib, libcmtd.lib, msvcrtd.lib
Debug Single-threaded (libcd.lib)
 libc.lib, libcmt.lib, msvcrt.lib, libcmtd.lib, msvcrtd.lib
Debug Multithreaded (libcmtd.lib)
 libc.lib, libcmt.lib, msvcrt.lib, libcd.lib, msvcrtd.lib
Debug Multithreaded using DLL (msvcrtd.lib)
 libc.lib, libcmt.lib, msvcrt.lib, libcd.lib, libcmtd.lib

For example, if you received this warning and you want to create an executable file that uses the non-debug, single-threaded version of the run-time libraries, you could use the following options with the linker:

/NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:msvcrt.lib /NODEFAULTLIB:libcd.lib /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:msvcrtd.lib


Expert Comment

ID: 1316406
Thank you!

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

656 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