Linker error: Multiple definition of a function

0

I got stuck with this linker error:

    movecursor.cpp:(.text+0x0): multiple definition of 'random()'
    beep.cpp:(.text+0x0): first defined here
    [Error] ld returned 1 exit status

What's going on?

main.cpp:

    #include "beep.h"
    #include "movecursor.h"

beep.h:

    #include <Windows.h>
    #include <mmsystem.h>

    DWORD WINAPI BeepSec(LPVOID parameters);

beep.cpp:

    #include "beep.h"
    #include "random.h"

    DWORD WINAPI BeepSec(LPVOID parameters)
    {
        code... :)
    }

movecursor.h:

    #include <Windows.h>
    #include "beep.h"

    DWORD WINAPI MoveCursor(LPVOID parameters);

movecursor.cpp:

    #include "movecursor.h"
    #include "random.h"

    DWORD WINAPI MoveCursor(LPVOID parameters)
    {
        also some code...
    }

random.h:

    #include <Windows.h>

    int random() {
            "Alright, let me show you this."
        HCRYPTPROV prov;
        if (prov == NULL)
            if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_SILENT | CRYPT_VERIFYCONTEXT))
        ExitProcess(1);

        int out;
        CryptGenRandom(prov, sizeof(out), (BYTE *)(&out));
        return out & 0x7fffffff;
    }

BUT NOW... HOW TO SOLVE THIS?

I tried to put the definition in random.cpp, and the declaration in random.h, to declare random in both of movecursor.cpp and beep.cpp, but it still doesn't work.

HOW TO SOLVE THIS?

c++
winapi
linker
asked on Stack Overflow Nov 3, 2019 by marchmello

1 Answer

1

Put the definition of random() in .cpp source file.

Use include guards in your .h header files:

#ifndef RANDOM_H
#define RANDOM_H
extern int random();
...
...
#endif
answered on Stack Overflow Nov 3, 2019 by suspectus

User contributions licensed under CC BY-SA 3.0