错误LNK2001:从现有项目添加某些文件时未解析的外部符号

时间:2022-06-03 15:31:42

I have copied some of the files from my existing project into new project. Everything works fine but I have started getting
error LNK2001: unresolved external symbol "class MessagingInterface message" (?message@@3VMessagingInterface@@A)

我已将现有项目中的一些文件复制到新项目中。一切正常但我已经开始收到错误LNK2001:未解析的外部符号“类MessagingInterface消息”(?message @@ 3VMessagingInterface @@ A)

The file which throws above error has extern MessagingInterface message ; declared in .cpp file. I have two other files MessagingInterface.h and .cpp which declares and defines function for MessagingInterface class. The interesting thing to note is both these projects are build under vs2010 but the new one has some qt functions in it so obviously I am building with qt plugin and moc files etc I have added files in the build using Cmake. Any tiny help will be fruitful for me..

抛出上述错误的文件具有extern MessagingInterface消息;在.cpp文件中声明。我还有另外两个文件MessagingInterface.h和.cpp,它们声明并定义了MessagingInterface类的函数。有趣的是要注意的是这些项目都是在vs2010下构建的,但是新项目中有一些qt函数,所以显然我正在构建qt插件和moc文件等我使用Cmake在构建中添加了文件。任何微小的帮助对我来说都很有成效。

MessageInterface.h

//#include <afxmt.h>
#include <iostream>
#include <sstream>
#include <QMutex>
#include <QString>

#include "sms_list.h"

class sms_list;


/* Macro to output the current position in the code
 */
#define CODE_CHECKPOINT message( MessageType::CODE ) \
    << __FILE__ << ", line " << __LINE__ << std::endl

/** Centralised messaging interface for printing messages to the display and to
  * disk
  */
class MessagingInterface  
{
public:
    /** Which messages to write to which stream
      */
    struct Config
    {
        std::ostream*       os;
        MessageType::Enum   types[ MessageType::NUMBER_OF_MESSAGE_TYPES ];
    };

    /** Allows more than one stream to be written to at the same time
      */
    class Proxy
    {
        friend class MessagingInterface;

    public:
        ~Proxy();
        std::ostream& getStream( void ) const;

    private:
        Proxy( MessagingInterface* ptr );
        Proxy( const Proxy& other ) {}

        MessagingInterface* parent_;
    };

    friend class Proxy;

    MessagingInterface( const Config* cstart, const Config* cend ); 

    /** Give responsibility to write to the streams to a proxy that will do the
      * work for the messaging class
      */
    Proxy operator() ( const MessageType::Enum type );
    template<typename Type>
 void operator ()( const Type& value )
 {
    QMutex mutex;
    mutex.lock();
           //CSingleLock        lock( &criticalSection_, true );

    for( const Config* iter = start_; iter != end_; ++iter )
        *(iter->os) << value;
}

    /** Outputs the value straight to all the streams
      */
    template<typename Type>
    void operator ()( const Type& value )
    {
        QMutex mutex;
        mutex.lock();
  //CSingleLock lock( &criticalSection_, true );

        for( const Config* iter = start_; iter != end_; ++iter )
            *(iter->os) << value;
    }


    void timestamp( void );

    /** Timestamps the specified stream
      */
    void timestamp( std::ostream& os );

private:
    /** Outputs the message and the type
      */
    void output( const MessageType::Enum type, QString str ) const;

    sms_list*               sms_list_;

    std::stringstream           ss_;
    MessageType::Enum           type_;

    const Config*               start_;
    const Config*               end_;

    //mutable CCriticalSection  criticalSection_;
      mutable QMutex mutex_;
};

/** Outputs a CString as text instead of as a pointer
  */
inline std::ostream& operator <<( std::ostream& os, const QString& str )
{
 os << ( str ).toStdString();
    return os;
}

/** Outputs the value to the proxy stream then returns a reference to the
  * stream so other items can be sent to it
  */
template<typename Type>
std::ostream& operator <<( MessagingInterface::Proxy& proxy, const Type& value )
{
    //somecode
 }

// MessageInterface.cpp

    #include "MessagingInterface.h"
#include "SMS_list.h"
#include "lib_utils/Exception.h"
#include <boost/function.hpp>


#include <algorithm>
#include <fstream>
#include <QString>
#include <QTime>
#include <qvariant.h>
#include <QDateTime>

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

using namespace MessageType;
using namespace std;
using namespace platform;

namespace
{

 bla bla .. some functions


}

MessagingInterface::MessagingInterface( const Config* cstart, const Config* cend )
: message_list_( NULL ), start_( cstart ), end_( cend )
{
    ss_.setf( ios::boolalpha );
}

/** Give responsibility to write to the streams to a proxy that will do the
  * work for the messaging class
  */
MessagingInterface::Proxy MessagingInterface::operator() ( const MessageType::Enum type )
{
    type_ = type;
    return this;
}
// some other functions

Calling.cpp

    #some other files
#include "MessagingInterface.h"
#include "calling.h"

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

extern MessagingInterface message;
// some other functions

//calling.h
#pragma once
#include <string>
#include "MessagingInterface.h"


//extern MessagingInterface message; // commented out as declared in cpp file. But I have tried uncommenting here n commenting on cpp and vice versa

HI Guys , if i just use extern MessageInterface message in calling.cpp it compiles fine but if i use even one function or macro from MessageInterface it throws linker error. For example I have used CODE_CHECKPOINT in calling.cpp and it start throwing linker error .. IM completely clueless

HI Guys,如果我只是在calling.cpp中使用extern MessageInterface消息它编译得很好但是如果我甚至使用MessageInterface中的一个函数或宏它会抛出链接器错误。例如,我在calling.cpp中使用了CODE_CHECKPOINT,它开始抛出链接器错误.IM完全无能为力

2 个解决方案

#1


0  

This

extern MessagingInterface message ;

its a declaration. It should be included in a header file. Apart from this you should also define this variable in some module. It is being done by either removing keyword extern or by keeping this keyword but initializing this variable.

它的声明。它应该包含在头文件中。除此之外,您还应该在某个模块中定义此变量。它是通过删除关键字extern或保留此关键字但初始化此变量来完成的。

#2


0  

Solved ..

extern MessagingInterface message was throwing error in calling .cpp as Message was not defined . Since I have copy and paste few files from existinmg project into new project , I failed to define message . After defining it in some other files it works fine now.

extern MessagingInterface消息在调用.cpp时抛出错误,因为未定义消息。由于我已将existsinmg项目中的少量文件复制并粘贴到新项目中,因此无法定义消息。在其他一些文件中定义后,它现在工作正常。

#1


0  

This

extern MessagingInterface message ;

its a declaration. It should be included in a header file. Apart from this you should also define this variable in some module. It is being done by either removing keyword extern or by keeping this keyword but initializing this variable.

它的声明。它应该包含在头文件中。除此之外,您还应该在某个模块中定义此变量。它是通过删除关键字extern或保留此关键字但初始化此变量来完成的。

#2


0  

Solved ..

extern MessagingInterface message was throwing error in calling .cpp as Message was not defined . Since I have copy and paste few files from existinmg project into new project , I failed to define message . After defining it in some other files it works fine now.

extern MessagingInterface消息在调用.cpp时抛出错误,因为未定义消息。由于我已将existsinmg项目中的少量文件复制并粘贴到新项目中,因此无法定义消息。在其他一些文件中定义后,它现在工作正常。