Thread: Using static libraries

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    Using static libraries

    Hello,

    I have developed a static library. The header for this static library is below:

    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);
    	};
    }
    Source file below for that uses this header file:
    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;
    	}
    }
    I compiled this library into a object file using the following which worked ok.
    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:
    Code:
    #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;
    }
    I am using kate as I am using Kubuntu to compile my programs
    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

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Use -lMathFunctions, not -llibMathFunctions. The lib part is always implicit.

    Also, why does your test program have "using namespace MathFunctions" when you then go and fully qualify the names anyway?

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    They are in the namespace MathFunctions, but his class name is MyMathFunctions. So he is calling the static member of the class in that instance.
    Woop?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Is a class that contains nothing but static functions really any better than a bunch of free functions (possibly in a namespace)?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Not to my knowledge.
    Woop?

  6. #6
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    Thanks it worked.

    steve@steve01:~/Projects/Calculator/src$ g++ -o ../bin/Calculator MyCalculator.cpp -I ../include -L ../lib -lMathFunctions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM