473,883 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ Linking error

kiseitai2
93 New Member
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 2202
oler1s
671 Recognized Expert Contributor
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 Recognized Expert MVP
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
kiseitai2
93 New Member
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 Recognized Expert Moderator Expert
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
kiseitai2
93 New Member
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
kiseitai2
93 New Member
The last line in the log says " E:\LUIS\mk\Make file.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
kiseitai2
93 New Member
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 Recognized Expert Moderator Expert
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
kiseitai2
93 New Member
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

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

Similar topics

0
1735
by: Wolfgang | last post by:
I have a problem with linking my CPP Code under a irix6 machine (sgi, UNIX). In my CPP code I use some Functions which are written in Python. So its a kind of CPP wrapper for my Python functions In my Python Code I use threads to communicate over the network and stuff like this. Compilation and linking are working very well under Windows and Linux with the same code. Under the sgi, UNIX machine some errors occur and I don't no why....
3
14560
by: Saurabh Aggrawal | last post by:
Hi, I am porting an application for 64-bit AMD processor and while linking the application i am getting the following errors: Processing directory uidll... Linking DLL G:\NewUIPackers1\drivers\Unser\bin\WIN32\DEBUG\PPKAW1UI.dll .... LINK : warning LNK4224: /PDBTYPE is no longer supported; ignored UIDLL.def : error LNK2001: unresolved external symbol DllCanUnloadNow
7
6562
by: wmkew | last post by:
Hello everyone I'm encountering a R6002 Runtime error and several bugs when trying to generate a simple Managed C++ application with .NET 2003. The main problem seems to arise from linking with LIBCMT(D).DLL. (My requirement is that we can't link with MSVCRT(D).LIB.) Below are steps I've followed, and the resulting problems 1. Using the New Project wizard, generate a Visual C++ .NET Class Library project (call it "Doomed") and a VC++...
1
8129
by: Joannes Vermorel | last post by:
I am currently trying to port a small open source scientfic library written in C++ to .Net. The code (including the VS solution) could be found at http://www.vermorel.com/opensource/selfscaling.zip My problem is that when I try to compile the library I got a list of linking error messages. I am not a specialist of porting C++ code to .Net. Does anyone has an idea on how to make this code compile in .Net ? Thanks, Joannes Vermorel
1
9492
by: Kay | last post by:
I already specified to ignore specific library: MSVCPRT.lib MSVCRT.lib LIBC.lib MSVCRTD.lib LIBCD.lib command line is like: /INCREMENTAL /NOLOGO /DLL /NODEFAULTLIB:"MSVCPRT.lib MSVCRT.lib LIBC.lib MSVCRTD.lib LIBCD.lib" but I am still getting conflict linking problems. But if I do specify /NODEFAULTLIB, I'll get anther bunch of linking errors. Anybody knows why? thanks,
4
6418
by: Sanjay Kumar | last post by:
Folks ! I am working with VC++ after a long time and having problem linking latest xerces 2.7 in VC++ 2005 Express Edition. I have done following: 1. downloaded and unpacked the the library: http://www.apache.org/dist/xml/xerces-c/binaries/xerces-c_2_7_0-windows_2000-msvc_60.zip
0
4894
by: Adam Clauss | last post by:
I have managed C++ library (is bridging between a Win32 .dll and a C# application). All was well when compiled under VS2003, but I am running into a series of linking errors when compiling against VS2005. They all/mostly seem to be within the STL. Any idea what might cause something like this? Linking errors follow. ---
0
2560
by: Philip Lowman | last post by:
I am in the process of trying to migrate a couple of build solutions to Visual Studio Express 2005 from VS 2003 Professional and I am running into a weird C/C++ runtime library linking issue when using the /MT compilation option. Our debug solution's /MTd flag works fine and using /MD also seems to work ok. For some reason I can't fathom, when I use /MT, linking the static excutable completely dies (problems resolving symbols in the STL,...
0
3992
by: xieml2007 | last post by:
Dear Madam or Sir, I encountered one problem which is quite similiar to the discussions launched at the web site: http://www.thescripts.com/forum/thread280324.html
0
2975
by: dotyet | last post by:
Hi Everyone, I am trying to build a DB2 UDB UDF which can perform regex over the table data. The user defined function will call an external .dll file to do the task. I am referring to the following link for doing so: http://www.ibm.com/developerworks/db2/library/techarticle/0301stolze/0301stolze.html While linking the below mentioned code, I am facing some errors. I have installed PCRE and am using Visual C++ 6.0 in 32-bit mode. The
0
9798
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10766
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10863
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9588
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7978
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5807
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3241
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.