Andes Workshop
http://forum.andestech.com/

How to generate a static library and how to use it
http://forum.andestech.com/viewtopic.php?f=23&t=745
Page 1 of 1

Author:  HuJin [ Thu Oct 24, 2013 1:08 pm ]
Post subject:  How to generate a static library and how to use it

This article will take an easy example to illustrate how to generate a static library and how to use the generated static library in AndeSight.

Section A: How to generate a static library

Step1: Open AndeSight. Click "File" --> "New" --> "C Project", "Project Type" choose "Andes Static Library" --> "Empty Project", select toolchain and key in project name "StaticLib". As:
Attachment:
pic1.png
pic1.png [ 53.12 KiB | Viewed 6912 times ]


Step2: "StaticLib" is shown in Project Exploer. Right click on "StaticLib", select "New" --> "Source File", Key in the source file name "lib.c".As:
Attachment:
pic2.png
pic2.png [ 113.76 KiB | Viewed 6912 times ]

Attachment:
pic3.png
pic3.png [ 45.31 KiB | Viewed 6912 times ]


Step3: Write the follow program code in lib.c:
int add(int x,int y)
{
return x + y;
}
Attachment:
pic4.png
pic4.png [ 53.44 KiB | Viewed 6912 times ]


Step4: Build the "StaticLib", the output is in the "Archives" folder. As:
Attachment:
pic5.png
pic5.png [ 35.13 KiB | Viewed 6912 times ]



Section B: How to use a static library in a Project
It's an example of Hello World Project to use the library "libStaticLib.a".

Step1: New a hello world project, as:
Attachment:
pic6.png
pic6.png [ 46.71 KiB | Viewed 6912 times ]


Step2: Copy the library file "libStaticLib.a" to "HelloWorld" Project, as:
Attachment:
pic7.png
pic7.png [ 56.39 KiB | Viewed 6912 times ]


Step3: Right click on "HelloWorld", select "New" --> "Header File", key in name "lib.h", and write the source coce in lib.h. As:
Attachment:
pic8.png
pic8.png [ 120.2 KiB | Viewed 6912 times ]

Attachment:
pic9.png
pic9.png [ 75.98 KiB | Viewed 6912 times ]

Attachment:
pic10.png
pic10.png [ 95.25 KiB | Viewed 6912 times ]


Step3: open file main.c, input the follow program:
#include <stdio.h>
#include <stdlib.h>
#include "lib.h"
int main(void) {
int x, y;
x = 6;
y = 5;
printf("add : %d\n", add(x, y));
return 0;
}
Attachment:
pic11.png
pic11.png [ 110.04 KiB | Viewed 6912 times ]


Step4: This step is critical, we should do the setting to classifty the library path. Right click on "HelloWorld", select "Proproties", on Proproties Tab, click "C/C++ Build" --> "Settings" --> "Andes C Linker" --> "Libraries", put "StaticLib" in "Library"(not the name "libStatlicLib.a") and put "${workspace_loc:/${ProjName}}" in "Library search Path". As:
Attachment:
pic12.png
pic12.png [ 285.27 KiB | Viewed 6905 times ]


Step5: Build & Run the "HelloWorld" Project, we can see the program output is like our expects.
Attachment:
pic13.png
pic13.png [ 34.27 KiB | Viewed 6912 times ]

Page 1 of 1 All times are UTC + 8 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/