为什么在链接时出现多定义错误? [英] Why do I get a multiple definition error while linking?

查看:64
本文介绍了为什么在链接时出现多定义错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a class in two separate files:

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

and 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)中使用此类:

Now I want to use this class in another file (main.cpp):

#include "modul1.h"

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

当我使用以下Makefile进行编译时,出现"...的多个定义"错误:

When I compile it with the following Makefile, I get a "multiple definition of..." error:

g ++ main.o modul1.o -o main modul1.o:在函数中 easylogger :: Logger :: Format(std :: basic_string< char,std :: char_traits< char>,std :: allocator< char>>const&)':modul1.cpp :(.text + 0x0):的多重定义 easylogger :: Logger :: Format(std :: basic_string,std :: allocator> const&)'main.o:main.cpp :(.text + 0x0):首先在这里定义modul1.o:在函数中 easylogger :: Logger :: WriteLog(easylogger :: LogLevel,easylogger :: Logger *,char const *,unsigned int,char const *,charconst *)':modul1.cpp :(.text + 0x2a):的多重定义 easylogger :: Logger :: WriteLog(easylogger :: LogLevel,easylogger :: Logger *,char const *,unsigned int,char const *,charconst *)'main.o:main.cpp :(.text + 0x2a):首先在这里定义collect2:ld返回了1个退出状态

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, std::allocator > 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对其进行了编译,并得到了相同的错误)

(At first I compiled it with code::blocks and got the same error)

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

How can I modify my Modul1 in order not to get this linking error? I don't think it is important, but I am using some Ubuntu 64bit with g++ 4.4.3

Makefile:

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

The way you are building this, easylogger.h (and consequently easylogger-inl.h) gets included twice, once for modul1.h and once for main.cpp

您的用法是错误的.但是您可以这样做以使其正常工作:

Your usage of it is wrong. But you can do this to make it work:

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

In modul1.h (remove #include "easylogger.h") and make it look like this

#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,包括真实的东西

and for modul1.cpp, include the real thing

#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!");
}

祝你好运!

这篇关于为什么在链接时出现多定义错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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