Andes Workshop

It is currently Tue Aug 21, 2018 8:54 am

All times are UTC + 8 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: How to generate a static library and how to use it
PostPosted: Thu Oct 24, 2013 1:08 pm 
Offline

Joined: Fri May 04, 2012 6:49 am
Posts: 17
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 2147 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 2147 times ]

Attachment:
pic3.png
pic3.png [ 45.31 KiB | Viewed 2147 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 2147 times ]


Step4: Build the "StaticLib", the output is in the "Archives" folder. As:
Attachment:
pic5.png
pic5.png [ 35.13 KiB | Viewed 2147 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 2147 times ]


Step2: Copy the library file "libStaticLib.a" to "HelloWorld" Project, as:
Attachment:
pic7.png
pic7.png [ 56.39 KiB | Viewed 2147 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 2147 times ]

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

Attachment:
pic10.png
pic10.png [ 95.25 KiB | Viewed 2147 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 2147 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 2140 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 2147 times ]


Top
 Profile Send private message E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group