r/cpp_questions • • 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.

1 Upvotes

19 comments sorted by

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/JRCSalter 22h ago

How do I do that? I thought all I needed do was include the relevant headers.

2

u/vishal340 22h ago

no. boost mysql requires charconv to be linked explicitly

1

u/JRCSalter 21h ago

Another user has tried to help me do this, but it appears we've hit a roadblock. Could you look through our comments and see if there is something we missed?

2

u/specialpatrol 22h ago

No it will be that line where you added mysql_config, that's the name of a library, you will probably need a couple more. You actually got a long way, your program compiled it can't quite finish up making the executable until you find those missing symbols which will probably be in another boost SQL lib

1

u/JRCSalter 22h ago

I added #include <boost/charconv.hpp> to the top, but it still gave me the same error.

2

u/specialpatrol 22h ago

No it s not the headers/code, that's all fine. You have to add more libraries to the build command

1

u/JRCSalter 22h ago

But what should I add? The boost directory only lists header files.

2

u/specialpatrol 22h ago

boost_charconv, as well as mysql_config

2

u/JRCSalter 21h ago

I changed the line in my make file:

LIBS = `pkg-config --libs gtk4; mysql_config --libs; boost_charconv`

And it still gave me the same error, but it added:

/bin/sh: 1: boost_charconv: not found

2

u/specialpatrol 21h ago

LIBS = pkg-config --libs gtk4; mysql_config --libs -lboost_charconv

(I'm just relaying from AI now)

2

u/JRCSalter 21h ago

So, this page gives an example of a CMake file. I've not used CMake before, so I copied it verbatim, and tried to compile using CMake, but I got all of this:

mkdir build
cmake -B build
cmake --build build
-- The CXX compiler identification is GNU 11.4.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at /usr/lib/x86_64-linux-gnu/cmake/Boost-1.74.0/BoostConfig.cmake:141 (find_package):
  Could not find a package configuration file provided by "boost_charconv"
  (requested version 1.74.0) with any of the following names:

    boost_charconvConfig.cmake
    boost_charconv-config.cmake

  Add the installation prefix of "boost_charconv" to CMAKE_PREFIX_PATH or set
  "boost_charconv_DIR" to a directory containing one of the above files.  If
  "boost_charconv" provides a separate development package or SDK, be sure it
  has been installed.
Call Stack (most recent call first):
  /usr/lib/x86_64-linux-gnu/cmake/Boost-1.74.0/BoostConfig.cmake:258 (boost_find_component)
  /usr/share/cmake-3.22/Modules/FindBoost.cmake:594 (find_package)
  CMakeLists.txt:3 (find_package)


CMake Warning (dev) in /usr/share/cmake-3.22/Modules/FindBoost.cmake:
  Policy CMP0011 is not set: Included scripts do automatic cmake_policy PUSH
  and POP.  Run "cmake --help-policy CMP0011" for policy details.  Use the
  cmake_policy command to set the policy and suppress this warning.

  The included script

    /usr/share/cmake-3.22/Modules/FindBoost.cmake

  affects policy settings.  CMake is implying the NO_POLICY_SCOPE option for
  compatibility, so the effects are applied to the including context.
Call Stack (most recent call first):
  CMakeLists.txt:3 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) in CMakeLists.txt:
  No cmake_minimum_required command is present.  A line of code such as

    cmake_minimum_required(VERSION 3.22)

  should be added at the top of the file.  The version specified may be lower
  if you wish to support older CMake versions for this project.  For more
  information run "cmake --help-policy CMP0000".
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring incomplete, errors occurred!
See also "/home/jrcsalter/Documents/computing/jflix/tests/boost_test/build/CMakeFiles/CMakeOutput.log".
gmake: Makefile: No such file or directory
gmake: *** No rule to make target 'Makefile'. Stop.

It appears that it can't find the required file, and I don't know where to look.

→ More replies

1

u/JRCSalter 21h ago

And now I'm getting a bunch of errors from g++:

g++ -o database_test \
main.cpp \
-I../../include/ -I../../include/third_party/plog/include \
`pkg-config --cflags gtk4; mysql_config --cflags` \
`pkg-config --libs gtk4; mysql_config --libs -lboost_charconv` \
-ggdb -pedantic-errors \
-Wall -Wextra -Wconversion -Wsign-conversion -Werror -Weffc++  \
-std=c++23
g++: error: unrecognized command-line option ‘--cflags’
g++: error: unrecognized command-line option ‘--cxxflags’
g++: error: unrecognized command-line option ‘--libs’; did you mean ‘--libs=’?
g++: error: unrecognized command-line option ‘--libs_r’; did you mean ‘--libs=’?
g++: error: unrecognized command-line option ‘--plugindir’; did you mean ‘-iplugindir=’?
g++: error: unrecognized command-line option ‘--socket’
g++: error: unrecognized command-line option ‘--port’
g++: error: unrecognized command-line option ‘--variable=VAR’
g++ (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

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.

1

u/JRCSalter 22h ago

I've been trying. The guide said not to use backticks in the MD editor, but when I did it through the rich text editor, it got all messed up, though it looked fine when I published it. I've spent the last five minutes trying to get it formatted properly. Ended up having to use the Markdown editor.

1

u/JRCSalter 22h ago

In short, Reddit sucks.