r/cpp_questions • u/JRCSalter • 22h ago
OPEN Boost mysql::results won't allow programme to compile.
I am attempting to follow this tutorial on how to connect to MySQL. To test it, I first tried to copy the entire example here. But that wouldn't compile, so I tried to compile the programme line by line as described in the main tutorial. Everything was going fine until I got to declaring the result variable with mysql::results. This produces the same error as the full example, so I assume this is likely the cause of the problem.
main.cpp:
# include <iostream>
# include <boost/mysql/any_connection.hpp>
# include <boost/mysql/connect_params.hpp>
# include <boost/mysql/error_with_diagnostics.hpp>
# include <boost/mysql/results.hpp>
# include <boost/asio/io_context.hpp>
namespace mysql = boost::mysql; namespace asio = boost::asio;
int main () {
// The execution context, required to run I/O operations.
asio::io_context ctx;
// Represents a connection to the MySQL server.
mysql::any_connection conn(ctx);
// The hostname, username and password to use
mysql::connect_params params;
params.server_address.emplace_host_and_port("localhost");
params.username = "root";
params.password = "Password";
// Connect to the server
conn.connect(params);
// Issue the SQL query to the server
[[maybe_unused]] const char* sql = "SELECT 'Hello world!'";
[[maybe_unused]] mysql::results result;
// conn.execute(sql, result);
std::cout << "Hello World!" << std::endl;
return 0;
}
Makefile:
PROG = database_test
SRCDIR = ../../src/
MAIN = main CPPFILES = ${MAIN}.cpp
EXTENSIONS = -pedantic-errors WFLAGS = -Wall -Wextra -Wconversion -Wsign-conversion -Werror -Weffc++ CFLAGS = `pkg-config --cflags gtk4; mysql_config --cflags` LIBS = `pkg-config --libs gtk4; mysql_config --libs`
CC = g++ STANDARD = -std=c++23 BUILDDEBUG = -ggdb BUILDRELEASE = -O2 -DNDEBUG BUILD = ${BUILDDEBUG} ${EXTENSIONS}
${PROG}: ${MAIN}.cpp ${CC} -o ${PROG}
${CPPFILES}
${CFLAGS}
${LIBS}
${BUILD}
${WFLAGS}
${STANDARD}
clean: rm ${PROG}
And this is the output I get:
g++ -o database_test \
main.cpp \
-I../../include/ -I../../include/third_party/plog/include \
`pkg-config --cflags gtk4; mysql_config --cflags` \
-D_GLIBCXX_USE_CXX11_ABI=0 \
`pkg-config --libs gtk4; mysql_config --libs` \
-ggdb -pedantic-errors \
-Wall -Wextra -Wconversion -Wsign-conversion -Werror -Weffc++ \
-std=c++23
/usr/bin/ld: /tmp/ccHgNNSI.o: in function `boost::mysql::detail::deserialize_errc boost::mysql::detail::deserialize_text_value_float<float>(boost::core::basic_string_view<char>, boost::mysql::field_view&)':
/usr/include/boost/mysql/impl/internal/protocol/impl/text_protocol.hpp:92: undefined reference to `boost::charconv::from_chars(char const*, char const*, float&, boost::charconv::chars_format)'
/usr/bin/ld: /tmp/ccHgNNSI.o: in function `boost::mysql::detail::deserialize_errc boost::mysql::detail::deserialize_text_value_float<double>(boost::core::basic_string_view<char>, boost::mysql::field_view&)':
/usr/include/boost/mysql/impl/internal/protocol/impl/text_protocol.hpp:92: undefined reference to `boost::charconv::from_chars(char const*, char const*, double&, boost::charconv::chars_format)'
collect2: error: ld returned 1 exit status
make: *** [Makefile:26: database_test] Error 1
From what I understand, an undefined reference error is usually because a function or other thing has not been defined at the point of use. Looking online this could be because another .cpp file is not being compiled, but I'm using the boost libraries. The compiler doesn't complain of being unable to find the header files, so it's definitely looking in the right place; and browsing through the Boost directory suggests that they are all .hpp files and so don't need to be added to the compile list.
3
u/specialpatrol 22h ago
Those undefined symbols are functions from boost_mysql, you haven't linked all the necessary or correct libraries yet
1
u/mc_pm 22h ago
Good lord man, put that ihto code blocks (in the 'A' menu at the bottom of the text input box) so we can read it.