Solved

Problems linking MFC with an external C lib

Posted on 1998-02-19
6
2,056 Views
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:

Linking...
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:

Linking...
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?

Eric










0
Comment
Question by:nielsew
6 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
Comment Utility
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.
0
 

Author Comment

by:nielsew
Comment Utility
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.

 
0
 
LVL 6

Expert Comment

by:jpk041897
Comment Utility
Does the external library have an associate header file, and if so, does it contain lines that look something like:

#ifndef _yourlibname
#define _yourlibname
#else
...

#endif _yourlibname

a header file containing this type of code is usualy used to solve conflicts of the type you describe.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Expert Comment

by:yingchunli
Comment Utility
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.  

0
 

Accepted Solution

by:
Beatachon earned 50 total points
Comment Utility
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

0
 

Expert Comment

by:Beatachon
Comment Utility
Thank you!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
paragon account problem 9 96
Process filename extension 3 154
Smart Camera scanning and reading information 3 86
Window placement 17 64
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now