c++ - 为什么在链接时出现多重定义错误?

标签 c++ ubuntu linker gcc4.4

我用这两个文件herehere .

我在两个单独的文件中创建了一个类:

modul1.h

#ifndef MODUL1_H
#define MODUL1_H

#include <iostream>
#include <fstream>

#include "easylogger.h"

class Modul1
{
    public:
        Modul1(std::string name);
    protected:
    private:
        easylogger::Logger *log;
};

#endif // MODUL1_H

和modul1.cpp

#include "modul1.h"

Modul1::Modul1(std::string name):log(new easylogger::Logger(name))
{
    //ctor
    //std::ofstream *f = new std::ofstream(name.c_str(), std::ios_base::app);
    //log->Stream(*f);
    //log->Level(easylogger::LEVEL_DEBUG);
    //LOG_DEBUG(*log, "ctor ende!");
}

现在我想在另一个文件(main.cpp)中使用这个类:

#include "modul1.h"

int main()
{
    std::cout << "Hello world!" << std::endl;
    Modul1 mod1("test.log");
    return 0;
}

当我使用以下 Makefile 编译它时,出现“multiple definition of ...”错误:

g++ main.o modul1.o -o main
modul1.o: In function easylogger::Logger::Format(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
modul1.cpp:(.text+0x0): multiple definition of easylogger::Logger::Format(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
main.o:main.cpp:(.text+0x0): first defined here
modul1.o: In function easylogger::Logger::WriteLog(easylogger::LogLevel, easylogger::Logger*, char const*, unsigned int, char const*, char const*): modul1.cpp:(.text+0x2a): multiple definition of easylogger::Logger::WriteLog(easylogger::LogLevel, easylogger::Logger*, char const*, unsigned int, char const*, char const*)
main.o:main.cpp:(.text+0x2a): first defined here
collect2: ld returned 1 exit status

(一开始我是用code::blocks编译的,同样的错误)

如何修改我的 Modul1 以避免出现此链接错误?我认为这不重要,但我正在使用一些带有 g++ 4.4.3 的 Ubuntu 64 位

生成文件:

CC=g++
CFLAGS=-c -Wall

all: log_test

log_test: main.o easylogger.h modul1.o
    $(CC) main.o modul1.o -o main

main.o: main.cpp modul1.h
    $(CC) $(CFLAGS) main.cpp

modul1.o: modul1.cpp modul1.h
    $(CC) $(CFLAGS) modul1.cpp

最佳答案

您构建它的方式,easylogger.h(以及随后的 easylogger-inl.h)被包含两次,一次用于 modul1.h,一次用于 main.cpp

你的用法是错误的。但是您可以这样做以使其工作:

在 modul1.h 中(删除#include "easylogger.h")并使其看起来像这样

#ifndef MODUL1_H
#define MODUL1_H

#include <iostream>
#include <fstream>
//#include "easylogger.h"

namespace easylogger { class Logger; };

class Modul1
{
    public:
        Modul1(std::string name);
    protected:
    private:
        easylogger::Logger *log;
};

#endif // MODUL1_H

对于 modul1.cpp,包含真实的东西

#include "modul1.h"
#include "easylogger.h"

Modul1::Modul1(std::string name):log(new easylogger::Logger(name))
{
    //ctor
    //std::ofstream *f = new std::ofstream(name.c_str(), std::ios_base::app);
    //log->Stream(*f);
    //log->Level(easylogger::LEVEL_DEBUG);
    //LOG_DEBUG(*log, "ctor ende!");
}

祝你好运!

关于c++ - 为什么在链接时出现多重定义错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9959563/

相关文章:

C++静态变量多实例

c++ - 强制 Visual Studio 链接 lib 文件中的所有符号

C++ 套接字。 const char* 正确到达,string.c_str() doenst

java - 未安装 JDK 8 - 错误 404 : Not Found

c++ - Winsock简单客户端服务器,接收失败

linux - 无法进行 LibreOffice 在线构建

apache - OpenSSL 在 csr 文件中生成未知的电子邮件地址?

linux - 使用 as 和 ld 在 x86-64 Linux 上汇编和运行 i386 asm 程序

c++ - 当 C 和 C++ 中严格要求内存释放时?

c++ - 计算二进制模式的子集