链接C ++库时的未定义引用

时间:2014-02-06 16:10:07

标签: c++ static linker

我编写了一个C ++库,当链接到库时,无法找到符号。这就是我所拥有的:

a.cpp:

void zak()
{
}

TEST.CPP:

extern void zak();

int main(int argc, const char ** argv)
{
    zak();
}

生成文件:

all:
    g++ -c -o a.o a.cpp
    ar r libzak.a a.o
    g++ -L. -lzak test.cpp -o test

以下是我的(Linux Mint 13)框中的内容:

g++ -c -o a.o a.cpp
ar r libzak.a a.o
g++ -L. -lzak test.cpp -o test
/tmp/ccC4cnLV.o: In function `main':
test.cpp:(.text+0x7): undefined reference to `zak()'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1

我确信我错过了一些明显的东西,但它是什么?

2 个答案:

答案 0 :(得分:5)

Link order matters.-lzak放在链接行{。}}之后。

答案 1 :(得分:0)

我认为-l适用于共享库(.so)。试试这个:g ++ libzak.a test.cpp -o test

相关问题