Sign in to follow this  
jdub

Multiply defined symbols?

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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 
Edited by Cosmic314

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this