Jump to content
  • Advertisement
Sign in to follow this  
jdub

Multiply defined symbols?

This topic is 1826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Share this post


Link to post
Share on other sites
Guest Hiwas

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!