Languages

Menu
Sites
Language
static library?

1. How do I call a function in a static library?
2. The structure is in a static library, how do you use in the call, please?

Responses

5 Replies
Alex Dem

Hi,
If you have added library's header into project file and have build project successfully: just call required method directly .
Alexey.

colin Rao

Hi,

For point 2, you should put the struct define in the header file, and input it into your project, you can use is as a normal standard c lib, such as tm in time.h

For point 1, I've tried to create a static library project, build success. Then i copy the header file to another project, i can use the strcut which defined in the header file of static library project, build success, but while I try to call the static library function, it raise an error "undefined reference to ...", I've do such test, put the compiled static library (.a, .o) to /lib folder. I can't find the .so file, any one know the root cause?

Alex Dem

Hi Colin, 
Here is tutorial (it was created for 2.2.1) but most of items applicable for 2.3:
https://developer.tizen.org/documentation/articles/working-static-libraries-tizen

So you should build static library first. After you need link static libray to your application:
1. You could copy 'libstatlic.a' file to your project  into (../lib) and header into (../hdr ) folders and include the path in Tizen C++ Linker > Miscellaneous ->Other objects: "${workspace_loc:/${ProjName}/lib/libstatlic.a}"
2. You could do not copy but include header from static library project "../../statLibPrj/inc/statlibprj.h" (for example) and path to *.a file in Tizen C++ Linker-> Miscellaneous ->Other objects: ${workspace_loc:/statLibPrj/Debug/libstatlib.a}
In my opinion the way does not matter. But in both cases functions from library is called.
Alexey.

colin Rao

Hi Alex,

Thanks, as your link my problem was resolved. I just define a simple struct and an interface, then I can use the struct and call the interface in another project.

Static Library:

#ifndef _MYSTATICLIBRARY_H_
#define _MYSTATICLIBRARY_H_

/**
 * This header file is included to define _EXPORT_.
 */
#include <stdbool.h>

struct myStruc {
    int a;
	int b;
	int sum;
};


// This method is exported from mystaticlibrary.so
extern int sum_sl(int *a, int *b, int *sum);

#endif // _MYSTATICLIBRARY_H_



/**
 * This file contains the exported symbol.
 */
#include "mystaticlibrary.h"

// This is an example of an exported method.
int
sum_sl(int *a, int *b, int *sum)
{
    *sum = *a + *b;
	return *sum;
}

Test code in another project,

    struct myStruc s;
	s.a = 10;
	s.b = 5;
	s.sum = s.a + s.b;
	sum_sl(&s.a, &s.b, &s.sum);
	dlog_print(DLOG_INFO, LOG_TAG, "s.a + s.b = %d", s.sum);

 

Sathya Priya R

Hi,

I have added some classes with methods and built the static lib.

How do we export these class methods?

Say for example, inside the static lib there is a class,

Class A{

int mValue;

public:

A();

int AddNumbers(int a){

return (this->mVaue + a);

}

};

How do we call the AddNumbers? Where should we add the "extern" keyword ??

Thanks.