Thread: Multiple definitions error for all function definitions in header

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Talking Multiple definitions error for all function definitions in header

    Ok, this is weird...
    My compiler is producing a "multiple definition" error for every single function definition inside a header file that I wrote. Each function definition is a member function of a class which is defined in the same file. I put the function definitions after the class definition.

    My header has include guards (i.e. #ifndef, #define, and #endif) wrapped around the whole code, and needless to say, I don't have any duplicated function definitions.

    Anyone have any idea why its doing this??

    Thanks in advance.

    EDIT: Another interesting thing to note is, when I click on one of the multiple definition errors, it brings up the 'new' file, with the cursor on the line which says:

    Code:
    inline void* operator new(std::size_t, void* __p) throw() { return __p; }
    What does this have to do with the "new" operator?
    Last edited by Programmer_P; 03-03-2011 at 11:24 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Separate your header file from that of defintion file and the main file..

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Mr.777 View Post
    Separate your header file from that of defintion file and the main file..
    Is that really necessary? As this is a generated file we're discussing, I want to keep the code in a single file, if possible.

    Isn't there someway to make it work with the function definitions in the header file, along with the class definition?
    A google search seems to suggest using the 'inline' keyword.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Yeah inline functions are those that are decided by the compiler, not by the programmer...
    And these functions have just one or two lines of code..

    So, now it depends what kinda functions you want to code....
    If you want your functions to limit their functionality for one or two lines, try using inline functions...
    Hope it will help.

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Mr.777 View Post
    Yeah inline functions are those that are decided by the compiler, not by the programmer...
    And these functions have just one or two lines of code..

    So, now it depends what kinda functions you want to code....
    If you want your functions to limit their functionality for one or two lines, try using inline functions...
    Hope it will help.
    No can do...
    My functions have many lines of code, and there's no way around that.

    EDIT: A quick question about 'inline' though...does it matter if you put the keyword before the function declaration or before the definition (or is both allowed)?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  6. #6
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    I think both are allowed, but putting the keyword 'inline' doesn't guarantee you that now it's an inline function...
    By the way, why you don't want to separate files??
    May be there are many other options to keep it in a single file..

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Mr.777 View Post
    I think both are allowed, but putting the keyword 'inline' doesn't guarantee you that now it's an inline function...
    Yeah, I know. I skimmed through a page on 'inline' which came up in a Google search, and learned that the compiler makes decisions about what functions to make inline, and what functions to not make inline, which I guess is implementation-specific.
    I went ahead, and added the inline keyword before all function declarations in my generated file, and that got rid of the "multiple definition" errors.
    Now its returning a "collect2: ld returned 1 exit status" instead.

    And I actually mis-phrased my above post. I don't actually have many lines in all functions of my generated file. I only have a bunch of lines for the constructor and two other functions. All the other ones only have one line of code inside the braces (not counting the empty line before and after).

    By the way, why you don't want to separate files??
    May be there are many other options to keep it in a single file..
    Well, for one, it would require serious modification (ok, maybe I'm exaggerating just a little...) to the code for the program that generates the file, and since I already made way too many changes after calling it 'done' the first ten-thousand times, I would like to think for once this program is now actually done (except for minor updates here and there, and if I want to add a feature). For two, I don't know, one generated file is easier to keep track of than two.
    Last edited by Programmer_P; 03-04-2011 at 12:21 AM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  8. #8
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Lol..... I think, you should try learning, handling many files with your single project.

    So, what's the problem now?

    If your functions got only one or two lines, make them inline and can you work without writing your own constructor because compiler can call default constructor in object request of that class...

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Mr.777 View Post
    Lol..... I think, you should try learning, handling many files with your single project.
    Oh, I will...just not in THIS program.
    So, what's the problem now?
    Quote Originally Posted by Programmer_P View Post
    Now its returning a "collect2: ld returned 1 exit status" instead.
    If your functions got only one or two lines, make them inline and can you work without writing your own constructor because compiler can call default constructor in object request of that class...
    No, my code wont work without writing my own constructor, because there are many important class variables which are assigned their values inside the constructor.
    Last edited by Programmer_P; 03-04-2011 at 12:01 AM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  10. #10
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Hmmm.... I have no idea of writing function definition inside header file... May be any solution exists but i got no such idea...
    Anyways, for good practice i will recommed you to make separate files...

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Programmer_P View Post
    I went ahead, and added the inline keyword before all function declarations in my generated file, and that got rid of the "multiple definition" errors.
    Now its returning a "collect2: ld returned 1 exit status" instead.
    That usually means you have an unresolved symbol (e.g. the linker is looking for a function, or maybe a static variable, that is not defined anywhere).

    Quote Originally Posted by Programmer_P View Post
    And I actually mis-phrased my above post. I don't actually have many lines in all functions of my generated file. I only have a bunch of lines for the constructor and two other functions. All the other ones only have one line of code inside the braces (not counting the empty line before and after).
    Yeah, OK. Your problem is not actually related to size of functions - the size of the function is only one factor that may cause the implementation not to inline a function.

    Your problem is probably related to a definition being seen in multiple object files by the linker. From what you are describing, you are #include'ing your header from multiple source files (eg .cpp files), which means that any definitions in that header files appear in multiple object files.

    Declaring your functions inline should address that problem. While it is true that the implementation (compiler, linker, etc as a suite) might decide not to inline a function that your code declares as inline ("inline" is a hint, not a directive) then the implementation is required to sort things out.

    Without seeing samples of actual code (a sample header, and a sample of two source files that include that header, and exhibit the problem) it is harder to give more specific advice.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Right 98% of the time, and don't care about the other 3%.
    No offense but shouldn't it be 2%??
    lol.... just a blunt humour.... hope you'll not mind that...

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Mr.777 View Post
    Yeah inline functions are those that are decided by the compiler, not by the programmer...
    And these functions have just one or two lines of code..

    So, now it depends what kinda functions you want to code....
    If you want your functions to limit their functionality for one or two lines, try using inline functions...
    Hope it will help.
    There is no limit to the size of lines of code in a inline function.
    Although having lots of lines of code in a function makes it less likely the compiler will make it inline.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by grumpy View Post
    That usually means you have an unresolved symbol (e.g. the linker is looking for a function, or maybe a static variable, that is not defined anywhere).
    Well, I am 100% sure that all functions I declared are defined somewhere. And I don't have any static variables in my class.
    Yeah, OK. Your problem is not actually related to size of functions - the size of the function is only one factor that may cause the implementation not to inline a function.

    Your problem is probably related to a definition being seen in multiple object files by the linker. From what you are describing, you are #include'ing your header from multiple source files (eg .cpp files), which means that any definitions in that header files appear in multiple object files.
    Actually, no I'm not. I only include the header in another header, which is then included in a single source file (and that only once). Also, I have header guards in the generated header file that we're discussing, so I don't see why that would be a problem even if I had included it multiple times.
    Declaring your functions inline should address that problem. While it is true that the implementation (compiler, linker, etc as a suite) might decide not to inline a function that your code declares as inline ("inline" is a hint, not a directive) then the implementation is required to sort things out.

    Without seeing samples of actual code (a sample header, and a sample of two source files that include that header, and exhibit the problem) it is harder to give more specific advice.
    Well, I attached the header with the function definitions in it, so you can take a look.
    Last edited by Programmer_P; 03-04-2011 at 02:46 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Header guards guard against including the same file multiple times in the same source file, not against including it into multiple source files (once).
    The single header is not enough to identify the problem. You should provide the smallest possible compilable example that demonstrates the problem.
    Last edited by Elysia; 03-04-2011 at 02:47 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 5 times "local function definitions are illegal"
    By Borislav in forum C++ Programming
    Replies: 24
    Last Post: 01-11-2011, 02:37 AM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM