Multiply defined symbols?

Started by
1 comment, last by GameDev.net 10 years, 8 months ago

I am a little baffled at this error. I have a header file "common_include.h" which contains some miscellaneous Global functions and inclusions of commonly used header files. Here's the file:


#ifndef COMMON_INCLUDE_H
#define COMMON_INCLUDE_H

#define WIN32_LEAN_AND_MEAN

#include <atlbase.h>
#include <memory>
#include <string>

template <class T> void SafeRelease(T **ppT)
{
    if (*ppT)
    {
        (*ppT)->Release();
        *ppT = 0;
    }
};

const std::wstring appPath = L"C:\\Users\\Jared\\Documents\\Visual Studio 2012\\Projects\\nbody_simulation\\";

std::wstring GetFilePath(const std::wstring &fileName)
{
	std::wstring tmp = std::wstring(appPath.data());
	return tmp.append(fileName.data());
}

#endif 

I use GetFilePath() 3 times in my code in other files and the linker is complaining that I have multiply defined symbols? what am I doing wrong?

J.W.
Advertisement

Your function definition and declaration for GetFilePath are both in the header. Anything that includes it will contain GetFilePath at global scope in each execution unit. When the linker combines to make the executable it will find these identical implementations and complain about multiple definitions.

You need to place GetFilePath implementation in something like Common_Functions.cpp and then only include the prototype in your header.

-- The code editor was giving me grief. Below are two separate files. --


//-----------------------------------
// Make a new file with this in it.
//-----------------------------------

#include "common_include.h"

std::wstring GetFilePath(const std::wstring &fileName)
{
	std::wstring tmp = std::wstring(appPath.data());
	return tmp.append(fileName.data());
}


#ifndef COMMON_INCLUDE_H
#define COMMON_INCLUDE_H

#define WIN32_LEAN_AND_MEAN

#include <atlbase.h>
#include <memory>
#include <string>

template <class T> void SafeRelease(T **ppT)
{
    if (*ppT)
    {
        (*ppT)->Release();
        *ppT = 0;
    }
};

const std::wstring appPath = L"C:\\Users\\Jared\\Documents\\Visual Studio 2012\\Projects\\nbody_simulation\\";

std::wstring GetFilePath(const std::wstring &fileName);

#endif 

I am a little baffled at this error. I have a header file "common_include.h" which contains some miscellaneous Global functions and inclusions of commonly used header files. Here's the file:


std::wstring GetFilePath(const std::wstring &fileName)
{
	std::wstring tmp = std::wstring(appPath.data());
	return tmp.append(fileName.data());
}

#endif 

I use GetFilePath() 3 times in my code in other files and the linker is complaining that I have multiply defined symbols? what am I doing wrong?

That's to be expected. If you add "inline" in front of GetFilePath it will be fixed. Though, as Cosmic314 says, you can move the definition into a cpp file which is often the cleaner solution.

This topic is closed to new replies.

Advertisement