为什么链接时会出现多重定义错误?

2024-04-17

我使用这两个文件here https://raw.github.com/elanthis/easylogger/master/easylogger.h and here https://raw.github.com/elanthis/easylogger/master/easylogger-impl.h.

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

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 编译它时,出现“多重定义...”错误:

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<char, std::char_traits<char>, std::allocator<char> > const&)
main.o:main.cpp:(.text+0x0):首先在这里定义
modul1.o:在函数中easylogger::Logger::WriteLog(easylogger::LogLevel, easylogger::Logger*, char const*, unsigned int, char const*, char const*): modul1.cpp:(.text+0x2a): 的多重定义easylogger::Logger::WriteLog(easylogger::LogLevel, easylogger::Logger*, char const*, unsigned int, char const*, char const*)
main.o:main.cpp:(.text+0x2a):首先在这里定义
Collect2: ld 返回 1 退出状态

(起初我用 code::blocks 编译它并得到相同的错误)

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

生成文件:

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

祝你好运!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么链接时会出现多重定义错误? 的相关文章

随机推荐

  • d3.event 在去抖动函数内为 null

    当尝试使用 mousemove 事件处理程序的去抖版本时 d3 event is null 我想使用d3 mouse此去抖动处理程序中的对象 但是d3 event返回 null 并抛出错误 我怎样才能访问d3 event在下面的代码中 a
  • 匹配两个不同文件中最接近的值并打印特定列

    大家好 我有两个文件 每个文件都有 N 列和 M 行 File1 1 2 4 6 8 20 4 8 10 12 15 5 7 9 11 File2 1 a1 b1 c5 d1 2 a1 b2 c4 d2 3 a2 b3 c3 d3 19 a
  • 如何在 C# 中获得正确的 HTML 编码?

    我正在尝试从网络词典中获取某个单词的发音 例如 在下面的代码中 我想得到的发音good from http collinsdictionary com http collinsdictionary com HTTP Agility Pack
  • 如何统计SVN分支中更改或添加的行数?

    我想将 SVN 分支中的行更改数量相加 这样我就可以从另一方知道我在项目过程中走了多远 并估计当我将其与主干合并时发生冲突的概率 我能想到的方法是获取统一的 diff 并进行一些 grep wc l hack 但问题是很难分离不同的文件类型
  • 进程退出的问题

    假设我有一个 ID 为 1234 的进程 该进程在我的应用程序运行之前运行 我有这个代码 Process app Process GetProcessById 1234 MessageBox Show app MainWindowTitle
  • WINAPI_FAMILY_PARTITION 有何作用?

    我正在阅读头文件的定义winapifamily h并注意以下定义WINAPI FAMILY PARTITION define WINAPI FAMILY PARTITION Partitions Partitions 该宏的一般用法 作为示
  • 我们可以在javascript中将对象分配给cookie吗?

    有谁知道是否可以在javascript中将一个对象分配给cookies 如果是的话 我们该怎么做呢 如果您可以将对象序列化为其规范的字符串表示形式 并且可以将其从所述字符串表示形式反序列化回其对象形式 那么您可以将其放入 cookie 中
  • 从空整数到逗号列表中的指针的转换

    我知道在我们的现代世界中 NULL 和 0 并不是指针操作的最佳实践 根据 cppreference 指针转换 空指针常量 参见 NULL 可以是 转换为任意指针类型 结果为空指针 该类型的值 这种转换 称为空指针转换 允许作为单个转换转换
  • Activator.CreateInstance 找不到构造函数(MissingMethodException)

    我有一个具有以下构造函数的类 public DelayCompositeDesigner DelayComposite CompositeObject InitializeComponent compositeObject Composit
  • 如果 URL 以 https:// 开头,则不会显示网站图标

    我在使用 favicon ico 时遇到一个问题 这是我的链接相关代码 已包含在标头部分中 问题是 如果 url 以 http 开头 我可以在所有浏览器中查看该图标 当地址以 https 开头时 图标不会在 IE 浏览器中显示 有什么我需要
  • OpenGL固定功能着色器实现[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 是否有任何包装器可以在 OpenGL ES 2 0 之上模拟 OpenGL ES 1 1 API 我进
  • iOS8 - 模拟器 - 如何模拟位置

    我的应用程序跟踪您的位置 对于 iOS8 我必须改变它启动位置服务的方式 我得到了这个工作 self locationManager requestAlwaysAuthorization 并将 NSLocationAlwaysUsageDe
  • 从当前日期减去月份 sql

    我正在尝试从今天减去日期 获取 1 个月前直到永远的报告 到目前为止我已经尝试过 DATE SUB NOW INTERVAL 1 MONTH 这是上下文 SELECT contracts currency ROUND SUM CASE WH
  • 如何将 Google Analytics 跟踪 ID 添加到 GitHub Pages

    可能是一个简单的问题 但我现在对添加充满疑问谷歌分析跟踪ID to GitHub 页面 我正在使用 GitHub 自动页面生成器来创建我的 GitHub 页面 但它要求提供 Google Analytics 跟踪 ID 我尝试注册 Goog
  • 我正在尝试导入 GoogleAPIClient 或 GoogleAPIClientForREST

    我正在努力追随谷歌的教程 https developers google com drive ios quickstart ver swift制作他们的 QuickStart 应用程序来学习如何使用 Swift 进行 API 调用 我完全按
  • asp:SqlDataSource 到数据集项

    我的aspx页面上有一个asp SqlDataSource ID SqlDataSource1 工具 我想要在后面的c Sharp代码中做的就是传输数据源返回的记录并将它们放入DataSet中 以便我可以添加分页我的页面 我该怎么做 到目前
  • 如何显示图像

    我尝试使用 IPython display 与以下代码 from IPython display import display Image display Image filename MyImage png 我还尝试使用 matplotl
  • 匹配具有相同发音的单词elasticsearch

    我想匹配拼写不同但发音相同的单词 如 雄 与 雄 平面 与 平 我们可以在elasticsearch中进行这样的匹配吗 您可以使用语音分析插件 https github com elastic elasticsearch analysis
  • 如何完全禁用 UITextView 的滚动?

    我正在使用 UITextView 为 iPad 开发一个简单的文本编辑应用程序 我在使用 UIScrollView 和 UITextView 时总是遇到一些问题 我想我对这两个人的期望太高了 当我将 myTextView text 设置为另
  • 为什么链接时会出现多重定义错误?

    我使用这两个文件here https raw github com elanthis easylogger master easylogger h and here https raw github com elanthis easylog