Question

I am working on a project in which I am using the external library IrrLicht. The problem I am having is that the external header files which I include also have definitions in them. The linker doesnt like this very much and it complains about redefinition. Should I go for another library or is this a way to work this around? Oh btw I am working with vs 2010.

Example math3.h

#ifndef MATH_H
#define MATH_H

#include "quat.h"

class Point{
public:
    float x;
    float y;
    float z;
    Point (float _x, float _y, float _z){
        x=_x;
        y=_y;
        z=_z;
    }
    Point(){
        x=0;
        y=0;
        z=0;
    }
    irr::core::vector3df operator-(Point p){
        return irr::core::vector3df(this->x-p.x,this->y-p.y,this->z-p.z);
    }
    float distance(Point p){
        return sqrt((this->x-p.x)*(this->x-p.x)+(this->y-p.y)*(this->y-p.y)+(this->z-p.z)*(this->z-p.z));
    }
    Point operator+(irr::core::vector3df v){
        return Point(this->x+v.X,this->y+v.Y,this->z+v.Z);
    }
};

collidables.h

#ifndef COLLIDABLES_H
#define COLLIDABLES_H


#include "quat.h"
#include "math3.h"


class Sphere{
public:
    Point cm;
    float R;
    irr::core::vector3df speed;
};

quat.h

#ifndef QUAT_H
#define QUAT_H

#include "irrlicht.h"

#endif

InitializeItems.cpp

#include <vector>
#include <time.h>

#include "InitializeItems.h"
#include "math3.h"
#include "quat.h"
#include "collidables.h"



#define SQRT2 1.4142135;
#define DIVSQRT2 0.70710678118;



std::vector<Sphere> InitializeAtoms(int N, float R)
{

    std::vector<Sphere> spheres;
    spheres.reserve(N);
    srand(time(NULL));
    Sphere sph;
    for (int i=0;i<N;i++){

        sph.cm=Point(rand()%2*N*R,rand()%2*N*R,rand()%2*N*R);
        sph.R=R;
        sph.speed=irr::core::vector3df(rand()%10,rand()%10,rand()%10);
        spheres.push_back(sph);

    }
    return spheres;
}   
std::vector<Molecule3> InitializeMol (int N, float R){

    std::vector<Molecule3> mols;
    mols.reserve(N);
    srand(time(NULL));
    Molecule3 m;
    for (int i=0;i<N;i++){

        m.cm=Point(rand()%2*N*R,rand()%2*N*R,rand()%2*N*R);
        m.R=R;
        m.sph1=m.sph2=m.sph3=(m.cm-Point(0,0,0));
        m.orientation=irr::core::quaternion(rand(),rand(),rand(),rand());
        m.orientation.normalize();
        m.w=AxisAngle(irr::core::vector3df(rand(),rand(),rand()),rand()%360);
        m.w.v.normalize();
        irr::core::vector3df temp1,temp2,temp3;
        temp1=irr::core::vector3df(-R,-R,0);
        temp2=irr::core::vector3df(R,-R,0);
        temp3=irr::core::vector3df(0,R,0);
        temp1=fromRotation(m.orientation,temp1);
        temp2=fromRotation(m.orientation,temp2);
        temp3=fromRotation(m.orientation,temp3);
        m.sph1+=temp1;
        m.sph2+=temp2;
        m.sph3+=temp3;
        mols.push_back(m);

    }   
    return mols;
}

InitializeItems.h

#ifndef INITIALIZEITEMS_H
#define INITIALIZEITEMS_H

#include <vector>
#include "collidables.h"


std::vector<Sphere> InitializeAtoms(int N, float R);
std::vector<Molecule3> InitializeMol(int N, float R);

#endif

Linker Errors:

1>main.obj : error LNK2005: "class irr::core::quaternion __cdecl fromVector(class irr::core::vector3d<float>)" (?fromVector@@YA?AVquaternion@core@irr@@V?$vector3d@M@23@@Z) already defined in InitiliazeItems.obj
1>main.obj : error LNK2005: "class irr::core::vector3d<float> __cdecl purequatToVector(class irr::core::quaternion)" (?purequatToVector@@YA?AV?$vector3d@M@core@irr@@Vquaternion@23@@Z) already defined in InitiliazeItems.obj
1>main.obj : error LNK2005: "class irr::core::vector3d<float> __cdecl fromRotation(class irr::core::quaternion,class irr::core::vector3d<float>)" (?fromRotation@@YA?AV?$vector3d@M@core@irr@@Vquaternion@23@V123@@Z) already defined in InitiliazeItems.obj
1>physics.obj : error LNK2005: "class irr::core::quaternion __cdecl fromVector(class irr::core::vector3d<float>)" (?fromVector@@YA?AVquaternion@core@irr@@V?$vector3d@M@23@@Z) already defined in InitiliazeItems.obj
1>physics.obj : error LNK2005: "class irr::core::vector3d<float> __cdecl purequatToVector(class irr::core::quaternion)" (?purequatToVector@@YA?AV?$vector3d@M@core@irr@@Vquaternion@23@@Z) already defined in InitiliazeItems.obj
1>physics.obj : error LNK2005: "class irr::core::vector3d<float> __cdecl fromRotation(class irr::core::quaternion,class irr::core::vector3d<float>)" (?fromRotation@@YA?AV?$vector3d@M@core@irr@@Vquaternion@23@V123@@Z) already defined in InitiliazeItems.obj
1>visuals.obj : error LNK2005: "class irr::core::quaternion __cdecl fromVector(class irr::core::vector3d<float>)" (?fromVector@@YA?AVquaternion@core@irr@@V?$vector3d@M@23@@Z) already defined in InitiliazeItems.obj
1>visuals.obj : error LNK2005: "class irr::core::vector3d<float> __cdecl purequatToVector(class irr::core::quaternion)" (?purequatToVector@@YA?AV?$vector3d@M@core@irr@@Vquaternion@23@@Z) already defined in InitiliazeItems.obj
1>visuals.obj : error LNK2005: "class irr::core::vector3d<float> __cdecl fromRotation(class irr::core::quaternion,class irr::core::vector3d<float>)" (?fromRotation@@YA?AV?$vector3d@M@core@irr@@Vquaternion@23@V123@@Z) already defined in InitiliazeItems.obj

edit: added linker errors

edit2: link to github for whole project https://github.com/DxsGeorge/Collider

Was it helpful?

Solution

It seems that "already defined" problems comes from the other three classes (main, physics, visuals), so show more info about them, anyway I suggest you to instead of using

#ifndef
#define

#endif

Use

#pragma once

at the beginning of the header file, which will do the same thing (on visual studio only) and will be less error prone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top