ITeye博客

Windows下Eclipse C开发加入第三方静态库

danlov · 2014-12-01 10:12 · Eclipse C · 编程语言 · Markdown

由于参考资料比较少,花了一整天经过不断尝试,终于成功在Eclipse中C工程里加入第三方静态库。

  环境:Windows7 + Eclipse IDE for C/C++ Developers4.4.1 + cygwin
  
  过程如下:
  第一步:新建一Static Library工程“libtest”,作为静态库进行测试
  1.新建Static Library C工程
  
  ![](http://dl2.iteye.com/upload/attachment/0103/7360/11f2a40b-cb23-3de8-8c6e-871492a67820.png)
   点击完成即可。
  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

    最后用控制台运行程序,可以看看效果:

程序运行良好!

   总结:上面的例子非常简单,但是达到了最原始的目的,如果出现很复杂的情况,比如要引用的头文件非常多,如何指定头文件的路径有待研究。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。