469,944 Members | 2,305 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,944 developers. It's quick & easy.

C++ Linking error

kiseitai2
Hi everyone. My problem is tha Dev-C++ compiles perfectly fine but it fails in the linking process. I receive the linker error "multiple definitions of"; howerver,
I modified the names of the functions in three of the files, checked my code to make sure I wasn't redefining my functions, changed code back and forth from .cpp to .h, and even excluded some files from the project and included them manually. Also, I even recopied the lines of code to a clean new project.

I remeber being able to compile some part of the project separately (of course, I had to change the function names to main). The only thing I'm thinking is that dev-c++ (since the version I have is beta (4.9.9.2)) is having problems compiling c++ projects correctly, especially if it has a bunch of them. Also, I'm new to C/C++ but not to the programming world and I've been following every rule I know and have researched to prevent errors.

Another thing, the linker doesn't give me a reference other than pointing to the Makefile.win document where it supposely saw the error when parsing it.

I'm running Win XP, 1.5 gb RAM.
Language: C/C++ (Bloodshed Dev-C++ 4.9.9.2 beta 5 using Mingw32 compiler set)
Part of code in program that shows this problem and is the simplest to follow:

Expand|Select|Wrap|Line Numbers
  1. //mathConsole.cpp
  2. //preprocessors go here
  3. #include "Functions.h"
  4. #include "cminclude.h"
  5.  
  6. using namespace std;
  7.  
  8. //global vars and function prototypes go here
  9.  
  10.  
  11. //functions go here
  12. void mathConsole ()
  13. {
  14. //containers and vars go here
  15.    double num1 = 0;
  16.    double num2 = 0;
  17.    string sign = "";
  18.    vector<string> cmd;
  19.  
  20. //C++ conatiners' runtime initialization data
  21.  cmd.push_back("cmd");
  22.  cmd.push_back("add");
  23.  cmd.push_back("rest");
  24.  cmd.push_back("multiply");
  25.  cmd.push_back("divide");
  26.  cmd.push_back("sqr");  
  27.  
  28. //main
  29.         cout << "***_____________________________________________________________________________***\n";
  30.         cout << "***                                                                             ***\n";
  31.         cout << "***                Welcome to the Math Console. Version: 1.0.0.0                ***\n";
  32.         cout << "***                          A MK-1 Built-in Calculator                         ***\n";
  33.         cout << "***_____________________________________________________________________________***\n";
  34.         cout << "\n Listing commands available in this module...\n";   
  35.           for (int i = 0; i < cmd.size(); ++i)
  36.      {
  37.  
  38.          cout <<"--------------------------------------";
  39.          cout << i << ". " << cmd[i] << endl;
  40.          }
  41.  do
  42.     {
  43.       cout << "Type First Number\n";
  44.       cin >> num1;
  45.       cout << "Write sign as command. Example, sqr for square root.\n";    
  46.     cin >> sign;
  47.     if (sign == "add")
  48.     {
  49.     cout << "Type Next Number";
  50.     cin >> num2;
  51.     cout << add(num1, num2) ;
  52.     }
  53.     if (sign == "rest")
  54.     {
  55.      cout << "Type Next Number";
  56.     cin >> num2;
  57.     cout << rest(num1, num2);
  58.     }
  59.     if ( sign == "multiply")
  60.     {
  61.        cout << "Type Next Number";
  62.        cin >> num2;
  63.        cout <<  multiply(num1, num2);
  64.        } 
  65.     if (sign == "sqr")
  66.     {
  67.              cout << sqr(num1);
  68.              }
  69.     if (sign == "cmd")
  70.          {
  71.              for (int i = 0; i < cmd.size(); ++i)
  72.                {
  73.  
  74.                   cout <<"--------------------------------------";
  75.                   cout << i << ". " << cmd[i] << endl;
  76.                      }
  77.                      }
  78. }while ((sign != "exit") || (sign != "break"));
  79.         cout << "***_____________________________________________________________________________***\n";
  80.         cout << "***                                                                             ***\n";
  81.         cout << "***                                Welcome Back to MK-1                         ***\n";
  82.         cout << "***                                                                             ***\n";
  83.         cout << "***_____________________________________________________________________________***\n";
  84.  
  85. }
Expand|Select|Wrap|Line Numbers
  1. //Functions.h
  2. //prepocessor go here
  3.  
  4.  
  5.  //typedefs go here
  6.  typedef long double ldouble;
  7.  
  8.  
  9. //math functions go here
  10.  
  11. ldouble add (ldouble Number1, ldouble Number2)
  12. {
  13.      return (Number1 + Number2);
  14.      }
  15.  
  16. ldouble rest (ldouble Number1, ldouble Number2)
  17. {
  18.     return (Number1 - Number2);
  19. }
  20.  
  21. ldouble divide (ldouble Number1, ldouble Number2)
  22. {
  23.         return (Number1 / Number2);
  24.         }
  25.  
  26. ldouble multiply (ldouble Number1, ldouble Number2)
  27. {
  28.         return (Number1 * Number2);
  29.         }
  30.  
  31. ldouble e( ldouble baseNumber, ldouble exponent)
  32. {
  33.         ldouble countDown = exponent;
  34.         ldouble results;
  35.         do
  36.         {
  37.          if ((results = 0) && (baseNumber != results) )
  38.          {
  39.                      results = baseNumber;
  40.                      }
  41.          if (countDown != 0)
  42.          {
  43.                        }
  44.          else
  45.          {
  46.            results = results * results;
  47.            countDown -= 1;
  48.            }
  49.            }while (countDown != 0);  
  50.         return (results);
  51.         }
  52.  
  53. ldouble sqr( ldouble baseNumber)
  54. {
  55.         const float ROOT = (1/2);
  56.         return ( baseNumber * ROOT);
  57.         }
  58.  
  59.  
  60. //void functions go here
Expand|Select|Wrap|Line Numbers
  1. //cminclude.h
  2. //preprocessors go here
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <ctime>
Thank you for your help :).
Jul 18 '08 #1
10 2003
oler1s
671 Expert 512MB
Post your linker errors verbatim. (Generally, don't leave us hanging with "I have X error", post actual output for us to examine).

Are you are aware of the difference between a declaration and a definition?
Jul 18 '08 #2
JosAH
11,448 Expert 8TB
Defining (not just declaring) functions in a .h file (Functions.h) is a nono. Don't
do that, never; you might get away with it now but it is asking for serious trouble.

kind regards,

Jos
Jul 18 '08 #3
1--The compiler error is "multiple definitions of (function name)".
2--And I also changed the code in Functions.h to Functions.cpp and still same error.
Jul 18 '08 #4
Banfa
9,065 Expert Mod 8TB
1--The compiler error is "multiple definitions of (function name)".
This is not actual output of the linker when oler1s says "Post your linker errors verbatim" they mean exactly what you got as the output, a character for character copy. Not being precise when describing or reporting a problem will only lead to confusion.
2--And I also changed the code in Functions.h to Functions.cpp and still same error.
Were you still including functions.cpp or did you compile and link it? If the former you need to stop including it and compile it.
Jul 19 '08 #5
Here's a link to the screenshot I took of the compiler/ linker log in the IDE.
CLICK HERE to open. (if the link before doesn't work tell me so as soon as my new domain name is on I upload the picture and provide you the link)

If the IDE gave me more information I wouldn't, probably, be causing this much trouble.
Jul 19 '08 #6
The last line in the log says " E:\LUIS\mk\Makefile.win [Build Error] [bin/Mk-1.exe] Error 1 " (everything else is just plain as I've told you.

Wait a second... How will I be able to use the functions inside the Functions.cpp in mathConsole.cpp without typing "include "Functions.cpp"".
I mean in vb I just needed to add the file to the project and make the functions public, but in c++ I get the error massage "add undeclared( first use this function" and the compilation stops once removed the line of code.

However, thanks to that error I checked the last line and it seems the compiler was compling the functions into the main.o and also was making an object file for each of the other .cpp resource files. My new idea is that the compiler is creating the linking error by creating a main.o (with the mathConsole() which uses the add1() function) and a mathConsole.o (with the mathConsole() which uses the add() function). In other words, when it's the linker's turn to do the rest it links a main.o and a mathConsole.o that have instances and definitions of the same functions, thus, showing the error "multiple definitions of". I hope you are following my idea. if not I'll try to graph it below.

Expand|Select|Wrap|Line Numbers
  1.                 Linker turn (the second object creates the redefinition )
  2. ------------------------------Error producing--------------------------------
  3. main.0                                                   mathConsole.o
  4. |            |                        +                 |                |
  5. add() mathConsole()                      add()    mathConsole()
  6. --------------------Correct Version: Should have been instead--------------------------------
  7. main.0                                          
  8.  |                |                        +       (Other object that does not contains a     
  9. add() mathConsole()                        definition of the functions add() 
  10.                                                      and mathConsole())
  11. --------------------------------------------------------------------------------------------------------------
  12.  
If that's what's happening, then I'm in for big trouble because I don't know how to tell the compiler to stop creating the other object files (.o) and instead compile all the functions in one single file (main.o)
Jul 19 '08 #7
Sorry the graph above got messed up. It should have displayed like how I create it (my bad too cause I didn't preview it before posting).
Jul 19 '08 #8
Banfa
9,065 Expert Mod 8TB
OK what I (and Jos) said is correct, you should define functions in CPP files and you should never include CPP files into other CPP files.

But if you want to call a function in your first CPP file from a second CPP file then the second CPP file needs to see a declaration (not a definition) of the function. It is normal to put function declarations (and type and data declarations) into H (header) files to group them together and make the easy to include multiple times.

If a function definition looks like this (and goes in a CPP file)
Expand|Select|Wrap|Line Numbers
  1. int DoNothing(int input)
  2. {
  3.     return input;
  4. }
  5.  
then the declaration looks like this (and goes in a H file)
Expand|Select|Wrap|Line Numbers
  1. int DoNothing(int input);
  2.  
In your code I only see definitions, no declarations.

BTW I can't see what you tried to link to, the link doesn't work.
Jul 19 '08 #9
Hmm... Never thought about it that way... I think I get what you are saying now... I'll try it now.
Jul 19 '08 #10
I'm grateful I learn something today. Thank you so much. It really works. I hope this help other people.
Jul 19 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Saurabh Aggrawal | last post: by
1 post views Thread by Joannes Vermorel | last post: by
1 post views Thread by Kay | last post: by
4 posts views Thread by Sanjay Kumar | last post: by
reply views Thread by Adam Clauss | last post: by
reply views Thread by dotyet | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.