Skip to main content
gamedev.net
🔒 Locked

linker errors

Started by exorcist_bob Apr 28, 2006 at 10:03 PM 2 replies 1.3k views
Original Post
exorcist_bob
exorcist_bob
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
Aprosenf
Aprosenf
You probably have something like this:

// header.hint 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.hextern 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.
jpetrie
jpetrie
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 */}
exorcist_bob
exorcist_bob
Thanks. I forgot that I have to inline all modules that are in a .h file. And yes, I do know what inlining is[smile].

Thanks,
exorcist_bob

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.