1

I was trying to use 2 .cpp files with 1 .h file but whenever I include the .h file in both .cpp files I get the "Multiple Definitions" error. So I made this to show my problem and yes I am aware thing.cpp never does anything but that still triggers the problem Also I use Code::Blocks and the GNU GCC Compiler if that helps.

main.cpp

#include <iostream>
#include <C:\0w0\Multiple file test\Hello_world.h>
#include <windows.h>
using namespace std;
int main()
{
    A:
    Sleep(500);
    Hello();
    x++;
    if(x==5)
    {
        Sleep(500);
        Hello();
        return -1;
    }
    goto A;
}

thing.cpp

#include <iostream>
#include <C:\0w0\Multiple file test\Hello_world.h>
#include <windows.h>
using namespace std;
int thing()
{
    A:
    Sleep(500);
    Hello();
    x++;
    if(x==5)
    {
        Sleep(500);
        Hello();
        return -1;
    }
    goto A;
}

hello_world.h

#ifndef Hello_World_H
#define Hello_World_H
#endif // Hello_World
int x=1;
using namespace std;
int Hello()
{
    cout << x << endl;
    return 69;
}

I expect it to work normally but it doesn't and I get these errors

obj\Debug\thing.o||In function 'Z5Hellov':|
C:\0w0\Multiple file test\Hello_world.h|7|multiple definition of 'Hello()'|
obj\Debug\main.o:C:\0w0\Multiple file test\Hello_world.h|7|first defined here|
obj\Debug\thing.o||In function 'Z5Hellov':|
C:\0w0\Multiple file test\Hello_world.h|7|multiple definition of 'x'|
obj\Debug\main.o:C:\0w0\Multiple file test\Hello_world.h|7|first defined here|
||error: ld returned 1 exit status|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

edit: I don't know how this is in any way close to that other question you guys said it is. So how do I make this work that's all I need to know

Alib234
  • 11
  • 3
  • 2
    When you include a header file it is copied into the including file. The include guard will stop it from being included multiple times in a single compiled file, but cannot prevent inclusions in multiple compiled files (this would be bad. Think on what would happen if only one file in a project could include `` for example). When you define variables and functions in a header everything that includes the header gets a copy of those definitions. Instant multiple definitions. – user4581301 Jul 09 '19 at 03:22
  • 1
    `A:` ... `goto A` is a `while (true) { ... }` loop[ with a bunch of extra gotchas and ways things can go wrong. [Prefer the `while` loop.](https://stackoverflow.com/questions/46586/goto-still-considered-harmful) – user4581301 Jul 09 '19 at 04:03
  • Change your design pattern. I recommend this simple rule: function prototypes in the header files, function definitions in source files. Exception for this rule are inline functions. – harper Jul 09 '19 at 07:00

1 Answers1

0

hello_world.h : #endif It must be at the end of the file to avoid double definition.

#ifndef Hello_World_H
#define Hello_World_H

int x=1;
using namespace std;
int Hello()
{
    cout << x << endl;
    return 69;
}
#endif // Hello_World
  • 1
    The `#if`/`#endif` guards work only per compilation unit. Since two source files are used here it will not work. Therefore your answer is wrong. – harper Jul 09 '19 at 06:56
  • ye placing that at the end doesn't work. Getting the same error – Alib234 Jul 09 '19 at 12:04