Sign in to follow this  

LNK2019 link errors. (std::ofstream) Help please.

This topic is 4816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys! I get some stupid "unresolved external symbol" link errors if I try to use a class from my static lib. I am using VC++ 2002 with the linker / compiler / .. from the VC++ 2003 ToolKit
lib_yae_2.lib(yae_logfile.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall std::basic_ofstream<char,struct std::char_traits<char> >::basic_ofstream<char,struct std::char_traits<char> >(char const *,int,int)" (__imp_??0?$basic_ofstream@DU?$char_traits@D@std@@@std@@QAE@PBDHH@Z) referenced in function "public: __thiscall yae::Logfile::Logfile(char const *)" (??0Logfile@yae@@QAE@PBD@Z)
lib_yae_2.lib(yae_logfile.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall std::basic_ofstream<char,struct std::char_traits<char> >::open(char const *,int,int)" (__imp_?open@?$basic_ofstream@DU?$char_traits@D@std@@@std@@QAEXPBDHH@Z) referenced in function "public: class yae::Logfile & __thiscall yae::Logfile::operator<<(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??6Logfile@yae@@QAEAAV01@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
Debug/editor.exe : fatal error LNK1120: 2 unresolved externals





here is what I try to do in the main.cpp form my .exe:
#include <yae_logfile.h>
yae::Logfile yae::global::LOG( "logfile.log" ); // create the global logfile instance#
//..





The other classes/funtions from my .lib work fine! Thank you! D: -- VC++ or DevC++ .. damn link errors :) -- Here the source code: header in .lib
// LOGFILE CLASS //
// ============= //

// Description: 
//		Logfile class that can log values and text into a logfile
// Use:
//              yae::Logfile LOG( "filename.log" );
//
//              int value = 555;
//              LOG << "Hello world !!  " << value << " Hehe " << ENDL;
//




#pragma once

#ifndef __YAE_LOGFILE_HPP__
#define __YAE_LOGFILE_HPP__
 
 #include <string>
 #include <fstream>
 #include <ios>

 namespace yae
 {

  class Logfile
  {
     private:
                std::ofstream m_clStream;
                const std::string m_strFileName;
     private:
		Logfile( const Logfile& );
		Logfile& operator= ( const Logfile& );
     public:
                Logfile( const char* pszFileName );
		~Logfile( void );
     public:           
                Logfile& operator<< ( const std::string& str );
                Logfile& operator<< ( const char* s );
                Logfile& operator<< ( char* s );
                Logfile& operator<< ( const char c );
                Logfile& operator<< ( const short n );
                Logfile& operator<< ( const unsigned short n);
                Logfile& operator<< ( const int n );
                Logfile& operator<< ( const unsigned int n );
                Logfile& operator<< ( const long n );
                Logfile& operator<< ( const unsigned long n );
                Logfile& operator<< ( const float n );
                Logfile& operator<< ( const double n );
                Logfile& operator<< ( const long double n );
                Logfile& operator<< ( void* n );
                Logfile& operator<< ( const void* n );

  }; // class Logfile 


  #define ENDL '\n'
   namespace global
   {
      extern yae::Logfile LOG;   // global logfile instance,
   }                             // that is used in the lib to                     report errors to the user
 } // namespace yae
  
#endif





.cpp in the lib:


#include "yae_logfile.h"

yae::Logfile::Logfile( const char* pszFileName )
 : m_clStream( pszFileName ), m_strFileName( pszFileName )
{
  m_clStream.close();
}
yae::Logfile::~Logfile( void )
{
}
yae::Logfile::Logfile( const Logfile& )
{
}
yae::Logfile& yae::Logfile::operator= ( const Logfile& )
{
   return *this;
}





yae::Logfile& yae::Logfile::operator<< ( const std::string& str )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << str;
  m_clStream.close();
  return *this;
}
yae::Logfile& yae::Logfile::operator<< ( const char* s )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << s;
  m_clStream.close();
  return *this;
}
yae::Logfile& yae::Logfile::operator<< ( char* s )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << s;
  m_clStream.close();
  return *this;
}

yae::Logfile& yae::Logfile::operator<< ( const char c )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << c;
  m_clStream.close();
  return *this;
}

yae::Logfile& yae::Logfile::operator<< ( const short n )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << n;
  m_clStream.close();
  return *this;
}

yae::Logfile& yae::Logfile::operator<< ( const unsigned short n )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << n;
  m_clStream.close();
  return *this;
}

yae::Logfile& yae::Logfile::operator<< ( const int n )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << n;
  m_clStream.close();
  return *this;
}

yae::Logfile& yae::Logfile::operator<< ( const unsigned int n )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << n;
  m_clStream.close();
  return *this;
}

yae::Logfile& yae::Logfile::operator<< ( const long n )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << n;
  m_clStream.close();
  return *this;
}

yae::Logfile& yae::Logfile::operator<< ( const unsigned long n )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << n;
  m_clStream.close();
  return *this;
}

yae::Logfile& yae::Logfile::operator<< ( const float n )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << n;
  m_clStream.close();
  return *this;
}

yae::Logfile& yae::Logfile::operator<< ( const double n )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << n;
  m_clStream.close();
  return *this;
}

yae::Logfile& yae::Logfile::operator<< ( const long double n )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << n;
  m_clStream.close();
  return *this;
}


yae::Logfile& yae::Logfile::operator<< ( void* n )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << n;
  m_clStream.close();
  return *this;
}


yae::Logfile& yae::Logfile::operator<< ( const void* n )
{
  m_clStream.open( m_strFileName.c_str(), std::ios::out | std::ios::app );
  m_clStream << n;
  m_clStream.close();
  return *this;
}





[Edited by - rakoon2 on October 10, 2004 3:49:56 AM]

Share this post


Link to post
Share on other sites
try and include
#include <iostream>

Also have a look at project properties
C++ -> CodeGeneration -> Runtime Libraries
See if it is the correct one. I'm useing /MDd (/MD for release) but my project is a dll.

Link errors suck, just had the same error (LNK2019) last week. Took me a day to solve :P

Share this post


Link to post
Share on other sites
What was the last change you made. Perhaps rollback until it works?

When posting something like this, always reduce it to the fewest nuber of lines that still causes the problem. With any luck you'll actually solve it half the time, by doing that.

Share this post


Link to post
Share on other sites
1. Both, the .lib and the .exe Runtime Library: /MD
( i'm using SDL )

2. The code above works fine if I use it with Dev-C++
( in a .lib(.a) or in the .exe )

3. #include <iostream> didn't help

4. There are no previous versions that work with VC++.
I'll try to reduce it to the fewest nuber of lines that
still cause the problem.


I am new to VS. Dev-C++ is alot easier! :p


Thanks! :)



::edit:: I found where the first linker error comes from!


Logfile( const std::string filename_ )
:filename(filename_), stream( filename_.c_str() )
{
}



this one:

, stream( filename_.c_str() ) // <- link error










error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall std::basic_ofstream<char,struct std::char_traits<char> >::basic_ofstream<char,struct std::char_traits<char> >(char const *,int,int)" (__imp_??0?$basic_ofstream@DU?$char_traits@D@std@@@std@@QAE@PBDHH@Z) referenced in function "public: __thiscall yae::Logfile::Logfile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Logfile@yae@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)















::edit2::

Hmm, just tried to use it in a new project.(.exe)
I still get the error(s)! It works with Dev-C++ ?! :(?


#pragma once

#ifndef __YAE_LOGFILE_HPP__
#define __YAE_LOGFILE_HPP__

#include <string>
#include <fstream>
#include <iostream>
#include <ios>

namespace yae
{
class Logf
{
private:
std::ofstream stream;
const std::string filename;
private:
Logf( const Logf& ){}
Logf& operator= ( const Logf& ){}
public:
Logf( const std::string filename_ )
:filename(filename_), stream( filename_.c_str() )
{
}
public:

};
}



#endif






Hmm? :/


Thanks.

[Edited by - rakoon2 on October 10, 2004 5:35:27 AM]

Share this post


Link to post
Share on other sites

This topic is 4816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this