由于参考资料比较少,花了一整天经过不断尝试,终于成功在Eclipse中C工程里加入第三方静态库。
环境:Windows7 + Eclipse IDE for C/C++ Developers4.4.1 + cygwin
过程如下:
第一步:新建一Static Library工程“libtest”,作为静态库进行测试
1.新建Static Library C工程
点击完成即可。
2.编写.h头文件与.c源代码文件(参考《C Primer Plus》里的例子)
list.h
#ifndef LISTTEST_LIST_H_
#define LISTTEST_LIST_H_
#include <stdbool.h>
#define TSIZE 45
struct film {
char title[TSIZE];
int rating;
};
typedef struct film Item;
typedef struct node {
Item item;
struct node *next;
} Node;
typedef Node List;
void InitializeList(List *plist);
bool ListIsEmpty(const List *plist);
bool ListIsFull(const List *plist);
unsigned int ListItemCount(const List *plist);
bool AddItem(Item item,List **plist);
void Travers(const List *plist,void (*pfun)(Item item));
void EmptyTheList(List *plist);
#endif /* LISTTEST_LIST_H_ */
list.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
static void CopyToNode(Item item, Node *pnode);
void InitializeList(List *plist) {
plist = NULL;
}
bool ListIsEmpty(const List *plist) {
if (plist == NULL) {
return true;
} else {
return false;
}
}
bool ListIsFull(const List *plist) {
Node *pt;
bool full;
pt = (Node*) malloc(sizeof(Node));
if (pt == NULL) {
full = true;
} else {
full = false;
}
return full;
}
unsigned int ListItemCount(const List *plist) {
unsigned int count = 0;
Node *pnode = plist;
while (pnode != NULL) {
++count;
pnode = pnode->next;
}
return count;
}
bool AddItem(Item item, List **plist) {
Node *pnew;
printf("-------------->%p", pnew);
Node *scan = *plist;
pnew = (Node *) malloc(sizeof(Node));
if (pnew == NULL) {
return false;
}
CopyToNode(item, pnew);
pnew->next = NULL;
if (scan == NULL) {
*plist = pnew;
} else {
while (scan->next != NULL) {
scan = scan->next;
}
scan->next = pnew;
}
return true;
}
void Travers(const List *plist, void (*pfun)(Item ite)) {
Node *pnode = plist;
while (pnode != NULL) {
(*pfun)(pnode->item);
pnode = pnode->next;
}
}
void EmptyTheList(List *plist) {
Node *psave;
while (plist != NULL) {
psave = plist->next;
free(plist);
plist = psave;
}
}
static void CopyToNode(Item item, Node *pnode) {
pnode->item = item;
}
3.编译工程,生成.a静态库
如上所示,编译后,生成名为"liblisttest.a"的静态库。该工程的设置走的默认路线,可以看看:
第二步:新建Executable工程,并链接上一步生成的liblisttest.a进行验证
1.新建testLib工程:
点击完成即可
2.将list.h头文件拷入工程并新建一测试源程序film.c
film.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void showmovies(Item item);
int main() {
List *head;
Item temp;
if (ListIsFull(head)) {
fprintf(stderr,"No memory availbale! Bye!\n");
exit(1);
}
puts("Enter first movie title:");
while (gets(temp.title) != NULL && temp.title[0] != '\0') {
puts("Enter your rating <0-10>:");
scanf("%d",&temp.rating);
while (getchar() != '\n') {
continue;
}
if (AddItem(temp,&head) == false) {
fprintf(stderr,"Problem allocating memory\n");
break;
}
if (ListIsFull(head)) {
puts("The list is now full.");
break;
}
puts("Enter your next movie title (empty line to stop):");
}
if (ListIsEmpty(head)) {
printf("No data entered.\n");
} else {
printf("Here is the moives list : \n");
Travers(head,showmovies);
}
printf("You entered %d movies.\n",ListItemCount(head));
EmptyTheList(head);
printf("Bye!\n");
return 0;
}
void showmovies(Item item) {
printf("Movie: %s Rating :%d\n",item.title,item.rating);
}
工程结构如下:
3.最关键的的一步,指定-L库的路径与-l需要链接的库.
测试时将liblisttest.a拷入到F:\libs下(随便指定,测试用,其它路径均可)
因为Eclipse下很多参数都是图形界面设置好后,会自动生成makefile,故我们只需要设置就行,具体的设置”右键工程-Properties-C/C++ Build-Settings-Tool Settings-Cygwin C Linker - Libraries“,打开后如下所示:
在右上与右上分别设置要链接的库与库的路径,本例子中分别为:listtest、"F:\libs",如图:
点击OK,编译工程 ,最后顺利生成testLib.est
最后用控制台运行程序,可以看看效果:
程序运行良好!
总结:上面的例子非常简单,但是达到了最原始的目的,如果出现很复杂的情况,比如要引用的头文件非常多,如何指定头文件的路径有待研究。
- 大小: 50.3 KB
- 大小: 6.1 KB
- 大小: 44.3 KB
- 大小: 52 KB
- 大小: 48.9 KB
- 大小: 3.5 KB
- 大小: 69.7 KB
- 大小: 57.7 KB
- 大小: 67.8 KB
- 大小: 7 KB
- 大小: 42.4 KB
分享到:
相关推荐
Windows下用Eclipse搭建C语言开发 pdf文档
Windows_下使用Eclipse开发Gtk
eclipse插件开发_第三版 (英文)
新手上路搭建环境,现在android的学习越来越受广大的学生欢迎,虽然有的学校已经开始有课程,但是对于开源的java来说,只要想学习网上一大把资料,这个就是windows下Eclipse开发环境的搭建供广大朋友参考
2.再删除eclipse的configuration目录下org.eclipse.update文件 3.重启eclipse,点击菜单栏中File->Restart 4.下载jad的可执行文件,解压后放在某一磁盘中.链接:http://varaneckas.com/jad/ 5.打开Eclipse,prefrence...
eclipse本身不能导出第三方jar包,以上为用eclipse导出第三方jar包的插件fatjar
eclipse集成开发环境C语言版本,可开发C语言,C++,使用方便快捷
Windows下Eclipse搭建Android_NDK开发环境
windows+eclipse+cdt开发环境配置
Windows下eclipse的 C++环境配置
使用eclipse搭建C语言开发的环境,步骤比较清晰,实践简单。
eclipse 插件开发,第三版(英文)2009
eclipse tomcat插件 第三方插件
讲述如何获取并安装Windows下的Eclipse,使用Eclipse开发Java应用程序,并介绍如何使用Eclipse调试Java的应用程。 主讲:陈宝峰
eclipse 第三方jar包配置.txt ,不使用eclipse自带的buildpath功能!
本书由浅入深、有重点、有针对性地介绍了Eclipse插件开发技术,全书分为4篇共24章。第一篇介绍Eclipse平台界面开发的...Eclipse插件开发学习笔记 第三四篇(26-28)+原代码.rar 下载资源后请评分,则会把资源分返还!!
Eclipse插件开发学习笔记 第三四篇(26-28)+原代码.rar 也可以直接使用下面的地址: http://download.csdn.net/source/1624919 http://download.csdn.net/source/1624984 http://download.csdn.net/source/1631186 ...
配图一步一步的讲解一下,如何去配置基于Eclipse的PHP开发环境
eclipse生成库和调用方式,因为算法及模块编程的不可拆分及版权保护的需求,要生成库lib,再应用到各方案中,故介绍DA14681如何生成库,生成后如何应用库lib。