我正在尝试为我正在处理的项目编写一个调试类,并且我不希望必须传递一个调试对象,所以我试图这样做。但是,我不知道为什么我的编译器或链接器似乎在跳过我为它编写的实现。调试类,C++,链接器错误
如果我添加#include“Debug.cpp”到main.cpp,这段代码工作得很好。除非我将实现放在.h文件或#include Debug.cpp中,否则编译器会以某种方式遗漏开放,关闭和打印函数的实现。
你知道为什么它没有在Debug.cpp中看到实现吗?
我不认为它很重要,但目前debug.h和debug.cpp在一个项目中,main.cpp在另一个项目中(都在同一个解决方案,但Visual Studio 2008)。 main.cpp被构建为可执行文件,并且debug.h和debug.cpp被编译为动态库(dll)。
Debug.h
代码:选择所有
#define BUFFER_SIZE 1028
#include <string>
#include <fstream>
#include <assert.h>
#include <stdarg.h> // va_arg
#ifndef _DEBUG_H_
#define _DEBUG_H_
namespace Debugger
{
// member variables
extern const unsigned int m_sizebuffer;
extern std::ofstream m_file;
extern bool m_fileopened;
extern char m_buffer[BUFFER_SIZE];
extern va_list m_vl;
/*
'extern' keyword there to remind myself that
all functions are implicitly extern, so
declaring variables with 'extern' is more intuitive.
Will remove when the 'extern' keyword becomes second nature to me.
*/
extern void open_file();
extern void close_file();
extern void print(const char* fmt, ...);
}
#endif
Debug.cpp
代码:选择所有
#ifndef _DEBUG_H_
#include "Debug.h"
#endif
namespace Debugger
{
const unsigned int m_sizebuffer = BUFFER_SIZE;
std::ofstream m_file;
bool m_fileopened;
char m_buffer[BUFFER_SIZE];
va_list m_vl;
void Debugger::open_file()
{
// file cannot already be opened.
assert(!m_fileopened);
m_file.clear(); // clear contents of debug file.
//if directory already exists nothing *SHOULD* happen. platform dependent.
system("mkdir Data");
m_file.open("./Data/ErrorLog.txt"); // hard-coding filename is intentional
if(m_file.is_open())
{
m_fileopened = true;
print("Debug: successfully loaded file './Data/ErrorLog.txt' \n");
}
}
void Debugger::close_file()
{
if(m_fileopened)
{
m_file.close();
m_fileopened = false;
}
}
/*
WARNING: Should only accept c-style strings only. If output is ever cryptic double check
that we are not passing c++ Strings instead of char*, do not know how to differentiate if
fmt is a c-style string (char*) or a C++ String.
*/
void Debugger::print(const char* fmt, ...)
{
if(!m_fileopened)
{
open_file();
print("Debug file opened. \n");
}
int retval = 0;
va_start(m_vl, fmt);
retval = vsnprintf_s(m_buffer, m_sizebuffer, m_sizebuffer, fmt, m_vl);
va_end(m_vl);
m_file << m_buffer;
m_file.flush();
assert(retval > 0);
}
}
的main.cpp
代码:选择全部
#include "stdlib.h"
#ifndef _DEBUG_H_
#include "Debug.h"
#endif
int main(int argc, char* argv[])
{
//Debugger::print("this should work~! yay");
return EXIT_SUCCESS;
}
如果我在主函数中,我得到以下错误取消注释打印线:
代码:选择所有
1>LINK : C:\Users\Benjamin\Desktop\vs projects (c++)\Game Debugger\debug class\Debug\Smashteroids.exe not found or not built by the last incremental link; performing full link
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl Debugger::print(char const *,...)" ([email protected]@@YAXPBDZZ) referenced in function _main
1>C:\Users\Benjamin\Desktop\vs projects (c++)\Game Debugger\debug class\Debug\Smashteroids.exe : fatal error LNK1120: 1 unresolved externals
编辑:一个链接到我原来的讨论:
http://elysianshadows.com/phpBB3/viewtopic.php?f=6&t=5328&start=999999
你是如何编译这个的?它看起来好像你没有编译Debug.cpp代码或正确链接它。 – easel 2010-08-31 02:14:03
对不起,我仍在学习如何使用链接器。你能更具体一点吗?我在一个解决方案中使用visual studio 2008编译了两个项目。一种解决方案,debug.cpp和debug.h作为一个dll文件,主要作为可执行文件。 – 2010-08-31 02:16:09
我很久没有与Visual Studio一起练习(“项目”?“解决方案”?),但它看起来好像它没有链接到动态库中。我建议你找到一个使用动态库的HelloWorld“解决方案”,并确保你可以使它工作。 – Beta 2010-08-31 02:18:25