Solved

Problems linking MFC with an external C lib

Posted on 1998-02-19
6
2,071 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
GIF file bit manipulation for color change 5 107
Expand data scrubbing tool 13 32
Execute multiple curl cmds with sleep and send output to file 10 88
mapAB Challlenge 35 124
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

22 Experts available now in Live!

Get 1:1 Help Now