Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple definition linker error after adding a function to a previously linking file

So my program is working fine. Compiling, linking, running, the works. Then, I decide to add a simple function to one of my files, like this:

#ifndef UTILITY_HPP
#define UTILITY_HPP

/* #includes here.  There's no circular include, I've checked. */

namespace yarl
{
    namespace utility
    {
        (several function declarations and definitions)

        bool isVowel(const char c)
        {
            if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
                return true;
            else return false;
        }
    }
}

#endif

That function definition is the only change I've made to my code. Everything else is exactly the same as it was. Nothing calls it yet. I compile, and it fails to link, with g++ giving one of these errors for every file that #includes this one:

./obj/Feature.o: In function `yarl::utility::isVowel(char)':
/home/max/Desktop/Development/Yarl Backup/yarl v0.27/src/Utility.hpp:130: multiple     definition of `yarl::utility::isVowel(char)'
./obj/Events.o:/home/max/Desktop/Development/Yarl Backup/yarl v0.27/src    /Utility.hpp:130: first defined here
./obj/GameData.o: In function `yarl::utility::isVowel(char)':

If I comment out isVowel, it works again. I've tried renaming it, still doesn't work. I've tried replacing it with just void randomFunctionName() {}, still doesn't work. I've tried making it non-inline and putting the function body in Utility.cpp, still doesn't work. I am extremely confused. Why would adding one simple function screw up the linker?

like image 882
Max Avatar asked Jun 28 '10 22:06

Max


People also ask

How do you solve linking errors?

You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.

What causes a linker error?

Linker errors occur when the linker is trying to put all the pieces of a program together to create an executable, and one or more pieces are missing. Typically, this can happen when an object file or libraries can't be found by the linker. Fixing linker errors can be tricky.

How do you avoid multiple definition errors in C++?

cpp file, but with multiple cpp files, the definition will still be there in multiple . cpp files, thus leading to the multiple definition error. The right way is to only extern byte lightmode; in the header file and then define the variable with byte lightmode; in only one cpp file.

What is undefined reference?

An “Undefined Reference” error occurs when we have a reference to object name (class, function, variable, etc.) in our program and the linker cannot find its definition when it tries to search for it in all the linked object files and libraries.


1 Answers

Either declare the function inline, or define it in a separate .cpp file. Otherwise every C++ file in which you include the header is trying to make its own, publicly-available definition of the function.

Edit: and fwiw, you don't need to explicitly return true or false if you're testing a conditional. Just return the conditional itself:

inline bool isVowel(const char c)
{
    return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
like image 51
Cogwheel Avatar answered Oct 07 '22 03:10

Cogwheel