【发布时间】:2011-05-03 13:16:01
【问题描述】:
我想知道如何在 C++ 中使用我创建的静态库,首先是 lib:
// header: foo.h
int foo(int a);
.
// code: foo.cpp
#include foo.h
int foo(int a)
{
return a+1;
}
那我先编译库:
- g++ foo.cpp
- ar rc libfoo.a foo.o
现在我想在一些文件中使用这些库,例如:
// prog.cpp
#include "foo.h"
int main()
{
int i = foo(2);
return i;
}
我现在必须如何编译这些? 我做了:
g++ -L. -lfoo prog.cpp
但由于找不到函数 foo 而出现错误
【问题讨论】:
-
g++ -L. -lfoo prog.cpp libfoo.aIIRC -
@sehe: -lfoo 大致相当于在命令行上列出 libfoo.a。不过顺序确实很重要。