Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined refences when compiling example from the documentation #828

Closed
ldpl opened this issue May 7, 2024 · 10 comments
Closed

Undefined refences when compiling example from the documentation #828

ldpl opened this issue May 7, 2024 · 10 comments

Comments

@ldpl
Copy link

ldpl commented May 7, 2024

I've build and installed version 7.9.0 and trying to compile "quick example" from https://pqxx.org/libpqxx/ with
g++ -lpq -lpqxx example.cpp gives a bunch of errors. Adding -std flags to the example or libpqxx compile doesn't help.

/usr/bin/ld: /tmp/ccW2W26H.o: in function `pqxx::internal::(anonymous namespace)::throw_for_encoding_error(char const*, char const*, unsigned long, unsigned long)':
example.cpp:(.text+0x1fd): undefined reference to `pqxx::argument_error::argument_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `main':
example.cpp:(.text+0x462): undefined reference to `pqxx::transaction_base::commit()'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `__static_initialization_and_destruction_0(int, int)':
example.cpp:(.text+0x600): undefined reference to `pqxx::internal::demangle_type_name[abi:cxx11](char const*)'
/usr/bin/ld: example.cpp:(.text+0x647): undefined reference to `pqxx::internal::demangle_type_name[abi:cxx11](char const*)'
/usr/bin/ld: example.cpp:(.text+0x68e): undefined reference to `pqxx::internal::demangle_type_name[abi:cxx11](char const*)'
/usr/bin/ld: example.cpp:(.text+0x6d5): undefined reference to `pqxx::internal::demangle_type_name[abi:cxx11](char const*)'
/usr/bin/ld: example.cpp:(.text+0x71c): undefined reference to `pqxx::internal::demangle_type_name[abi:cxx11](char const*)'
/usr/bin/ld: /tmp/ccW2W26H.o:example.cpp:(.text+0x763): more undefined references to `pqxx::internal::demangle_type_name[abi:cxx11](char const*)' follow
/usr/bin/ld: /tmp/ccW2W26H.o: in function `pqxx::check_version()':
example.cpp:(.text._ZN4pqxx13check_versionEv[_ZN4pqxx13check_versionEv]+0x31): undefined reference to `pqxx::internal::check_pqxx_version_7_9()'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `pqxx::string_traits<char const*>::into_buf(char*, char*, char const* const&)':
example.cpp:(.text._ZN4pqxx13string_traitsIPKcE8into_bufEPcS4_RKS2_[_ZN4pqxx13string_traitsIPKcE8into_bufEPcS4_RKS2_]+0xaa): undefined reference to `pqxx::conversion_overrun::conversion_overrun(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `pqxx::connection::connection(char const*)':
example.cpp:(.text._ZN4pqxx10connectionC2EPKc[_ZN4pqxx10connectionC5EPKc]+0x6b): undefined reference to `pqxx::connection::init(char const*)'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `pqxx::connection::~connection()':
example.cpp:(.text._ZN4pqxx10connectionD2Ev[_ZN4pqxx10connectionD5Ev]+0x18): undefined reference to `pqxx::connection::close()'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `pqxx::field::view() const &':
example.cpp:(.text._ZNKR4pqxx5field4viewEv[_ZNKR4pqxx5field4viewEv]+0x28): undefined reference to `pqxx::field::size() const'
/usr/bin/ld: example.cpp:(.text._ZNKR4pqxx5field4viewEv[_ZNKR4pqxx5field4viewEv]+0x37): undefined reference to `pqxx::field::c_str() const &'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `pqxx::transaction_base::exec1(pqxx::zview)':
example.cpp:(.text._ZN4pqxx16transaction_base5exec1ENS_5zviewE[_ZN4pqxx16transaction_base5exec1ENS_5zviewE]+0x5d): undefined reference to `pqxx::result::front() const'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `pqxx::transaction_base::exec_n(int, pqxx::zview)':
example.cpp:(.text._ZN4pqxx16transaction_base6exec_nEiNS_5zviewE[_ZN4pqxx16transaction_base6exec_nEiNS_5zviewE]+0x63): undefined reference to `pqxx::transaction_base::exec_n(int, pqxx::zview, std::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > pqxx::internal::state_buffer_overrun<long, unsigned long>(long, unsigned long)':
example.cpp:(.text._ZN4pqxx8internal20state_buffer_overrunIlmEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_T0_[_ZN4pqxx8internal20state_buffer_overrunIlmEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_T0_]+0x3d): undefined reference to `pqxx::internal::state_buffer_overrun[abi:cxx11](int, int)'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `pqxx::transaction<(pqxx::isolation_level)0, (pqxx::write_policy)1>::transaction(pqxx::connection&)':
example.cpp:(.text._ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_12write_policyE1EEC2ERNS_10connectionE[_ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_12write_policyE1EEC5ERNS_10connectionE]+0x31): undefined reference to `pqxx::internal::basic_transaction::basic_transaction(pqxx::connection&, pqxx::zview)'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `pqxx::transaction<(pqxx::isolation_level)0, (pqxx::write_policy)1>::~transaction()':
example.cpp:(.text._ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_12write_policyE1EED2Ev[_ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_12write_policyE1EED5Ev]+0x26): undefined reference to `pqxx::transaction_base::close()'
/usr/bin/ld: example.cpp:(.text._ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_12write_policyE1EED2Ev[_ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_12write_policyE1EED5Ev]+0x32): undefined reference to `pqxx::internal::basic_transaction::~basic_transaction()'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `int pqxx::transaction_base::query_value<int>(pqxx::zview)':
example.cpp:(.text._ZN4pqxx16transaction_base11query_valueIiEET_NS_5zviewE[_ZN4pqxx16transaction_base11query_valueIiEET_NS_5zviewE]+0xca): undefined reference to `pqxx::usage_error::usage_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: example.cpp:(.text._ZN4pqxx16transaction_base11query_valueIiEET_NS_5zviewE[_ZN4pqxx16transaction_base11query_valueIiEET_NS_5zviewE]+0x10d): undefined reference to `pqxx::row::operator[](int) const'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > pqxx::to_string<int>(int const&)':
example.cpp:(.text._ZN4pqxx9to_stringIiEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_[_ZN4pqxx9to_stringIiEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_]+0xa8): undefined reference to `pqxx::conversion_error::conversion_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: example.cpp:(.text._ZN4pqxx9to_stringIiEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_[_ZN4pqxx9to_stringIiEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_]+0x164): undefined reference to `pqxx::internal::integral_traits<int>::into_buf(char*, char*, int const&)'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `void pqxx::internal::render_item<int>(int const&, char*&, char*)':
example.cpp:(.text._ZN4pqxx8internal11render_itemIiEEvRKT_RPcS5_[_ZN4pqxx8internal11render_itemIiEEvRKT_RPcS5_]+0x2e): undefined reference to `pqxx::internal::integral_traits<int>::into_buf(char*, char*, int const&)'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > pqxx::connection::quote<char*>(char* const&) const':
example.cpp:(.text._ZNK4pqxx10connection5quoteIPcEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_[_ZNK4pqxx10connection5quoteIPcEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_]+0x151): undefined reference to `pqxx::connection::esc_to_buf(std::basic_string_view<char, std::char_traits<char> >, char*) const'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `int pqxx::field::as<int>() const':
example.cpp:(.text._ZNK4pqxx5field2asIiEET_v[_ZNK4pqxx5field2asIiEET_v]+0x19): undefined reference to `pqxx::field::is_null() const'
/usr/bin/ld: example.cpp:(.text._ZNK4pqxx5field2asIiEET_v[_ZNK4pqxx5field2asIiEET_v]+0x2c): undefined reference to `pqxx::internal::throw_null_conversion(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > pqxx::to_string<char*>(char* const&)':
example.cpp:(.text._ZN4pqxx9to_stringIPcEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_[_ZN4pqxx9to_stringIPcEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_]+0xa8): undefined reference to `pqxx::conversion_error::conversion_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /tmp/ccW2W26H.o: in function `int pqxx::from_string<int>(std::basic_string_view<char, std::char_traits<char> >)':
example.cpp:(.text._ZN4pqxx11from_stringIiEET_St17basic_string_viewIcSt11char_traitsIcEE[_ZN4pqxx11from_stringIiEET_St17basic_string_viewIcSt11char_traitsIcEE]+0x2c): undefined reference to `pqxx::internal::integral_traits<int>::from_string(std::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: /tmp/ccW2W26H.o:(.data.rel.ro._ZTVN4pqxx11transactionILNS_15isolation_levelE0ELNS_12write_policyE1EEE[_ZTVN4pqxx11transactionILNS_15isolation_levelE0ELNS_12write_policyE1EEE]+0x20): undefined reference to `pqxx::internal::basic_transaction::do_commit()'
/usr/bin/ld: /tmp/ccW2W26H.o:(.data.rel.ro._ZTVN4pqxx11transactionILNS_15isolation_levelE0ELNS_12write_policyE1EEE[_ZTVN4pqxx11transactionILNS_15isolation_levelE0ELNS_12write_policyE1EEE]+0x28): undefined reference to `pqxx::transaction_base::do_abort()'
/usr/bin/ld: /tmp/ccW2W26H.o:(.data.rel.ro._ZTIN4pqxx11transactionILNS_15isolation_levelE0ELNS_12write_policyE1EEE[_ZTIN4pqxx11transactionILNS_15isolation_levelE0ELNS_12write_policyE1EEE]+0x10): undefined reference to `typeinfo for pqxx::internal::basic_transaction'
collect2: error: ld returned 1 exit status

@ldpl ldpl changed the title Undefined refences when compiling example frome the documentation Undefined refences when compiling example from the documentation May 7, 2024
@jtv
Copy link
Owner

jtv commented May 7, 2024

@ldpl what compile options did you use for compiling libpqxx?

@jtv
Copy link
Owner

jtv commented May 7, 2024

This appears to be a duplicate of #820 and #822, but we never got to the bottom of those. :-(

In this case though, your link options seem to be in the wrong order. You did -lpq -lpqxx but the proper order is -lpqxx -lpq.

@ldpl
Copy link
Author

ldpl commented May 8, 2024

I tried compiling with no options, tried with ./configure --disable-shared as in documentation, tried different combinations like ./configure --enable-shared --prefix=/usr CXXFLAGS="-std=c++17", nothing makes any difference.
Swapping -lpqxx and -lpq changes nothing as well.

@jtv
Copy link
Owner

jtv commented May 8, 2024

Thanks @ldpl, that tells us a few things about what the problem isn't. Unfortunately at this point we're left with a mystery, so we'll have to go through the possibilities.

Here's a very basic one: What happens if, on the command line where you compile & link your own application, you put the -lpqxx -lpq part after the name of your source file?

So: g++ example.cpp -lpq -lpqxx

@ldpl
Copy link
Author

ldpl commented May 8, 2024

So: g++ example.cpp -lpq -lpqxx

No idea why but that compiles just fine.

UPD. And so does g++ -fPIC -shared -lpq -lpqxx example.cpp
UPD2. Actually, g++ example.cpp -lpqxx -lpq compiles, not g++ example.cpp -lpq -lpqxx

@tt4g
Copy link
Contributor

tt4g commented May 8, 2024

This may be due to gcc's behavior.
I found a detailed explanation on Stack Overflow: https://stackoverflow.com/a/409402

@jtv
Copy link
Owner

jtv commented May 10, 2024

Exactly. Thanks @tt4g for looking that up.

The surprising part is that AFAIR the linking of libraries with -l used to be done in a separate stage, after any source files and object files named on the command line were linked.

@jtv
Copy link
Owner

jtv commented May 13, 2024

Well, then I think we know why this happens. I'll close this ticket, and add the note to the earlier instances of the same problem.

(Unfortunately I don't think I can prevent recurrences of this problem in the future, since the documentation already shows the correct order pretty clearly. Or did I miss a spot?)

@ldpl
Copy link
Author

ldpl commented May 14, 2024

What I don't understand here is why -l defaults to static linking for pqxx that depends on -l order. Or, at least, that's what I see happening here.

@jtv
Copy link
Owner

jtv commented May 14, 2024

It puzzles me too, @ldpl. AFAIK the convention has always been to write the linking command line with the object file(s) before the libraries, and perhaps the compiler's behaviour has changed for usage that does not follow that convention.

An optimisation perhaps... At least in libpqxx a lot of the build time is spent linking. And to make it worse it's a single-process step where it can be hard for build systems to find other useful work to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants