I have a “multiple definition” error in Eclipse when trying to include a file called sniffer.h.
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;
And it has the statements of all the functions in 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");
}
However in the first function it says that pipeFullName has multiple definitions again.
I only included it once and the string is defined in sniffer.h.
You should not include
.cppfile!!!In doing so you end up violating the One definition Rule.
The usual way is to place declarations in header files and definitions in cpp files.
You can declare something as many times as you want but define it only once.
So you should be including header files only. if you include a cpp file, a copy of definition of symbols in the cpp file get added to every translation unit where you include the header file thus violating the ODR.