IrrLicht - Linker error, redefinition - c++

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

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.

Related

How do I avoid `already defined` linking error with pugixml if two static libs contain pugixml objs?

So I have 2 static libs defined like this:
StaticLib1
// StaticLib1.h
#pragma once
class StaticLib1
{
public:
void doSomething1();
};
cpp:
// StaticLib1.cpp
#include "pugixml.hpp"
#include "StaticLib1.h"
void StaticLib1::doSomething1()
{
pugi::xml_node node;
}
StaticLib2
// StaticLib2.h
#pragma once
class StaticLib2
{
public:
void doSomething2();
};
cpp:
// StaticLib1.cpp
#include "pugixml.hpp"
#include "StaticLib2.h"
void StaticLib2::doSomething2()
{
pugi::xml_node node;
}
Main
#include <iostream>
#include "StaticLib1.h"
#include "StaticLib2.h"
int main(int argv, char** argc)
{
StaticLib1 staticlib1;
StaticLib2 staticlib2;
staticlib1.doSemething1();
staticlib2.doSemething2();
getchar();
return 0;
}
Now, if I build this. I get a lot of linking errors. Here are the first few linking errors:
3>StaticLib2.lib(StaticLib2.obj) : error LNK2005: "public: __thiscall pugi::xml_attribute::xml_attribute(struct pugi::xml_attribute_struct *)" (??0xml_attribute#pugi##QAE#PAUxml_attribute_struct#1##Z) already defined in StaticLib1.lib(StaticLib1.obj)
3>StaticLib2.lib(StaticLib2.obj) : error LNK2005: "public: __thiscall pugi::xml_attribute::xml_attribute(void)" (??0xml_attribute#pugi##QAE#XZ) already defined in StaticLib1.lib(StaticLib1.obj)
3>StaticLib2.lib(StaticLib2.obj) : error LNK2005: "private: __thiscall pugi::xml_attribute_iterator::xml_attribute_iterator(struct pugi::xml_attribute_struct *,struct pugi::xml_node_struct *)" (??0xml_attribute_iterator#pugi##AAE#PAUxml_attribute_struct#1#PAUxml_node_struct#1##Z) already defined in StaticLib1.lib(StaticLib1.obj)
...
...
Now, I understand that this linking error is because there is a pugixml.obj inside StaticLib1.lib, and there is pugixml.obj inside StaticLib2.lib. But I don't understand why this would cause linking error with pugixml signatures. Why would they be defined twice? If I call staticlib1.doSomething1() shouldn't main not care if there are multiple definitions of pugi? Shouldn't staticlib1.doSomething1() handle all of that?
on the pugiconfig.hpp I have these specific settings:
#ifndef HEADER_PUGICONFIG_HPP
#define HEADER_PUGICONFIG_HPP
#define PUGIXML_WCHAR_MODE
#define PUGIXML_HEAD_ONLY
#include "pugixml.cpp"
#endif
So yes, from user0042advice, I realize it is better to compile a pugixml.lib on your own rather than having #include "pugixml.cpp" on the config. I'm working with legacy code so these surprises are there. Now, I've fixed my issue and made my company code slightly cleaner.

Unresolved external symbol referenced in function error

I've looked at other people's solutions to this problem and none of them, unfortunately, have solved my issue. I was having this error in my dev project, so I started from scratch, followed this tutorial to the letter, including the file names and locations, and I am still getting
1>unittest1.obj : error LNK2019: unresolved external symbol "public:
static int __cdecl HelloWorld::getTwo(void)"
(?getTwo#HelloWorld##SAHXZ) referenced in function "public: void
__thiscall UnitTest1::UnitTest1::TestMethodGetTwo(void)" (?TestMethodGetTwo#UnitTest1#1#QAEXXZ)
1>unittest1.obj : error
LNK2019: unresolved external symbol "public: int __thiscall
HelloWorld::getN(void)const " (?getN#HelloWorld##QBEHXZ) referenced in
function "public: void __thiscall
UnitTest1::UnitTest1::TestMethodGetN(void)"
(?TestMethodGetN#UnitTest1#1#QAEXXZ)
The code is in the tutorial I linked but I will copy it below. I don't understand what I could be doing wrong at this point - my test project depends on my build project, my definitions for these functions are in the class.
HelloWorld.h
#pragma once
class HelloWorld
{
int n = 10;
public:
static int getTwo();
int getN() const;
};
HelloWorld.cpp
#include "stdafx.h"
#include "HelloWorld.h"
int main()
{
return 0;
}
int HelloWorld::getTwo()
{
return 2;
}
int HelloWorld::getN() const
{
return n;
}
unittest1.cpp (in test project)
#include "stdafx.h"
#include "CppUnitTest.h"
#include "../HelloWorld/HelloWorld.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethodGetTwo)
{
Assert::AreEqual(2, HelloWorld::getTwo());
}
TEST_METHOD(TestMethodGetN)
{
HelloWorld hw = HelloWorld();
Assert::AreNotEqual(0, hw.getN());
}
};
}
For some reason the linker can't find my definitions, and I'm out of ideas about why that might be.

error LNK2019: unresolved external symbol vs2013 [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
I get this error, but I don't know how to fix it.
I'm using Visual Studio 2013.
code
-----DateUtils.h
#pragma once
#include <string>
class DateUtils
{
public:
DateUtils();
~DateUtils();
static time_t str2time_t(const std::string&, const std::string&);
};
-----ForexUtils32.h
#include <string>
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the FOREXUTILS32_EXPORTS
// symbol defined on the command line. This symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// FOREXUTILS32_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef FOREXUTILS32_EXPORTS
#define FOREXUTILS32_API __declspec(dllexport)
#else
#define FOREXUTILS32_API __declspec(dllimport)
#endif
// This class is exported from the ForexUtils32.dll
class FOREXUTILS32_API CForexUtils32 {
public:
CForexUtils32(void);
// TODO: add your methods here.
};
extern FOREXUTILS32_API int nForexUtils32;
FOREXUTILS32_API int fnForexUtils32(void);
/******************** Add Begin *************************/
FOREXUTILS32_API time_t str2time(const std::string&, const std::string&);
/******************** Add End *************************/
-------DateUtils.cpp
#include "stdafx.h"
#include "DateUtils.h"
#include <sstream>
#include <iomanip>
using namespace std;
DateUtils::DateUtils()
{
}
DateUtils::~DateUtils()
{
}
time_t str2time_t(const string& datetimeIn, const string& formatIn) {
struct tm tm_time;
// For C++11
istringstream iss(datetimeIn);
iss >> get_time(&tm_time, formatIn.c_str());
time_t time = mktime(&tm_time);
return time;
}
------ForexUtils32.cpp
// ForexUtils32.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "ForexUtils32.h"
#include "DateUtils.h"
// This is an example of an exported variable
FOREXUTILS32_API int nForexUtils32=0;
// This is an example of an exported function.
FOREXUTILS32_API int fnForexUtils32(void)
{
return 42;
}
// This is the constructor of a class that has been exported.
// see ForexUtils32.h for the class definition
CForexUtils32::CForexUtils32()
{
return;
}
/******************** Add Begin *************************/
FOREXUTILS32_API time_t str2time(const std::string& datetime, const std::string& format)
{
time_t t = DateUtils::str2time_t(datetime, format);
return t;
}
/******************** Add End *************************/
Error message:
Error 1 error LNK2019: unresolved external symbol "public: static
__int64 __cdecl DateUtils::str2time_t(class std::basic_string,class
std::allocator > const &,class std::basic_string,class std::allocator > const &)"
(?str2time_t#DateUtils##SA_JABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##0#Z) referenced in function "__int64 __cdecl str2time(class
std::basic_string,class
std::allocator > const &,class std::basic_string,class std::allocator > const &)"
(?str2time##YA_JABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##0#Z) D:\visual
studio
2013\Projects\32bit\ForexUtils32\ForexUtils32\ForexUtils32.obj ForexUtils32
How can i fix this error ?Help me Please(I'm not good at english very much. Thanks)
The first line of the function definition
time_t str2time_t(const string& datetimeIn, const string& formatIn) {
have to be
time_t DateUtils::str2time_t(const string& datetimeIn, const string& formatIn) {
(Add the class name to which the member function belongs)

Problems with headers and linker errors (new to C++)

I'm new to c++ and I'm having some trouble with making a header file. The exact error I'm getting is
obj.obj : error LNK2019: unresolved external symbol "float * __cdecl getVertices(class std::basic_string,class std::allocator >,int,float *)" (?getVertices##YAPAMV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##HPAM#Z) referenced in function "struct ObjModel __cdecl importObj(void)" (?importObj##YA?AUObjModel##XZ)
The bugs/solutions I'm seeing seem much more complicated that what I'm doing. Here's my header, which I suspect is wrong.
//obj.h
#ifndef OBJ_H_INCLUDED
#define OBJ_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
struct ObjVertex {
float x, y, z;
};
struct ObjTriangle {
int Vertex[3];
int Normal[3];
};
struct ObjModel {
int NumVertex, NumNormal, NumTexCoord, NumTriangle;
ObjVertex *VertexArray;
ObjVertex *NormalArray;
ObjTriangle *TriangleArray;
};
//function prototypes
float* getVertices(string buf, int i, float* ret);
ObjModel importObj();
char* subString(char* buf, int b, int e);
#endif
I've just started in C++ but I have experience in Java and C, so it's probably an issue with me not knowing some C++ specific thing.
There is no implementation for float* getVertices(string buf, int i, float* ret); hence you get a linker error.
You must attach module where getVertices is declared to the project.

unresolved external errors

I have the following .h and .cpp files
If i have to I will include the full codes of the function definitions
When i compile my program i get the errors shown at the end
hash.h
#define BUCKETS 64
#define B_ENTRIES 50000
int curr_tanker;
typedef unsigned long int ulong;
typedef struct bucket
{
int bucket_id;
ulong bucket_entries;
}bucket;
typedef struct tanker_record
{
ulong tanker_id;
ulong tanker_size;
ulong num_of_entries;
ulong bucket_entry_count;
}tanker_record;
typedef struct fpinfo
{
unsigned long chunk_offset;
unsigned long chunk_length;
unsigned char fing_print[33];
}fpinfo;
struct fpinfo* InitHTable(fpinfo *);
int CreateTanker(tanker_record tr[]);
int Hash_CreateEntry(struct fpinfo *,struct fpinfo he,tanker_record tr);
ht.cpp
#include <stdlib.h>
#include <string.h>
#include<stdio.h>
#include <iostream>
#include "ht.h"
struct fpinfo* InitHTable(struct fpinfo ht[][B_ENTRIES])
{
}
int CreateTanker(tanker_record tr[])
{
}
int
Hash_CreateEntry(struct fpinfo *t[][B_ENTRIES],struct fpinfo he,tanker_record tr[])
{
}
static void
WriteHTtoFile(struct fpinfo *t[][B_ENTRIES],int this_tanker)
{
}
main.cpp
#include<iostream>
#include"ht.cpp"
#include<conio.h>
#include<stdlib.h>
void main(int argc, char **argv)
{
static fpinfo hash_table[BUCKETS][B_ENTRIES];
static tanker_record tr[100];
InitHTable(&hash_table[0][0]);
CreateTanker(tr);
struct fpinfo fp;
...
ar = Hash_CreateEntry(&hash_table[0][0], fp,tr[0]);
i get the following errors when i try to compile it using vc2010
1>main.obj : error LNK2005: "struct fpinfo * __cdecl InitHTable(struct fpinfo (* const)[50000])" (?InitHTable##YAPAUfpinfo##QAY0MDFA#U1##Z) already defined in ht.obj
1>main.obj : error LNK2005: "int __cdecl CreateTanker(struct tanker_record * const)"
(?CreateTanker##YAHQAUtanker_record###Z) already defined in ht.obj
1>main.obj : error LNK2005: "int __cdecl Hash_CreateEntry(struct fpinfo * (* const)[50000],struct fpinfo,struct tanker_record * const)" (?Hash_CreateEntry##YAHQAY0MDFA#PAUfpinfo##U1#QAUtanker_record###Z) already defined in ht.obj
1>main.obj : error LNK2005: "int curr_tanker" (?curr_tanker##3HA) already defined in ht.obj
1>main.obj : error LNK2019: unresolved external symbol "int __cdecl Hash_CreateEntry(struct fpinfo *,struct fpinfo,struct tanker_record)"
(?Hash_CreateEntry##YAHPAUfpinfo##U1#Utanker_record###Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "struct fpinfo * __cdecl InitHTable(struct fpinfo *)" (?InitHTable##YAPAUfpinfo##PAU1##Z) referenced in function _main
THANKS FOR YOUR HELP!!
You're including ht.cpp from main.cpp, which will include all the definitions of functions already defined in ht.cpp itself.
You want to include ht.h instead.
It won't help in this situation, but you should also protect the header file with include guards:
#ifndef HT_H
#define HT_H
// contents of ht.h
#endif
You also need the arguments of the function declarations to match those of the definitions:
struct fpinfo* InitHTable(struct fpinfo[][B_ENTRIES]);
// Missing: ^^^^^^^^^^^
int CreateTanker(tanker_record tr[]); // OK
int Hash_CreateEntry(struct fpinfo*[][B_ENTRIES],struct fpinfo,tanker_record[]);
// Missing ^^^^^^^^^^^^^ ^^
Add an "include guard" in your header, so that it its contents aren't "seen" twice after preprocessing. For Microsoft, #pragma once at the beginning of the .h file. In general, add:
#ifndef __YOUR_HEADER_H
#define __YOUR_HEADER_H
// all the stuff from the header here
#endif
Make sure to adopt a consistent "unique" naming scheme for each of your headers. __YOUR_HEADER_H would do, for example customio.h into __CUSTOM_IO_H.