---
title: "Windows下Eclipse C开发加入第三方静态库"
author: "danlov"
date: "2014-12-01T10:12:20Z"
tags: []
categories: ["编程语言"]
slug: "windows-eclipse-c"
draft: false
lastmod: "2014-12-01T09:50:30Z"
id: 2161692
url: "https://danlov.iteye.com/blog/2161692"
---

由于参考资料比较少，花了一整天经过不断尝试，终于成功在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

```C
#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

```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静态库
      
![](http://dl2.iteye.com/upload/attachment/0103/7390/fee77793-d1b3-3b3c-a0ac-3cff5de1e284.png)

      如上所示，编译后，生成名为"liblisttest.a"的静态库。该工程的设置走的默认路线，可以看看：
      
![](http://dl2.iteye.com/upload/attachment/0103/7394/835a5c30-3bd3-3995-a6b2-4ac9c2d09208.png)

![](http://dl2.iteye.com/upload/attachment/0103/7396/6baabe3b-2398-3cd0-9379-6e4d4a2425ed.png)


      第二步：新建Executable工程，并链接上一步生成的liblisttest.a进行验证

      1.新建testLib工程：
      
![](http://dl2.iteye.com/upload/attachment/0103/7400/19b15408-381b-3f7d-8072-9a377a651b03.png)
       点击完成即可
 
      2.将list.h头文件拷入工程并新建一测试源程序film.c

        film.c
        
```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);
}
```

      工程结构如下：
      
![](http://dl2.iteye.com/upload/attachment/0103/7402/b8f0e7a9-6e4b-38ed-b2bf-98759fa7b5e3.png)

      3.最关键的的一步，指定-L库的路径与-l需要链接的库.

        测试时将liblisttest.a拷入到F:\libs下(随便指定，测试用，其它路径均可)
        
![](http://dl2.iteye.com/upload/attachment/0103/7410/9bb34406-8a4e-32c3-b371-41db9b38ac23.png)
       
        因为Eclipse下很多参数都是图形界面设置好后，会自动生成makefile，故我们只需要设置就行，具体的设置”右键工程-Properties-C/C++ Build-Settings-Tool Settings-Cygwin C Linker - Libraries“,打开后如下所示：
       
![](http://dl2.iteye.com/upload/attachment/0103/7434/174fb5ca-cf3f-3e51-99f8-ae6493834377.png)

         在右上与右上分别设置要链接的库与库的路径，本例子中分别为：listtest、"F:\libs"，如图：
         
![](http://dl2.iteye.com/upload/attachment/0103/7913/59dd9611-83c6-3ea8-8c5d-b8fa26db0b92.png)
         
        点击OK,编译工程 ，最后顺利生成testLib.est
                            
![](http://dl2.iteye.com/upload/attachment/0103/7919/16135bac-e5f3-3eae-b741-1712a4e684f6.png)

        最后用控制台运行程序，可以看看效果：
        
![](http://dl2.iteye.com/upload/attachment/0103/7925/03b0626d-c6a9-3640-8148-8ea8a845c63d.png)
       程序运行良好！

       总结：上面的例子非常简单，但是达到了最原始的目的，如果出现很复杂的情况，比如要引用的头文件非常多，如何指定头文件的路径有待研究。