`
danlov
  • 浏览: 28351 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

 
阅读更多
      由于参考资料比较少,花了一整天经过不断尝试,终于成功在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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics