使用C++ Connector访问MySQL数据库:如何添加标准搜索路径
Hey there! Let's walk through this step by step—totally get how confusing compiler flags can feel when you're just starting out with C++ and MySQL. Let's break down what -I and -L do, then give you concrete examples tailored to your Cygwin setup.
First: Understand the Flags
Let's get clear on what those flags actually do, since that'll help you adapt this to your specific paths:
-I/path/to/headers: Tells g++ where to look for header files (likeconnection.h). This is exactly what you need to fix the "not found" error.-L/path/to/libs: Tells g++ where to find the compiled library files (the.soor.afiles that let your code talk to MySQL).-lmysqlcppconn: Tells g++ to link against the MySQL Connector/C++ library (note we drop thelibprefix and.so/.asuffix here).
Step 1: Find Your Library Paths
First, locate where you extracted/installed MySQL Connector/C++. Let's assume you have it in a folder like ~/mysql-connector-c++ (your home directory in Cygwin) or C:\mysql-connector-c++ (which translates to /cygdrive/c/mysql-connector-c++ in Cygwin).
Inside that folder, you'll find:
- A
includedirectory (holds header files likecppconn/connection.h) - A
liborlib64directory (holds the library files likelibmysqlcppconn.so)
Step 2: Compilation Command Example
Let's say your code file is named db_test.cpp, and your Connector paths are:
- Headers:
/home/your-username/mysql-connector-c++/include - Libraries:
/home/your-username/mysql-connector-c++/lib64
Your g++ command would look like this:
g++ db_test.cpp -o db_test -I/home/your-username/mysql-connector-c++/include -L/home/your-username/mysql-connector-c++/lib64 -lmysqlcppconn
If you're using Boost (since you mentioned downloading it), add its header path with another -I flag:
g++ db_test.cpp -o db_test -I/home/your-username/boost_1_83_0 -I/home/your-username/mysql-connector-c++/include -L/home/your-username/mysql-connector-c++/lib64 -lmysqlcppconn
Quick Notes for Your Setup:
- Replace
your-usernamewith your actual Cygwin username, or adjust the path to match where you stored the Connector/Boost. - Make sure your code uses the correct include path for
connection.h: it should be#include <cppconn/connection.h>(not just<connection.h>) because the header lives inside thecppconnsubfolder of theincludedirectory.
Step 3: Running Your Program (Dynamic Library Note)
If you're using the dynamic library (.so file), Cygwin might not find it when you try to run your program. Fix this by adding the library path to your LD_LIBRARY_PATH environment variable first:
export LD_LIBRARY_PATH=/home/your-username/mysql-connector-c++/lib64:$LD_LIBRARY_PATH
Then run your program:
./db_test
Test Code to Verify
Here's a simple snippet you can use to test your connection (replace the database credentials with your own):
#include <iostream> #include <cppconn/connection.h> #include <cppconn/driver.h> int main() { sql::Driver* driver; sql::Connection* con; try { // Get the MySQL driver instance driver = get_driver_instance(); // Connect to your local database (adjust username, password, DB name!) con = driver->connect("tcp://127.0.0.1:3306", "your-db-username", "your-db-password"); con->setSchema("your-database-name"); std::cout << "Successfully connected to MySQL! 🎉" << std::endl; // Clean up delete con; } catch (sql::SQLException& e) { std::cout << "SQL Error: " << e.what() << std::endl; std::cout << "Error Code: " << e.getErrorCode() << std::endl; return 1; } return 0; }
If everything works, you'll see the success message—if not, double-check your paths, credentials, and that MySQL is running locally.
内容的提问来源于stack exchange,提问作者Min Joon So




