Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

howto add a static library (.a) into a C++ program?

Tags:

I want to know how I can use a static library in C++ which I created, first the lib:

// header: foo.h
int foo(int a);

.

// code: foo.cpp
#include foo.h
int foo(int a)
{
    return a+1;
}

then I compile the library first:

  1. g++ foo.cpp
  2. ar rc libfoo.a foo.o

now I want to use these library in some file like:

// prog.cpp
#include "foo.h"
int main()
{ 
    int i = foo(2);
    return i;
}

how must I compile these now? I made:

g++ -L. -lfoo prog.cpp

but get an error because the function foo would not be found