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
Solved

Problems linking MFC with an external C lib

Posted on 1998-02-19
6
2,079 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
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.
0
 

Author Comment

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

 
0
 
LVL 6

Expert Comment

by:jpk041897
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
#else
...

#endif _yourlibname

a header file containing this type of code is usualy used to solve conflicts of the type you describe.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Expert Comment

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

0
 

Accepted Solution

by:
Beatachon earned 50 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

0
 

Expert Comment

by:Beatachon
ID: 1316406
Thank you!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

790 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