【问题标题】:Multiple-definition error for classes in EclipseEclipse 中类的多重定义错误
【发布时间】:2012-12-26 09:29:05
【问题描述】:

尝试包含名为 sniffer.h 的文件时,Eclipse 中出现“多重定义”错误。

sniffer.h:

#ifndef SNIFFER_H_
#define SNIFFER_H_
#include "sys/types.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "netdb.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <queue>
#include <pthread.h>
#include <sys/timeb.h>
#include <map>
#include <algorithm>
#include <math.h>
#include <syslog.h>
#include <signal.h>
#include <asm-generic/errno-base.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
//Syslog libraries.
#include <sys/wait.h>
#include <ctime>
using namespace std;


string gconfigfilename = "config";          //Holds the config file name, dynamic     because of the add_interface option.
int pipehandler;                            //pipe handler for     the pipe that the gulp writes to
string pipeFullName;                        //absolute path for the pipe that the gulp writes to
FILE* pipe_fd;

并且有sniffer.cpp中所有函数的语句:

#include "../h/sniffer.h"

     void OpenPipeRead(/*string sinterface*/)
{
    int ret_val;
    pipeFullName = "";
    pipeFullName.append(mconfig[C_PIPEPATH]);
    //Its not empty only when there is argument for parallal telepath_sniff instances.
    pipeFullName.append(mconfig[C_PIPENAME]);
    //if(strcmp(sinterface.c_str(), "") != 0)
    //  lpipename.append("_" + sinterface);     
    printf("Try to open Pipe for reading\n");
    syslog(LOG_INFO, "Try to open Pipe for reading\n"); 
    /* Create the named - pipe */
    ret_val = mkfifo(pipeFullName.c_str(), 0666);
    if ((ret_val == -1) && (errno != EEXIST)) {
        perror("Error creating the pipe");
        syslog(LOG_ERR, "Error creating the pipe");         
        exit(1);
    }       
    if((pipehandler = open(pipeFullName.c_str(), O_RDWR)) < 1)      /* Open the pipe for reading and writing , in append mode */
    {
       perror("Failed to open pipe");
       syslog(LOG_ERR, "Failed to open pipe");
       exit(1);
    }
    printf("Pipe opened.\n");
    syslog(LOG_INFO, "Pipe opened.\n");
}

但是在第一个函数中,它说pipeFullName 再次有多个定义。

我只包含了一次,字符串在 sniffer.h 中定义。

【问题讨论】:

    标签: c++ linux eclipse


    【解决方案1】:

    你不应该包含.cpp文件!!!

    这样做你最终违反了One definition Rule

    通常的方法是将声明放在头文件中,将定义放在cpp文件中。
    您可以根据需要多次声明某些内容,但只能定义一次。

    所以你应该只包含头文件。如果包含 cpp 文件,则 cpp 文件中符号定义的副本会添加到包含头文件的每个翻译单元,从而违反 ODR。

    【讨论】:

    • 好的,我把所有的变量都移到了对应的h文件中,并包含在cpp文件中我还在h文件中声明了所有需要的函数……并且在cpp文件中包含了h文件然而现在它说一些变量是“多重定义的”:-\
    • @user1432779:你在头文件中声明了一些变量吗?我相信你做到了。你不应该这样做。如果你想在多个文件之间共享变量,请检查如何使用关键字extern
    • 谢谢它的工作......我已经有一段时间没有使用c++了..正在运行
    猜你喜欢
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 2010-12-17
    • 2011-05-02
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    相关资源
    最近更新 更多