Reputation: 67
I've been trying to connect to a MySQL database using C++ and specifically using the example from here:
https://dev.mysql.com/doc/connector-cpp/1.1/en/connector-cpp-examples-complete-example-1.html
and getting the error Undefined symbols for architecture x86_64 (abbreviated output at bottom of post or full output here: https://gist.github.com/plisken1/2de09557954b16c5a86348177a0bcff8)
I'm running; MacOS 10.14.6
CLion Version: 2020.01.2
I've Installed the MySQL C++ Connector Version 8.0.18 from here: https://dev.mysql.com/downloads/connector/cpp/
I've also installed boost version 1.72 via Home-brew from here: https://formulae.brew.sh/formula/boost
The example code from the link above is in a file called main.cpp and the source is as below;
/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>
/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next()) {
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
/* Access column data by numeric offset, 1 is the first column */
cout << res->getString(1) << endl;
}
delete res;
delete stmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout << endl;
return EXIT_SUCCESS;
}
Naturally I've not gotten as far as entering my own database details.
My CMakeLists.txt file looks like;
cmake_minimum_required(VERSION 3.16)
project(mySQL_Example)
set(CMAKE_CXX_STANDARD 11)
#For mysql connector include..
include_directories(/usr/local/mysql-connector-c++-8.0.18/include/jdbc/)
#For Boost..
include_directories(/usr/local/Cellar/boost/1.72.0_3/include/)
add_executable(mySQL_Example main.cpp)
#For imported linking..
add_library(libmysqlcppconn STATIC IMPORTED)
set_property(TARGET libmysqlcppconn PROPERTY IMPORTED_LOCATION /usr/local/mysql-connector-c++-8.0.18/lib64/libmysqlcppconn-static.a)
target_link_libraries (mySQL_Example libmysqlcppconn)
I believe my paths in the above are all correct.
and lastly the output error looks like;
====================[ Build | mySQL_Example | Debug ]===========================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build "/Volumes/D_SLAVE/My Documents/My Projects/CLion/mySQL_Example/cmake-build-debug" --target mySQL_Example -- -j 12
[ 50%] Linking CXX executable mySQL_Example
Undefined symbols for architecture x86_64:
"_BIO_free", referenced from:
sha256_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
sha256_password_auth_client_nonblocking(MYSQL_PLUGIN_VIO*, MYSQL*, int*) in libmysqlcppconn-static.a(client_authentication.cc.o)
caching_sha2_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
caching_sha2_password_auth_client_nonblocking(MYSQL_PLUGIN_VIO*, MYSQL*, int*) in libmysqlcppconn-static.a(client_authentication.cc.o)
################################################
MANY SIMILAR LINES REMOVED TO KEEP TO POST CHAR LIMIT FULL OUTPUT HERE: https://gist.github.com/plisken1/2de09557954b16c5a86348177a0bcff8
################################################
"_X509_check_host", referenced from:
ssl_verify_server_cert(Vio*, char const*, char const**) in libmysqlcppconn-static.a(client.cc.o)
"_X509_check_ip_asc", referenced from:
ssl_verify_server_cert(Vio*, char const*, char const**) in libmysqlcppconn-static.a(client.cc.o)
"_X509_free", referenced from:
ssl_verify_server_cert(Vio*, char const*, char const**) in libmysqlcppconn-static.a(client.cc.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [mySQL_Example] Error 1
make[2]: *** [CMakeFiles/mySQL_Example.dir/all] Error 2
make[1]: *** [CMakeFiles/mySQL_Example.dir/rule] Error 2
make: *** [mySQL_Example] Error 2
I'm guessing the big hint here is: Undefined symbols for architecture x86_64 but I've no clue how to resolve.
Any and all assistances would be greatly appreciated.
UPDATE:
I can now get the source to compile from the command-line with the following;
g++ -I /usr/local/Cellar/boost/1.72.0_3 main.cpp -I /usr/local/mysql-connector-c++-8.0.18/include/jdbc -I /usr/local/Cellar/boost/1.72.0_3/include/boost -o main.o -L /usr/local/mysql-connector-c++-8.0.18/lib64/ -l mysqlcppconn
but on running it, I get the following error;
dyld: Library not loaded: @rpath/libmysqlcppconn.7.dylib
Referenced from: /Volumes/D_SLAVE/SHARED/Debian/mySql_test/./main.o
Reason: image not found
Abort trap: 6
Progress but still not really close unfortunately.
Upvotes: 0
Views: 949
Reputation: 11
I spent hours myself trying to compile the basic tutorial on the MySQL C++ Connector website. Using your compile command I was able to get the same error as you. I found the following article addressing how to solve a dyld Library not loaded error:
https://appuals.com/how-to-fix-dyld-library-not-loaded-error-on-macos/
The problem is that the libraries are stored in /usr/local/mysql-connector-c++/lib64 but the program is not loading said libraries. I followed solution #1 in the link, which was to create symbolic links for the library that would not load. As one could expect, another abort trap error would happen each time with a new library to be added. Once I added all required libraries to /usr/lib, I was able to run my program and access my database.
Upvotes: 1