Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use function from static library if I don't have header file

Tags:

Is it a way to use function from static lib, if I don't have header file, only *.a file, but I know function signature?

like image 327
Hate Avatar asked Oct 06 '11 10:10

Hate


People also ask

Do you need header files for static library?

No. A library contains solely and only object files. Headers are not object files. Headers are not contained in libraries.

How do you call a library function in C++?

To call a function funcname in C++ library libname with input arguments arg1,arg2,... and output argument retVal , use the MATLAB clib package. MATLAB automatically loads the library when you type: retVal = clib. libname.

Where to find c libraries?

Libaries consist of a set of related functions to perform a common task; for example, the standard C library, 'libc. a', is automatically linked into your programs by the “gcc” compiler and can be found at /usr/lib/libc. a. Standard system libraries are usually found in /lib and /usr/lib/ directories.


2 Answers

Yes, if you know the function signature

Just write the function signature before calling it, as:

void f(int); //it is as if you've included a header file  //then call it f(100); 

All you need to do is : link the slib.a to the program.

Also, remember that, if the static library is written in C and has been compiled with C compiler, then you've to use extern "C" when writing the function signature (if you program in C++), as:

extern "C" void f(int); //it is as if you've included a header file  //then call it f(100); 

Alternatively, if you've many functions, then you can group them together as:

extern "C"  {    void f(int);     void g(int, int);     void h(int, const char*); }  

You may prefer writing all the function signatures in a namespace so as to avoid any possible name-collisions:

namespace capi {   extern "C"    {     void f(int);      void g(int, int);      void h(int, const char*);   }  }  //use them as:  capi::f(100);  capi::g(100,200);  capi::h(100,200, "string");  

Now you can write all these in a header file so that you could include the header file in your .cpp files (as usual), and call the function(s) (as usual).

Hope that helps.

like image 59
Nawaz Avatar answered Oct 07 '22 17:10

Nawaz


The easiest way: Write the signature in a header file, include it and link against the library.

like image 20
thiton Avatar answered Oct 07 '22 17:10

thiton