错误LNK2001:当从现有项目添加了一些文件时,无法解析外部符号
error LNK2001: unresolved external symbol when some files have been added from existing project
我已经将现有项目中的一些文件复制到了新项目中。一切都很好,但我开始变得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时引发错误,因为未定义消息。由于我已经将现有项目中的一些文件复制并粘贴到新项目中,所以我无法定义消息。在其他一些文件中定义它之后,它现在可以正常工作了。
- 使用gcc从静态链接的文件中查找可选符号
- 检测 COFF 对象文件中C++内联符号
- 使用 fopen 打开 .pak 文件并使该文件应用于 const 无符号字符* (C++)
- 如何从二进制文件中的给定符号中获取调用程序图
- 如何将 UTF-8 文本从文件转换为某个可以迭代的容器,并检查每个符号是否为C++字母数字?
- CMake 生成的 MSVC 项目找不到符号,即使为其相关 dll 正确生成了 lib 文件也是如此
- 错误:当我从"WinDbg"打开可执行文件时,找不到符号文件。默认导出 ntdll 的符号.dll
- 可以在 cpp 文件中C++定义为特殊符号的变量β
- 尝试将字符串从文件读取到无符号字符向量中
- C++和带有国家符号的文件路径(也许用 UTF8 编码)
- OSX clang++:用于 cpp 文件中显式实例化模板的体系结构x86_64的未定义符号
- 生成文件:体系结构x86_64的未定义符号
- 标头保护文件中结构的重复符号
- 使用共享库编译可执行文件时仅链接所需的符号
- C++ - 将任何文件的字节读取到无符号字符数组中
- 体系结构x86_64的未定义符号:c++/ h 文件和 cpp 文件
- c/c 的文件/符号依赖图形可视化
- 计算 c# 中二进制文件符号的频率不起作用,但适用于等效的 c++ 代码
- Visual c++资源文件符号
- 创建一个只包含所有目标文件符号的文件时应该使用什么命令?