Ethan Shulman
Ethan Shulman

Reputation: 43

c++ multi defined symbol

So I have this dll that holds some functions for my program. Called gc.dll I have it as a reference in my main project. In my main project I have 2 headers and 2 source files,

dx9d3d.h
main.h


main.cpp
dx9d3d.h

Heres my includes on each file

dx9d3d.h:
#include "gc.h"


dx9d3d.cpp:
#include "dx9d3d.h"

main.h:
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include "basics.h"
#include "dx9d3d.h"

main.cpp:
#include "main.h"

I keep getting the linker error for multi defined symbols for the d3ddevice and directx symbols I have in my gc.h. I have no idea how I can include gc.h to dx9d3d.h without it being a multi defined symbol :/ please help im kindof a noob to these linker errors.

and heres the code inside gc.h

namespace xaD3D {
__declspec(dllexport) LPDIRECT3D9 d3d;
__declspec(dllexport) LPDIRECT3DDEVICE9 d3dDev;
    __declspec(dllexport) bool initD3D(HWND hWnd);
}

and heres the errors

2>main.obj : error LNK2005: "struct IDirect3DDevice9 * xaD3D::d3dDev" (?                     d3dDev@xaD3D@@3PAUIDirect3DDevice9@@A) already defined in dx9d3d.obj
2>main.obj : error LNK2005: "struct IDirect3D9 * xaD3D::d3d" (?     d3d@xaD3D@@3PAUIDirect3D9@@A) already defined in dx9d3d.obj  

Upvotes: 2

Views: 412

Answers (2)

Ioan Paul Pirau
Ioan Paul Pirau

Reputation: 2833

Usually multiple include errors are solved in c++ by this type of construct:

#ifndef GC_Header
#define GC_Header
//header code enclosed here
#endif //GC_Header

Try adding these instructions at the beginning and at the end of the gc.h header file. Your gc.h file should look like this:

#ifndef GC_Header
#define GC_Header

namespace xaD3D {
__declspec(dllexport) LPDIRECT3D9 d3d;
__declspec(dllexport) LPDIRECT3DDEVICE9 d3dDev;
    __declspec(dllexport) bool initD3D(HWND hWnd);
}
#endif //GC_Header

HTH,
JP

Upvotes: 1

MRAB
MRAB

Reputation: 20654

The problem is occurring because the project contains main.cpp and dx9d3d.cpp, both of which include dx9d3d.h, which includes gc.h, so gc.h gets included twice in total, and therefore xaD3D gets defined twice.

As @John Paul said, the usual solution is to wrap the definition in #ifndef/#endif:

#ifndef GC_H
#define GC_H
...
#endif

An alternative is to use #pragma once (if supported) in the header file.

Upvotes: 1

Related Questions