I am having a linker error in a file of some 3000 lines that I cannot seem to reproduce. I was wondering if anybody had any ideas where the problem could be. Basically I have a function in a header file and whenever I go to link my project it says:

file:               line:   Message:
main.o                      In function 'myFunction@16':
main.h              123     multiple definition of 'myFunction@16'
main.h              123     first defined here

To me this seems iffy at best! What is going on?!

Without code to look at, my best guess is that main.h doesn't have an include guard and is somehow getting included more than once.

What gusano79 said. A header guard is something like this:

#ifndef MAIN_H
#define MAIN_H
// Contents of file below.
.
.
#endif /* MAIN_H */

This will keep the header contents from being processed more than once, even if included in more that one other header file that you are using.

I have header guards in the files. So that is not the problem :(

I figured it out, but now I cannot find a solution. The problem was that they were global in the header file for a DLL, the main.cpp for compiling the DLL uses extern TYPE VARIABLE_NAME; throughout, whereas when any other main.cpp uses the header file it uses TYPE VARIABLE_NAME. The thing is that I absolutely need globals to be used. Is there any way to use globals in a DLL? EG:

myDLL.h:

#ifndef MYDLL_H
#define MYDLL_H
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport) __stdcall
#else
#define DLL_EXPORT __declspec(dllimport) __stdcall
#endif
int globalVar;
DLL_EXPORT void init();
DLL_EXPORT void myFunction();
#endif

myDLL.cpp:

#include "myDLL.h"
DLL_EXPORT void init()
{
    globalVar=0;
}
DLL_EXPORT void myFunction()
{
    globalVar++;
}

main.cpp:

#include "myDLL.h"
int main()
{
    init();
    myFunction();
    return globalVar;
}

Shouldn't this...

#define DLL_EXPORT __declspec(dllexport) __stdcall
#else
#define DLL_EXPORT __declspec(dllimport) __stdcall
#endif

...really be this...

#define DLL_EXPORT __declspec(dllexport) __stdcall
#else
#define DLL_IMPORT __declspec(dllimport) __stdcall
#endif

...?

No it shouldnt, otherwise i would have to compile with DLL_EXPORT and then change it all to DLL_IMPORT when I want to use it. DLL_EXPORT and DLL_IMPORT are merely macros. Anyways, I am using a pseudo-solution to my problem:

myDLL.h:
#ifndef MYDLL_H#define MYDLL_H#ifdef BUILD_DLL#define DLL_EXPORT __declspec(dllexport) __stdcall#else#define DLL_EXPORT __declspec(dllimport) __stdcall#endifint globalVar;DLL_EXPORT void init();DLL_EXPORT void myFunction();#endif

Wow... that was not supposed to happen, I just hit ENTER and it saved. Oh well, this is what I meant to post:

myDLL.h:

#ifndef MYDLL_H
#define MYDLL_H

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport) __stdcall
#else
    #define DLL_EXPORT __declspec(dllimport) __stdcall
#endif
struct GLOBAL_STRUCTURE
{
    int globalInt;
};
#ifndef THIS_IS_MAIN_CPP
    GLOBAL_STRUCTURE globals;
#endif
DLL_EXPORT void initG(GLOBAL_STRUCTURE *gs);
void init(){initG(&globals);}
DLL_EXPORT void myFunctionG(GLOBAL_STRUCTURE *gs);
void myFunction(){myFunctionG(&globals);}
DLL_EXPORT int *getIntG(GLOBAL_STRUCTURE *gs);
void getInt(){return getIntG(&globals);}
#endif

myDLL.cpp

#define THIS_IS_MAIN_CPP
#include "myDLL.h"
DLL_EXPORT void initG(GLOBAL_STRUCTURE *gs)
{
    gs->globalInt=0;
}
DLL_EXPORT void myFunctionG(GLOBAL_STRUCTURE *gs)
{
    gs->globalInt++;
}
DLL_EXPORT void getIntG(GLOBAL_STRUCTURE *gs)
{
    return &(gs->globalInt);
}

My question is whether or not this will work, and if it does work is it safe? Also how c compatible is it?

I was just looking around the web and it looked like this would be a valid solution... is it?

DLL_EXPORT GLOBAL_STRUCTURE globals; //in the above code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.