错误LNK2001:当从现有项目添加了一些文件时,无法解析外部符号

error LNK2001: unresolved external symbol when some files have been added from existing project

本文关键字:文件 符号 外部 LNK2001 添加 项目 错误      更新时间:2023-10-16

我已经将现有项目中的一些文件复制到了新项目中。一切都很好,但我开始变得
error LNK2001: unresolved external symbol "class MessagingInterface message" (?message@@3VMessagingInterface@@A)

引发上述错误的文件在.cpp文件中声明了extern MessagingInterface message ;。我还有另外两个文件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

调用.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

嗨,伙计们,如果我在调用.cpp时只使用extern MessageInterface message,它编译得很好,但如果我使用MessageInterface中的一个函数或宏,它会引发链接器错误。例如,我在调用.cpp时使用了CODE_CHECKPOINT,它开始抛出链接器错误。。IM完全不知道

extern MessagingInterface message ;

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

已解决。。

extern MessagingInterface消息在调用.cpp时引发错误,因为未定义消息。由于我已经将现有项目中的一些文件复制并粘贴到新项目中,所以我无法定义消息。在其他一些文件中定义它之后,它现在可以正常工作了。