| | |
How do I link my stuff?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 15
Reputation:
Solved Threads: 0
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:
here are the two headers in case I messed those up:
and the second one:
thanks in advance.
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:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <stdexcept> #include "PromptModule.h" #include "ErrorHandlingModule.h" using namespace std; char GetOperator(void) { char theOperator; cout << "Operator: "; cin >> theOperator; return theOperator; } float GetOperand(void) { float theOperand = 1; cout << "The Operand: "; cin >> theOperand; return theOperand; } float Accumulate (const char theOperator,const float theOperand) { static float myAccumulator = 0; //Inititalize to 0 when the program starts switch (theOperator) { case '+': myAccumulator = myAccumulator + theOperand; break; case '-': myAccumulator = myAccumulator - theOperand; break; case '*': myAccumulator = myAccumulator * theOperand; break; case '/': myAccumulator = myAccumulator / theOperand; break; default: throw runtime_error("Error - Invalid Operator"); }; return myAccumulator; } int main(int argc, char* argv[]) { SAMSErrorHandling::Initialize(); do { try { char Operator = GetOperator(); float Operand = GetOperand(); cout << Accumulate(Operator,Operand) << endl; } catch (runtime_error RuntimeError) { SAMSErrorHandling::HandleRuntimeError(RuntimeError); } catch (...) { SAMSErrorHandling::HandleNotANumberError(); }; } while (SAMSPrompt::UserWantsToContinueYOrN("More ?")); return 0; }
here are the two headers in case I messed those up:
C++ Syntax (Toggle Plain Text)
#ifndef PromptModuleH #define PromptModuleH namespace SAMSPrompt { bool UserWantsToContinueYOrN (const char *theThingWeAreDoing); } #endif
and the second one:
C++ Syntax (Toggle Plain Text)
#ifndef ErrorHandlingModuleH #define ErrorHandlingModuleH #include <stdexcept> using namespace std; namespace SAMSErrorHandling { void Initialize(void); int HandleNotANumberError(void); //Returns error code int HandleRuntimeError(runtime_error theRuntimeError); } #endif
thanks in advance.
•
•
Join Date: Jan 2008
Posts: 15
Reputation:
Solved Threads: 0
well these are the rest of the files i haven't showed
ErrorHandlingModule.cpp
andd......
PromptModule.cpp
I've got them all to object files with no problem...
never linked before though x_x
ErrorHandlingModule.cpp
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <stdexcept> #include "ErrorHandlingModule.h" using namespace std; namespace SAMSErrorHandling { using namespace std; void Inititalize(void) { cin.exceptions(cin.failbit); } int HandleNotAnumberError(void) //Returns error code { cerr << "Input error - not a number?" << endl; cin.clear(); //Eat the bad input so we can pause the program char BadInput[5]; cin >> BadInput; return 1; // An error occured } int HandleRuntimeError(runtime_error theRuntimeError) { cerr << theRuntimeError.what() << endl; return 1; } }
andd......
PromptModule.cpp
C++ Syntax (Toggle Plain Text)
#include <iostream> #include "PromptModule.h" using namespace std; namespace SAMSPrompt { void PauseForUserAcknowledgement(void) { //Note: You must type something before Enter char StopCharacter; cout << endl << "Press a key and \"Enter\": "; cin >> StopCharacter; } bool UserWantstoContinueYOrN (const char *theThingWeAreDoing) { char DoneCharacter; bool InvalidCharacterWasEntered = false; do { cout << endl << theThingWeAreDoing << " - Press \'n\' and \'Enter\' to stop: "; cin >> DoneCharacter; InvalidCharacterWasEntered = ! ( (DoneCharacter == 'y') || (DoneCharacter == 'n') ); if (InvalidCharacterWasEntered) { cout << "...Error - " << "please enter \"y\" or \"n\"."<<endl; }; }while (InvalidCharacterWasEntered); return (DoneCharacter != 'n'); } }
I've got them all to object files with no problem...
never linked before though x_x
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
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Need help with a simple while loop
- Next Thread: Why the second loop does'nt work??
| Thread Tools | Search this Thread |
Tag cloud for c++, error, linker
429 apache api application array arrays asp assignment beginner binary bitmap blue borland bsod c# c++ char char* class code compile compiler compression console development display division download ebook email encryption equation error file fstream function functions game gdi+ gmail graph guessing gui input int java jni keyboard linker management math microsoft modal mysql newbie numbers office operand output parallel parse php pointers primenumbersinrange problem proficiency program programing programming project python read recursion recursive registry rpg screen server small string strings struct syntax system temperature template templates text timer tree tutorial update url vb vector vista visual win32 windows xml






