[faq] multiple definition

Multiple definition is a linker error.
It is a violation of the One Definition Rule, you've got several translation units that define the same function or variable.

This is common caused because you put the definition of a function in a header file. Then all the sources that include that header would have that function definition (it doesn't matter that all definitions are the same)

The header should only have function declarations, move the definitions to a source file and link it to your project
(see also [faq] undefined reference http://www.cplusplus.com/forum/general/113904/ )

Note: you may also have the error if you #include a .cpp file. Don't do that.

Global variables
So you want a variable to be visible anywhere in your code. So you declared it global in a header, and put an #include in the sources want to use it.
To declare a global variable use extern, by instance extern nih::ostream cout;
You do need to define the variable in one source file.

Template and inline
These are an exception. Their definition is needed at compile time, so a special case is generated and you would not have multiple definition errors.
Simply put the definition in a header file.
Last edited on
I think this would fit into the FAQ under the Compiling/Linking section? Or would it better go in Beginners?

Also, have you seen http://www.cplusplus.com/faq/compiling/files/#headers?

(The [faq] undefined reference definitely belongs under Compiling/Linking.)
Linking, I suppose. It is not a programming topic, but an issue with the setup.

Sorry, I don't understand what you are trying to say with that link. ¿is there a mistake in my info?
by the way, I thought that inline functions were part of C

Before I forgot, [faq] undefined reference is missing the WinMain case, caused by choosing a wrong type of project (¿?)
Sadly, the thread is archived now.
Yes, it will go in the linking subsection of the Compiling/Linking section of the FAQ.

I was not trying to show any mistake in your info. I'll have to update my FAQ to remind people that C11 allows inline functions (as C98 did not). [I should have been more explicit about that.]

As for the archived-ness of the threads, that's not a problem, since I will add them to the FAQ as new pages with pretty formatting and stuff. So the WinMain case is not an issue -- we can add or modify the FAQs you wrote however you like.
> C11 allows inline functions (as C98 did not)
they were introduced in C99 https://en.wikipedia.org/wiki/C99
C99 then. I learn stuff all the time. :->
Topic archived. No new replies allowed.