针对C 库链接时的未定义参考

undefined reference in linking against C++ library

本文关键字:未定义 参考 链接 针对      更新时间:2023-10-16

i编写一个C 库,在链接到库时,找不到其中的符号。这是我得到的:

A.CPP:

void zak()
{
}

test.cpp:

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

makefile:

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

我敢肯定我缺少一些明显的东西,但是这是什么?

链接顺序很重要。将-lzak放在test.cpp之后的链接线上。

我认为-l适用于共享库(.so)。尝试以下操作:g libzak.a test.cpp -o test