How do I link my stuff?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 15
Reputation: thetechguy is an unknown quantity at this point 
Solved Threads: 0
thetechguy thetechguy is offline Offline
Newbie Poster

How do I link my stuff?

 
0
  #1
Nov 1st, 2009
I'm newish to c++ and even newer to ubuntu

I tried linking and this is what I got:

g++ -o calculator *.o -lcrypt -lm
main.o: In function `main':
main.cpp: (.text+0x1ea): undefined reference to `SAMSErrorHandling::Initialize()'
main.cpp: (.text+0x2fa): undefined reference to `SAMSErrorHandling::HandleNotANumberError()'
main.cpp: (.text+0x322): undefined reference to `SAMSPrompt::UserWantsToContinueYOrN(char const*)'
collect2: ld returned 1 exit status


let it be noted I've never linked anything using command line
Dev-C++ used to magically do all that stuff for me.
Not sure what the problem is...
Someone reccomended I put "using namespace SAMSErrorHandling" in main.cpp but the error message remains the same
everything compiles fine, it's just putting these all together into something I can run that's giving me the probelm

here's main.cpp:

  1. #include <iostream>
  2. #include <stdexcept>
  3. #include "PromptModule.h"
  4. #include "ErrorHandlingModule.h"
  5.  
  6. using namespace std;
  7.  
  8.  
  9. char GetOperator(void)
  10. {
  11. char theOperator;
  12. cout << "Operator: ";
  13. cin >> theOperator;
  14. return theOperator;
  15. }
  16.  
  17. float GetOperand(void)
  18. {
  19. float theOperand = 1;
  20. cout << "The Operand: ";
  21. cin >> theOperand;
  22. return theOperand;
  23. }
  24.  
  25.  
  26.  
  27.  
  28.  
  29. float Accumulate (const char theOperator,const float theOperand)
  30. {
  31. static float myAccumulator = 0; //Inititalize to 0 when the program starts
  32. switch (theOperator)
  33. {
  34. case '+':
  35. myAccumulator = myAccumulator + theOperand;
  36. break;
  37.  
  38. case '-':
  39. myAccumulator = myAccumulator - theOperand;
  40. break;
  41. case '*':
  42. myAccumulator = myAccumulator * theOperand;
  43. break;
  44. case '/':
  45. myAccumulator = myAccumulator / theOperand;
  46. break;
  47. default:
  48. throw
  49. runtime_error("Error - Invalid Operator");
  50. };
  51. return myAccumulator;
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58. int main(int argc, char* argv[])
  59. {
  60. SAMSErrorHandling::Initialize();
  61. do
  62. {
  63. try
  64. {
  65. char Operator = GetOperator();
  66. float Operand = GetOperand();
  67. cout << Accumulate(Operator,Operand) << endl;
  68. }
  69. catch (runtime_error RuntimeError)
  70. {
  71. SAMSErrorHandling::HandleRuntimeError(RuntimeError);
  72. }
  73. catch (...)
  74. {
  75. SAMSErrorHandling::HandleNotANumberError();
  76. };
  77. }
  78. while (SAMSPrompt::UserWantsToContinueYOrN("More ?"));
  79. return 0;
  80. }

here are the two headers in case I messed those up:
  1. #ifndef PromptModuleH
  2. #define PromptModuleH
  3.  
  4.  
  5. namespace SAMSPrompt
  6. {
  7. bool UserWantsToContinueYOrN (const char *theThingWeAreDoing);
  8. }
  9. #endif

and the second one:
  1. #ifndef ErrorHandlingModuleH
  2. #define ErrorHandlingModuleH
  3. #include <stdexcept>
  4.  
  5. using namespace std;
  6.  
  7. namespace SAMSErrorHandling
  8. {
  9. void Initialize(void);
  10. int HandleNotANumberError(void); //Returns error code
  11. int HandleRuntimeError(runtime_error theRuntimeError);
  12. }
  13.  
  14. #endif

thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 416
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 48
gerard4143's Avatar
gerard4143 gerard4143 is online now Online
Posting Pro in Training
 
0
  #2
Nov 1st, 2009
If you could list your files, I will show how to compile and link them
Last edited by gerard4143; Nov 1st, 2009 at 9:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 15
Reputation: thetechguy is an unknown quantity at this point 
Solved Threads: 0
thetechguy thetechguy is offline Offline
Newbie Poster

mmk

 
0
  #3
Nov 1st, 2009
well these are the rest of the files i haven't showed

ErrorHandlingModule.cpp
  1. #include <iostream>
  2. #include <stdexcept>
  3. #include "ErrorHandlingModule.h"
  4.  
  5. using namespace std;
  6.  
  7. namespace SAMSErrorHandling
  8. {
  9. using namespace std;
  10.  
  11. void Inititalize(void)
  12. {
  13. cin.exceptions(cin.failbit);
  14. }
  15.  
  16. int HandleNotAnumberError(void) //Returns error code
  17. {
  18. cerr << "Input error - not a number?" << endl;
  19. cin.clear();
  20. //Eat the bad input so we can pause the program
  21. char BadInput[5];
  22. cin >> BadInput;
  23. return 1; // An error occured
  24. }
  25. int HandleRuntimeError(runtime_error theRuntimeError)
  26. {
  27. cerr << theRuntimeError.what() << endl;
  28. return 1;
  29. }
  30. }

andd......
PromptModule.cpp

  1. #include <iostream>
  2. #include "PromptModule.h"
  3.  
  4. using namespace std;
  5.  
  6. namespace SAMSPrompt
  7. {
  8.  
  9. void PauseForUserAcknowledgement(void)
  10. {
  11. //Note: You must type something before Enter
  12. char StopCharacter;
  13. cout << endl << "Press a key and \"Enter\": ";
  14. cin >> StopCharacter;
  15. }
  16.  
  17. bool UserWantstoContinueYOrN (const char *theThingWeAreDoing)
  18. {
  19. char DoneCharacter;
  20. bool InvalidCharacterWasEntered = false;
  21. do
  22. {
  23. cout << endl << theThingWeAreDoing << " - Press \'n\' and \'Enter\' to stop: ";
  24. cin >> DoneCharacter;
  25.  
  26. InvalidCharacterWasEntered =
  27. !
  28. (
  29. (DoneCharacter == 'y')
  30. ||
  31. (DoneCharacter == 'n')
  32. );
  33.  
  34. if (InvalidCharacterWasEntered)
  35. {
  36. cout << "...Error - " << "please enter \"y\" or \"n\"."<<endl;
  37. };
  38. }while (InvalidCharacterWasEntered);
  39. return (DoneCharacter != 'n');
  40.  
  41. }
  42. }

I've got them all to object files with no problem...
never linked before though x_x
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 416
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 48
gerard4143's Avatar
gerard4143 gerard4143 is online now Online
Posting Pro in Training
 
0
  #4
Nov 1st, 2009
All I was looking for was the file names....Oh well
If you have the files compiled to object files then all you have to do is collect them together for the final linking/compiling...

g++ ErrorHandlingModule.o PromptModule.o main.o -o exefilename -lcrypt -lm

I'm assuming the switches at the end are correct(I got them from your original posting)

Also a great thing to learn is creating makfiles...here's a link

http://makepp.sourceforge.net/1.19/makepp_tutorial.html
Last edited by gerard4143; Nov 1st, 2009 at 10:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 15
Reputation: thetechguy is an unknown quantity at this point 
Solved Threads: 0
thetechguy thetechguy is offline Offline
Newbie Poster

hmm..

 
0
  #5
Nov 1st, 2009
tried that but I'm still getting that same error message
somewhere in my code I must have messed something up
as I'm pretty sure I'm using the correct commands to link it
Reply With Quote Quick reply to this message  
Reply

Tags
c++, error, linker

Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for c++, error, linker
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC