How to Connect C++ to MySQL on CentOS and Fix Common Build Errors
This guide walks through the typical problems faced when linking C++ applications to MySQL on CentOS—missing Boost headers and undefined driver references—and provides step‑by‑step solutions, including installing Boost, adjusting compile commands, and a working connection code sample.
Problem Overview
When building a C++ program that uses MySQL Connector/C++ on a fresh CentOS system, two common issues arise:
The Boost header files are missing from /usr/include.
The linker reports undefined reference to `get_driver_instance' because the MySQL driver library is not linked.
Installing Boost Headers
The latest MySQL Connector/C++ package depends on Boost. CentOS does not ship Boost headers by default. To satisfy the dependency:
Download the Boost source archive from https://www.boost.org/.
Extract the archive, then copy the top‑level boost directory into the system include path:
tar -xf boost_*.tar.gz
sudo cp -r boost_*/boost /usr/include/After copying, the compiler can locate Boost headers such as boost/system.hpp.
Compiling and Linking with MySQL Connector/C++
The undefined reference error occurs because the MySQL Connector/C++ library (and its Boost dependency) are not linked. Use the following compilation command, adjusting library paths if necessary:
g++ -std=c++11 my_program.cpp \
-I/usr/include \
-L/usr/lib64 \
-lmysqlcppconn -lboost_system \
-o my_programThis command:
Specifies C++11 standard.
Adds /usr/include to the header search path.
Links against libmysqlcppconn.so and libboost_system.so located in /usr/lib64.
Sample C++ Code Using MySQL Connector/C++
The following minimal example demonstrates how to obtain a driver instance, create a connection, execute a simple query, and process the result set.
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/statement.h>
#include <cppconn/resultset.h>
#include <iostream>
int main() {
try {
// Obtain the driver instance
sql::Driver *driver = get_driver_instance();
// Create a connection (replace user/password/db as needed)
std::unique_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3306", "user", "password"));
con->setSchema("testdb");
// Create a statement and execute a query
std::unique_ptr<sql::Statement> stmt(con->createStatement());
std::unique_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT id, name FROM sample_table"));
// Iterate over the result set
while (res->next()) {
std::cout << "id=" << res->getInt("id") << ", name=" << res->getString("name") << std::endl;
}
} catch (sql::SQLException &e) {
std::cerr << "Error: " << e.what() << " (MySQL error code: " << e.getErrorCode() << ")" << std::endl;
return 1;
}
return 0;
}Compile the program with the command shown earlier, and the resulting executable will connect to MySQL and print the query results.
Summary
Installing Boost headers manually and linking the MySQL Connector/C++ library (plus its Boost System dependency) resolves the build failures on CentOS. The provided compilation line and example code give a concrete reference for setting up a C++ MySQL client environment.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
