目标: 利用自己的函数,来调用其它打包好的静态lib库中的函数。
1 . 生成自己的lib库:(vs2008)
新建一个工程, 取好名称, 然后next到 Application type中设置为 Static library , 设置 Application options为 Empty project ,毕竟我不需要这个预编译的头文件。
2. 添加一个文件到该工程中 , 我这里想要打包到lib库中有一个函数, 名为hello(char *a) ,如下:
#include <stdio.h>
void hello(char *a){
printf("%s", a);
}
3 . 编译, 就会在所在编译目录下面的debug文件夹中生成了一个packagelib.lib(名字跟你的工程名称有关)的文件,这个就是我们需要的静态lib库。
4 . 我们希望通过我们的函数来调用这里的 hello(char *a) 函数,同样,我们新建一个自己的工程, 在设置Application options时, 不要选择Static library ,而是选择 Console application , 同样没有添加预编译的头文件。代码如下:
#include <stdio.h>
void hello(char *);
int main(){
hello("i am CAP !\n");
return 0;
}
5. 这里需要注意的一点就是, 需要在main()的上面写上lib库中的函数声明, 告诉编译器其他地方有一个叫hello(char *);这样的函数, 这样是为了编译通过。在编译main.cpp之前,你需要设置一下工程的属性以便在链接的时候能够链接到静态库。 【右键工程】 ==> 【Property】 ==> 【Configuration Properties】 ==> 【Linker】 ==> 【Input】 ==> 【Additional Dependences】 选择刚刚生成的那个lib库所在的路径+lib库的文件名称+后缀名。
6. 这样就可以编译main.cpp 了, 执行后的效果就是 "I am CAP ! " 。