Solved

problem with linker errors (with fstream) in VC .net

Posted on 2004-08-01
5
1,532 Views
Last Modified: 2013-12-14
hi.  I really need help with this one... The damn thing was compiling and linking just fine... now for some reason i have linker errors when i try to use fstream.  I made NO edits that immediately resulted in breaking my program...  so i'm totally confused.  My project compiles fine if i remove everything regarding fstream.  But as soon as i try to declare an fstream object, i get the following 9 linker errors:

solution3 error LNK2019: unresolved external symbol __malloc_dbg referenced in function "void * __cdecl operator new(unsigned int,struct std::_DebugHeapTag_t const &,char *,int)" (??2@YAPAXIABU_DebugHeapTag_t@std@@PADH@Z)
solution3 error LNK2001: unresolved external symbol __malloc_dbg
solution3 error LNK2001: unresolved external symbol __malloc_dbg
solution3 error LNK2019: unresolved external symbol __malloc_dbg referenced in function __Wcsxfrm
solution3 error LNK2019: unresolved external symbol __free_dbg referenced in function "void __cdecl operator delete(void *,struct std::_DebugHeapTag_t const &,char *,int)" (??3@YAXPAXABU_DebugHeapTag_t@std@@PADH@Z)
solution3 error LNK2001: unresolved external symbol __free_dbg
solution3 error LNK2019: unresolved external symbol __realloc_dbg referenced in function "private: void __thiscall std::locale::_Locimp::_Addfac(class std::locale::facet *,unsigned int)" (?_Addfac@_Locimp@locale@std@@AAEXPAVfacet@23@I@Z)
solution3 error LNK2019: unresolved external symbol __CrtDbgReport referenced in function __Mbrtowc
solution3 fatal error LNK1120: 4 unresolved externals


my inclusions look like this:

#include <windows.h>   // include important windows stuff
#include <windowsx.h>
#include <mmsystem.h>
//#include <iostream> // include important C/C++ stuff
//#include <conio.h>
//#include <stdlib.h>
//#include <malloc.h>
//#include <memory.h>
//#include <string.h>
//#include <stdarg.h>
//#include <stdio.h>
//#include <math.h>
//#include <io.h>
//#include <fcntl.h>
#include <time.h>
#include <fstream>
//#include <stdlib.h>
using namespace std;

the includes that are commented out were NOT commented out before... and commenting them out did nothing to help my situation.  I know about the namespace issues... but I believe i have the correct namespace accessess here.   Any ideas?

Could it potentially be a broken dll?  Will re-acquiring the fstream libraries from microsoft help out at all?   reinstalling .net?

for reference:
windows XP pro, VC .net.  the project is being written with win32 and Direct9.0 in c++.  I'm using an older library of directX wrapper functions (I believe these might be causing the problem)  I have had a lot of trouble trying to include these in the right order and in the right way.  .net seems to be extremely anal about inclusions.


please help!

thanks,
chris
0
Comment
Question by:mordekai
  • 2
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 350 total points
ID: 11691455
Do you compile with Precompiled Headers? If yes, commenting header includes DOES NOT change anything as you have to recompile the precompiled header file (e. g. stdafx.cpp).

The unresolved externals you have above come from

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

that redefines operator new and operator delete in order to find memory leaks. If you are using these definitions in ONE cpp file you have to use it in ALL other cpp files as well and should not include header files that have another overload of these operators.

If it still not worked you should post your linker settings (Settings - Link - Project Options).

Regards, Alex
 
0
 

Author Comment

by:mordekai
ID: 11695014
well, i include my own files of course... the full include list looks like this:

#include <windows.h>   // include important windows stuff
#include <windowsx.h>
#include <mmsystem.h>
//#include <iostream> // include important C/C++ stuff
//#include <conio.h>
//#include <stdlib.h>
//#include <malloc.h>
//#include <memory.h>
//#include <string.h>
//#include <stdarg.h>
//#include <stdio.h>
//#include <math.h>
//#include <io.h>
//#include <fcntl.h>
#include <time.h>
#include <fstream>
//#include <stdlib.h>
using namespace std;


//#include <dshow.h>
#include <ddraw.h>  // directX includes
#include <dsound.h>
#include <dmksctrl.h>
#include <dmusici.h>
#include <dmusicc.h>
#include <dmusicf.h>
#include <dinput.h>
#include "T3DLIB1.h" // game library includes
#include "T3DLIB2.h"
#include "T3DLIB3.h"
#include "Block.h"
#include "BlockCollection.h"
#include "globals.h"
#include "HighScores.h"
#include "Title.h"

the three game t3d files are a directX wrapper library i am using from a tutorial.  I'm worried that they might be causing problems.

in all of my header files, i use the following directives:

#ifndef  _NAME_OF_FILE
#define _NAME_OF_FILE

// file stuff here

#endif //_NAME_OF_FILE

this is what i've always been taught to do to prevent the files from being included more than once.  To be honest I don't really understand what the directives you have listed do.  Can you rephrase what you think i may have done wrong?  

thanks !
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11698337
>> Can you rephrase what you think i may have done wrong?  

That's only a guess as i hadn't enough information.

>> LNK2019: unresolved external symbol __malloc_dbg referenced in function "void * __cdecl operator new

That error message says that the 'operator new' function calls a global function calls a function named '_malloc_dbg' but the linker couldn't find any object file  or library where _malloc_dbg is defined, i. e. where an implementation of _malloc_dbg is provided.

>> What is 'operator new'?

Whenever you use a statement like

     int* p = new int[20];

in C++ operator new function is called that allocates memory and returns a pointer to the first byte of the allocated buffer. It is possible to overload that function in order to track all memory alliocated. Thus for example when the program is exiting a list of all allocations that hasn't be freed by operator delete could be shown.

The following preprocessor statements replace all occurences of 'new' in your cpp file by 'DEBUG_NEW' . That is also a macro that evaluates to 'new(THIS_FILE, __LINE__).

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

Thus a statement like    

     int* p = new int[20];

turns to

     int* p = new(__FILE__, __LINE__) int[20];

__FILE__ is the current cpp file and __LINE__ is the current line number. Therefore a 'special' new function was called that takes the source file name and the line number as additional input parameters. With that information the compiler could show the memory leaks, i. e. a list of all sources and line numbers where there was a call to 'new' but not a 'delete'.

The implementation of operator new ´(size_t nSize, int nType, LPCSTR lpszFileName, int nLine) is in afxmem.cpp and in this function there is a call to _malloc_dbg that does the allocation (Note, the compiler adds another '_' character to all global functions, e. g. __malloc_dbg). There are some default libraries where the module _malloc_dbg is included (libcd.lib, libcmtd.lib, msvcrtd.lib). If you use the wizard to create a new VC project file (.dsp) one of these libraries automatically are linked against your program. However, if you are including headers from different environments it is possible that the linker already found an object of 'operator new(size_t nSize, int nType, LPCSTR lpszFileName, int nLine) ' but from another implementation that doen't call _malloc_dbg. So, _malloc_dbg remains as an unresolved external.

>> What to do?

You could search all your files if you find that sequence where 'new' is mapped to 'DEBUG_NEW'. Then, remove all these lines or comment them with /*   */. Then, rebuild all, and if you are lucky your errors are gone.

The next thing you could do is to give the linker explicitly all input libraries that it requires. Goto Project - Settings - Link - Input and insert  

         nafxcwd.lib libcmtd.lib
   
if your prog is multi-threaded or

       nafxcwd.lib libcd.lib

if it is single-threaded before all other libraries  that are defined in 'Object/Libraries Modules'

Then, rebuild all.

Good luck.

Alex


0
 

Author Comment

by:mordekai
ID: 11926446
i'm sorry, i thought i had closed this question... i clicked the button, but i guess i didn't complete the task or something.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

746 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

13 Experts available now in Live!

Get 1:1 Help Now