开发者

Undefined reference for libpq functions

开发者 https://www.devze.com 2023-01-26 17:08 出处:网络
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 aw

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?

开发者_JAVA百科this is really bugging me out, what could it be?

(my english is not that good, sorry)

Thanks,

Jonathan


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 an extern "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 (check ldd /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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号