#ifndef include guard help!

Hello. I am new to using #ifndef, #ifdef, #endif and such. I
put the following at the top of my header file:
1
2
#ifndef INCLUDE_H
#define INCLUDE_H 

And an #endif at the end of the file. Yet I get a compile-time
error that says there's multiple definitions of all the functions
in the file. Can anyone tell me what I am doing wrong?
Thanks in advance.

EDIT: The cause for this is, I think, that there are two .cpp files in my project (forgot to say)

EDIT: Not sure what inline means, but it made my code work. Thanks!
Last edited on
My guess is that you're defining non-inline functions inside of your header file, which is #included into at least two different compilation units. But without seeing the code, it's just a guess.

I suggest making a separate, minimal example that still reproduces the issue. In doing so, you may find where the issue is yourself.
http://www.sscce.org/
Post removed by author
Last edited on
Here is a code I made.
Same compile-time error.

header.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef HEADER_H
#define HEADER_H

#include <iostream>
#include <windows.h> // For Sleep() function
using namespace std;

void Say(string x)
{
	for(int i; x[i] != '\0'; i++)
	{
		Sleep(100);
		cout << x[i];
	}
}

#endif 


main.cpp:
1
2
3
4
5
6
7
#include "header.h"

int main()
{
	void func1(void);
	func1();
}


other.cpp:
1
2
3
4
5
6
#include "header.h"

void func1(void)
{
	Say("Hello world!");
}


Can you spot the problem? I can't.
Last edited on
Yes, I happened to guess correctly; your issue is that you are defining a non-inline function (Say) inside of your header file, which is #included into two different compilation units (other.cpp and main.cpp).

You should either add the inline keyword to your function definition, or only declare the function in the header file, and implement (define) it in either main.cpp or other.cpp.

e.g.

header.h
1
2
3
4
5
6
7
8
9
#ifndef HEADER_H
#define HEADER_H

#include <string>

// declaration (used by multiple files)
void Say(string x);

#endif 


other.cpp
1
2
3
4
5
6
7
#include "header.h"

// definition (only compiled once)
void Say(string x)
{
    // ...
}


main.cpp
1
2
3
4
5
6
#include "header.h"

int main()
{
    Say("blah");
}


______________________________

When you "#include" something, think of it as directly copying the text from the header into the .cpp file. So what happens is you have two .cpp files, x.cpp and y.cpp, and they are both defining the Say(string) function independently, which causes the "multiple definition" error.

Each compilation unit is built separately in C++, and then they are linked together.
Last edited on
Ok, thank you. I guess the c++ book I read doesn't include all the keywords such as
inline. Is using multiple c++ files a bad idea? I mean I've seen it done but
only int main() does the calling, I tried two int main()s
(one in each .cpp file). When that didn't work I thought I'd declare the function in one
.cpp file and make it do something in another.
Last edited on
The inline keyword tells the linker to ignore multiple-definition errors by assuming that both definitions of Say that the linker sees are in fact the same.

The downside of defining your function in the header file is that that function needs to be reprocessed/recompiled each time it's #included in a different file. By separating out the declaration of the function and its definition (see my example above), you don't need the inline keyword. But each will work, it doesn't make much difference in this scenario.

> Is using multiple c++ files a bad idea?
Any non-trivial project or codebase for a program is going to use multiple files. It becomes unmanageable for humans to keep everything in one file. C++ uses separate compilation for each file, and then links them together when the final executable is being produced.

A program only has one main() function. That is the entry point into the application.
Last edited on
Oh, so, should I use inline keyword at all times in that header or just in cases
with multiple sources?
The point of a header file is to allow multiple sources to use it. Prefer declaring a function in the header, and defining (implementing) it in the source file. This allows the function to only be compiled once.
Ok, thank you. :D
The inline keyword tells the linker to ignore multiple-definition errors by assuming that both definitions of Say that the linker sees are in fact the same.


Note that this usage requires C++17.
No it doesn't. You're confusing that with the new feature with static class variables (edit: applies to variables outside of classes, too).
inline for functions has been around from the beginning.
Last edited on
OK. I've looked at the definition of inline https://en.cppreference.com/w/cpp/language/inline and it's much different from when I first came across it (many, many years ago!) as 'to serve as an indicator to the optimizer that inline substitution of a function is preferred over function call' Yes, I was thinking about inline variables (which is C++17). I should re-read all the current definitions - they change that much it's hard to keep absolutely current :) :)
Last edited on
Problem solved. Thank you all for helping!!
Topic archived. No new replies allowed.