libpq 函数的未定义引用
我有两个项目,一个是一个使用 libpq 标头和可执行文件的库,它链接到我的库和 libpq,但无论我如何放入 makefile,它都不会链接 libpq 并且会返回一个未定义的参考错误。它可以链接我的库。
g++ -o MYAPP main.o -L../Libs -lpq -lMYLIB
libpq.a 和 libmylib.a 都在 Libs 目录中。
我使用 nm 来检查函数,它们在那里,我将 -lpq 更改为 -lpq_ 并且编译器抱怨缺少 lib,因此它正在查找并且理论上链接,但找不到引用?
这真的让我很烦恼,这可能是什么?
(我的英语不太好,抱歉)
谢谢,
乔纳森
I have two projects, one is a library that uses libpq headers and the executable, that links to my library and to libpq, but no matter how I put in the makefile, it just doesn't link the libpq and aways return me an undefined reference error. It links my lib ok.
g++ -o MYAPP main.o -L../Libs -lpq -lMYLIB
both libpq.a and libmylib.a are inside the Libs directory.
I used nm to check the functions and they are there, I changed the -lpq to -lpq_ and the compiler complains about the missing lib, so it is finding and theorically, linking, but can't find the references?
this is really bugging me out, what could it be?
(my english is not that good, sorry)
Thanks,
Jonathan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我快速检查了一些我所使用的 postgre 头文件,当使用 C++ 编译器编译时,它们似乎没有将自己包装在
extern "C"
中。您的问题是否可以通过将 postgre 标头的 include 语句包含在extern "C" { ... }
块中来解决?编辑。
我认为问题是这样的:
libpq.a 和 libmylib.a
。我一开始在你原来的帖子中忽略了这一点,对于噪音感到抱歉。当您将-lpq
提供给 gcc 时,它会尝试动态链接。 除非您使用 -L 指定库目录并且,否则仅存在库的静态版本。静态库无法记录它们需要引入哪些其他库。此外,对于静态库,规范顺序也很重要。依赖于其他库的库需要列在它们之前。通过 apt-get 安装,您安装了共享库,这些库正确记录了它们还需要链接的内容(检查 ldd /usr/lib/libpq.so )。因此,在您之前的尝试中,您可能只需要使用-lMYLIB
反转-lpq
并列出 libpq 需要的其他库。I quickly checked some of the postgre headers I have flying around and they seem to not wrap themselves up in
extern "C"
when compiled with a C++ compiler. Could it be possible that your problem is resolved by enclosing the include statements for postgre's headers within anextern "C" { ... }
block?Edit.
I think the problem is this:
libpq.a and libmylib.a
. I overlooked this in your original post at first, sorry for the noise. When you give-lpq
to gcc it tries to link dynamically. Unless you give a library directory with -L and there live only static versions of the libraries. Static libraries cannot document which other libraries they need to pull in. Also, with static libraries, specification order is important. Libraries that depend on other libraries need to be listed before them. By installing via apt-get you installed shared libraries that properly document what else they need to link (checkldd /usr/lib/libpq.so
). So, in your previous attempt, you probably just needed to reverse-lpq
with-lMYLIB
and list the other libraries that libpq needs.