Problems linking MFC with an external C lib

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










nielsewAsked:
Who is Participating?
 
BeatachonConnect With a Mentor Commented:
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
 
Tommy HuiEngineerCommented:
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
 
nielsewAuthor Commented:
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
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.

 
jpk041897Commented:
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
 
yingchunliCommented:
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
 
BeatachonCommented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.