Hello,
I have developed a static library. The header for this static library is below:
Source file below for that uses this header file:Code://MathFunctionsLib.hpp namespace MathFunctions { class MyMathFunctions { public: //Return a + b static double Add(double a, double b); //Return a - b static double Subtract(double a, double b); //Return a * b static double Multiply(double a, double b); //Return a / b //Throws DivideByZeroException if b is 0 static double Divide(double a, double b); }; }
I compiled this library into a object file using the following which worked ok.Code://MathFunctionsLib.cpp #include "MathFunctionsLib.hpp" #include <stdexcept> using namespace std; namespace MathFunctions { double MyMathFunctions::Add(double a, double b) { return a + b; } double MyMathFunctions::Subtract(double a, double b) { return a - b; } double MyMathFunctions::Multiply(double a, double b) { return a * b; } double MyMathFunctions::Divide(double a, double b) { if(b == 0) { throw new invalid_argument("b cannot be zero"); } return a / b; } }
g++ -I MathFunctionsLib.cpp -o MathFunctionsLib.o
Then creating the archive
ar rcs libMathFunctions.a MathFunctions.o
The directory structure I have for linking this library into an existing project.
src/calculator.cpp
include/MathFunctionsLib.hpp
lib/libMathFunctions
bin
The calculator program that will use the library is this:
I am using kate as I am using Kubuntu to compile my programsCode:#include <iostream> #include "MathFunctionsLib.hpp" using namespace MathFunctions; int _main(int argc, char** argv) { double a, b, answer; answer = MyMathFunctions::Add(10, 10); std::cout << "The answer is: " << answer; getchar(); return 0; }
Projects/Calculator/src$ g++ -o ../bin/Calculator MyCalculator.cpp -I ../include -L ../lib -llibMathFunctions
However, when I try and compile the program and link with the library I get the following error message:
/usr/bin/ld: cannot find -llibMathFunctions
collect2: ld returned 1 exit status
Can anyone tell me where I am going wrong with this.
Many thanks for your time,
Steve



LinkBack URL
About LinkBacks


