在.OBJ已定义 - 没有双夹杂物 [英] Already defined in .obj - no double inclusions

查看:141
本文介绍了在.OBJ已定义 - 没有双夹杂物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然得到已经.OBJ错误定义。这是我的项目的结构:

I happened to get that already defined in .obj error. This is structure of my project:

#include "main.h";

main.h

#include <iostream>
#include <string>
#include <sstream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include "client.cpp"

client.cpp

#ifndef SOCKET_CLIENT_CLASS
#define SOCKET_CLIENT_CLASS
#ifndef BOOST_ASIO_HPP
#include <boost/asio.hpp>
#endif
/*CLASS DEFINITION HERE*/
#endif

这是什么编译器抱怨:

main.obj:错误LNK2005:市民:布尔__thiscall SocketClient ::阅读(INT,CHAR *)(?读@ SocketClient @@ QAE_NHPAD @ Z)已经定义的在client.obj

main.obj : error LNK2005: "public: bool __thiscall SocketClient::read(int,char *)" (?read@SocketClient@@QAE_NHPAD@Z) already defined in client.obj

请注意这是抱怨我的课,不刺激。有趣的是,当我删除的#include&LT;升压/ asio.hpp&GT; client.cpp 后,我得到的错误thouhg <强>它包含在main.h太

Note it is complaining about my class, not boost. One interesting thing is, that when I remove #include <boost/asio.hpp> from client.cpp, I get errors thouhg it is included in main.h too.

正如你所看到的,我不是双界​​定/包括我的课,其包含的只有一次中的 main.h 的。所以,这是怎么回事呢?结果
我已阅读这个答案,但它是没有帮助的,因为它预计双夹杂物。就拿这个事实到osideration重复投票前,因为这只是意味着我斩首毫不手软。

As you can see, I'm not double defining/including my class, its included exactly once in main.h. So what's going on here?
I have read this answer, but it was no help, since it expects double inclusions. Take this fact into osideration before voting for duplicate, because this simply means beheading me without mercy.

推荐答案

这是不是一个的编译的错误:错误是从的链接的到来。编译后,连接器将合并从每个翻译单元(的.cpp 文件)的汇编后的目标文件。

This is not a compiler error: the error is coming from the linker. After compilation, the linker will merge the object files resulting from the compilation of each of your translation units (.cpp files).

链接器发现,你已经在同一个符号在不同的翻译单位定义多次,并且抱怨它(这是一个违反了一个定义规则)。

The linker finds out that you have the same symbol defined multiple times in different translation units, and complains about it (it is a violation of the One Definition Rule).

原因肯定是那个的main.cpp 包括 client.cpp ,并且这两个文件单独处理由编译器生成的两个的独立的目标文件。因此,在 client.cpp 翻译单元中定义的所有符号也将在的main.cpp 转换单元中定义。这其中的原因之一,你通常不会的#include 的.cpp 文件。

The reason is most certainly that main.cpp includes client.cpp, and both these files are individually processed by the compiler to produce two separate object files. Therefore, all the symbols defined in the client.cpp translation unit will be defined also in the main.cpp translation unit. This is one of the reasons why you do not usually #include .cpp files.

把你的类的定义中,不单独 client.hpp 文件的不可以包含该类的成员函数也定义;接下来,就让 client.cpp 的main.cpp 包括文件(我的意思是的#include )。最后,留在 client.cpp 你的类的成员函数的定义。

Put the definition of your class in a separate client.hpp file which does not contain also the definitions of the member functions of that class; then, let client.cpp and main.cpp include that file (I mean #include). Finally, leave in client.cpp the definitions of your class's member functions.

client.h

#ifndef SOCKET_CLIENT_CLASS
#define SOCKET_CLIENT_CLASS
#ifndef BOOST_ASIO_HPP
#include <boost/asio.hpp>
#endif

class SocketClient // Or whatever the name is...
{

// ...

    bool read(int, char*); // Or whatever the name is...

//  ...
};

#endif

client.cpp

#include "Client.h"

// ...

bool SocketClient::read(int, char*)
{
    // Implementation  goes here...
}

// ... (add the definitions for all other member functions)

main.h

#include <iostream>
#include <string>
#include <sstream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include "client.h"
//              ^^ Notice this!

的main.cpp

#include "main.h"

这篇关于在.OBJ已定义 - 没有双夹杂物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆