Jump to content
  • Advertisement
Sign in to follow this  
exorcist_bob

Unity linker errors

This topic is 4462 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

Hello, community! I don't understand why the following won't work. I have a global namespace SkepWorks. Everything that is about my engine is in this namespace. In one of my modules, I have a namespace SkepWorks::Asm, which is where all my assembly routines are. But for some reason, I keep getting duplicate functions errors from a module that is only including this module. In other words:
//SWMAngle.cpp
#include "SWMMath.h"
#include "SWMAngle.h"

//SWMMath.h
#include "SWMAngle.h"

namespace SkepWorks
{
namespace Asm
{
//functions
}
}

I get twelve errors like this one: 1>SWMMath.obj : error LNK2005: "float __cdecl SkepWorks::Asm::asm_arccos(float)" (?asm_arccos@Asm@SkepWorks@@YAMM@Z) already defined in SWMAngle.obj I believe that there is a circular dependency going on here, but what is the easiest way to fix it. Thanks for any assistance, exorcist_bob

Share this post


Link to post
Share on other sites
Advertisement
You probably have something like this:


// header.h
int foo;

// file1.cpp
#include "header.h"

// file2.cpp
#include "header.h"



Which results in the global variable foo being defined in two different translation units, which causes the linker to spit out an error that it is already defined in another object file when it encounters the second definition. The solution is to define foo in a source file, not a header file, and include an extern declaration like so:


// header.h
extern int foo;

// file1.cpp
#include "header.h"

int foo; // Only do this in one .cpp file

// file2.cpp
#include "header.h"


The extern statement says to the compiler "this variable exists, but I don't know where it is yet." References to it are resolved at link time, and the linker will only find in definition, in file1's object file.

Share this post


Link to post
Share on other sites
Functions defined in header files suffer the same problem, which is (I'm guessing) an issue more specific to the OP's problem.

Given that the functions are defined in the headers and since they are written in assembly you are presumably attemping to be "fast," you can mark the function definitions as "inline." The general case solution is to move the definitions into a source file, but making them inline might be a better option in this specific case.

It looks like this in case you are not familiar with it:

inline float asm_arccos(float v)
{
/* whatever */
}

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!