Errors when Linking mySQL C++ Connector to my Project (Windows) - c++

I'm trying to make a basic program to connect to my mySQL database in C++. I downloaded the mySQL C++ connector, added the path to the libraries with -L (Connector C++ 8.0\lib64), added the name of the library with -l (mysqlcppconn8-2-vs14), and added the include path (Connector C++ 8.0\include\jdbc) with -I. I wrote a few lines to connect to my database:
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace sql;
int main() {
try{
Driver* driver { get_driver_instance() };
Connection* con { driver->connect("tcp://localhost:3306", "root", "") };
Statement* statement { con->createStatement() };
ResultSet* res = statement->executeQuery("SELECT * FROM test.testdb");
}
catch(std::exception &e) {
std::cout << "ERROR" << std::endl;
}
}
But when I compile the program, I get a load of errors consistent with me not linking libraries correctly:
C:/Program Files/MySQL/Connector C++ 8.0/include/jdbc/cppconn/driver.h:77: undefined reference to `check(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
C:/Program Files/MySQL/Connector C++ 8.0/include/jdbc/cppconn/driver.h:78: undefined reference to `check(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > const&)'
src\main.o: In function `get_driver_instance_by_name':
C:/Program Files/MySQL/Connector C++ 8.0/include/jdbc/cppconn/driver.h:90: undefined reference to `_get_driver_instance_by_name'
src\main.o: In function `get_driver_instance_by_name':
C:/Program Files/MySQL/Connector C++ 8.0/include/jdbc/mysql_driver.h:106: undefined reference to `sql::mysql::_get_driver_instance_by_name(char const*)'
collect2.exe: error: ld returned 1 exit status
13:53:03 Build Failed. 4 errors, 15 warnings. (took 839ms)
Am I forgetting to include a library? I'm on Windows using MinGW with Eclipse.

Related

getting "undefined reference to `check' "error on Windows

I try to connect a DB to C++ with the Connector C++ 8.0.
When I compile a really simple code :
#include <iostream>
#include <jdbc.h>
int main(){
sql::Driver *driver;
driver = get_driver_instance();
return 0;
}
I get this error:
c:/program files (x86)/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\newva\AppData\Local\Temp\ccjFB5x8.o:Main.cpp:(.text+0x1f): undefined reference to `check(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
c:/program files (x86)/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\newva\AppData\Local\Temp\ccjFB5x8.o:Main.cpp:(.text+0x53): undefined reference to `check(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >
const&)'
collect2.exe: error: ld returned 1 exit status
When I comment the "driver = get_driver_instance();" line, the code compile.
I compile with a Makefile :
MYSQL_CONCPP_DIR = "C:\Program Files\MySQL\Connector C++ 8.0"
CPPFLAGS=-I $(MYSQL_CONCPP_DIR)\include\mysql -L $(MYSQL_CONCPP_DIR)\lib64\vs14
LDLIBS=-lmysqlcppconn
CXXFLAGS=-std=c++20
result.exe:Main.cpp
g++ Main.cpp ${CXXFLAGS} ${CPPFLAGS} ${LDLIBS} -o result.exe
I am on Windows 10.
Connector C++ has 2 APIs.
JDBC (the one you are using)
X DevAPI
You are linking with mysqlcppconn8 which is DevAPI. You should link with mysqlcppconn.
Both are bundled on the same package.

undefined reference to `check(std::string const&)' while linking

I have some "simple" code in C++ to connect to a MySQL database (which is localhost, btw).
#include <stdlib.h>
#include <iostream>
#include "mysql/include/mysql/jdbc.h"
using namespace sql;
int main() {
std::cout << "Test"<< std::endl;
try {
sql::Driver *myDriver;
sql::Connection *myConn;
sql::Statement *myStmt;
sql::ResultSet *myRes;
myDriver = sql::mysql::get_mysql_driver_instance();
myConn = myDriver ->connect("tcp://127.0.0.1", "root", "");
myConn->setSchema("root");
myStmt = myConn->createStatement();
myRes = myStmt->executeQuery("SELECT 'Hello World' AS _message");
while (myRes->next()) {
std::cout << myRes->getString("_message") << std::endl;
}
delete myRes;
delete myStmt;
delete myConn;
} catch (sql::SQLException &e) {
std::cout << "Filed connect to Database" << std::endl;
std::cout << "Error: " << e.what() << std::endl;
std::cout << "Error code: " << e.getErrorCode() << std::endl;
}
}
The file jdbc.h is just a lot of different #include for header files.
I use 2 commands to compile:
g++ -c -Wall -I/usr/include/cppconn connect.cpp -o connect.o and
g++ -lm -L/usr/lib/x86_64-linux-gnu -lmysqlcppconn connect.o -o connect
Which produced this linking error:
/usr/bin/ld: connect.o: in function "check_lib()':
connect.cpp:(.text+0x2c): undefined reference to `check(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)"
/usr/bin/ld: connect.cpp:(.text+0x77): undefined reference to "check(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > const&)"
/usr/bin/ld: connect.o: in function `sql::mysql::get_driver_instance_by_name(char const*)':
connect.cpp:(.text+0xfa): undefined reference to "sql::mysql::_get_driver_instance_by_name(char const*)"
collect2: error: ld returned 1 exit status
Which I kinda resolved using this answer: https://stackoverflow.com/a/33395489/14814564 , essentially by putting #define _GLIBCXX_USE_CXX11_ABI 0 at the top of the file, which after compilation, produces this linking error:
/usr/bin/ld: connect.o: in function "check_lib()':
connect.cpp:(.text+0x2c): undefined reference to "check(std::string const&)'
/usr/bin/ld: connect.cpp:(.text+0x77): undefined reference to "check(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&)'
/usr/bin/ld: connect.o: in function `sql::mysql::get_driver_instance_by_name(char const*)':
connect.cpp:(.text+0xfa): undefined reference to "sql::mysql::_get_driver_instance_by_name(char const*)'
collect2: error: ld returned 1 exit status
Why does this linking error keep popping up and how do I resolve it?
I had the same problem, I solved it by building a Makefile`
MYSQL_CONCPP_DIR = ./mysql
CPPFLAGS=-I $(MYSQL_CONCPP_DIR)/include/mysql -L $(MYSQL_CONCPP_DIR)/lib64
LDLIBS=-lmysqlcppconn
CXXFLAGS=-std=c++11
out.exe:test.cpp
g++ test.cpp ${LDLIBS} ${CXXFLAGS} ${CPPFLAGS} -o out.exe
You have there
#include "mysql/include/mysql/jdbc.h"
In my case include needs to be done like this`
#include <jdbc.h>

googlemock giving error while building a program with mock

I have some experience in C and C++. But I am new to googletest and googlemock.
I am trying unit testing for C++ Programs.
I did not get any error while I was working on googletest. But, when I started working on googlemock, I got problems.
I have a simple code for working with googlemock, and when I tried to build it with the command
g++ mock.cpp -lgtest -lgtest_main -lgmock -pthread
I get this error which I am not able to understand. (I have attached both the program and the error which I got).
Please help me to understand it and overcome.
Thanks in advance.
mock.cpp
#include<iostream>
#include<vector>
#include<gtest/gtest.h>
#include<gmock/gmock.h>
using namespace std;
using ::testing::AtLeast;
using ::testing::Return;
using ::testing::_;
class DataBaseConnect{
public:
virtual bool login(string username, string password){ return true; }
virtual bool logout(string username){ return true; }
virtual int fetchRecord(){ return -1; }
};
class MockDB : public DataBaseConnect{
public:
MOCK_METHOD0(fetchRecord, int());
MOCK_METHOD1(logout, bool(string username));
MOCK_METHOD2(login, bool(string username, string password));
};
class MyDatabase{
DataBaseConnect & dbC;
public:
MyDatabase(DataBaseConnect & _dbC) : dbC(_dbC) {}
int Init(string username, string password){
if(dbC.login(username, password) != true){
cout<<"DB FAILURE\n";
return -1;
}
else{
cout<<"DB SUCCESS\n";
return 1;
}
}
};
TEST(MyDBTest, LoginTest){
//Arrange
MockDB mdb;
MyDatabase db(mdb);
EXPECT_CALL(mdb, login(_, _))
.Times(1)
.WillOnce(Return(true));
//Act
int retVal = db.Init("Terminator", "I'm Back");
//Assert
EXPECT_EQ(retVal, 1);
}
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
command:
g++ mock.cpp -lgtest -lgmock -lgtest_main -pthread
error:
g++ mock.cpp -lgtest -lgmock -lgtest_main -pthread
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::linked_ptr_internal::join(testing::internal::linked_ptr_internal const*)':
mock.cpp:(.text._ZN7testing8internal19linked_ptr_internal4joinEPKS1_[_ZN7testing8internal19linked_ptr_internal4joinEPKS1_]+0x2a): undefined reference to `testing::internal::g_linked_ptr_mutex'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::linked_ptr_internal::depart()':
mock.cpp:(.text._ZN7testing8internal19linked_ptr_internal6departEv[_ZN7testing8internal19linked_ptr_internal6departEv]+0x27): undefined reference to `testing::internal::g_linked_ptr_mutex'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::FunctionMockerBase<int ()>::InvokeWith(std::tuple<> const&)':
mock.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFivEE10InvokeWithERKSt5tupleIJEE[_ZN7testing8internal18FunctionMockerBaseIFivEE10InvokeWithERKSt5tupleIJEE]+0x33): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::FunctionMockerBase<bool (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)>::InvokeWith(std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)':
mock.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE10InvokeWithERKSt5tupleIJS7_EE[_ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE10InvokeWithERKSt5tupleIJS7_EE]+0x33): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::FunctionMockerBase<bool (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)>::InvokeWith(std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)':
mock.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EE10InvokeWithERKSt5tupleIJS7_S7_EE[_ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EE10InvokeWithERKSt5tupleIJS7_S7_EE]+0x33): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::FunctionMockerBase<bool (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)>::AddNewExpectation(char const*, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::tuple<testing::Matcher<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, testing::Matcher<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)':
mock.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EE17AddNewExpectationEPKciRKS7_RKSt5tupleIJNS_7MatcherIS7_EESG_EE[_ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EE17AddNewExpectationEPKciRKS7_RKSt5tupleIJNS_7MatcherIS7_EESG_EE]+0xda): undefined reference to `testing::Expectation::Expectation(testing::internal::linked_ptr<testing::internal::ExpectationBase> const&)'
collect2: error: ld returned 1 exit status
Seems the tutorial you followed have forgotten to install gmock.so, so after building finished, you need to type sudo make install to install all the libraries file.
And the version of gtest you used is out of date, suggest to use this version:
https://github.com/google/googletest/archive/refs/tags/release-1.11.0.tar.gz
Other answers around your tutorial have also pointed out that, we need to install gmock libraries. Then the problem should be fixed.

Using mysql connector-c++ to connect mysql server ,a little demo,but stucked

My server is Ubuntu,code is:
#include <mysqlx/xdevapi.h>
#include <iostream>
using namespace mysqlx;
int main()
{
const char *url = "mysqlx://root#127.0.0.1";
std::cout << "Create session on " << url <<std::endl;
Session sess(url);
Schema sch = sess.getSchema("IM.User");
}
when I compile it,got this:
g++ dbtest.cpp -g -std=c++11 -L/usr/lib64/ -lmysqlcppconn8-static -I/usr/include/mysqlx -lssl -lpthread -lcrypto -o dbtest.o
----------------------------------------------------------------------
/tmp/cccFQIzq.o: In function `mysqlx::string::string(char const*)':
/usr/include/mysqlx/devapi/common.h:100: undefined reference to `mysqlx::string::Impl::from_utf8(mysqlx::string&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/cccFQIzq.o: In function `mysqlx::string::operator std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >() const':
/usr/include/mysqlx/devapi/common.h:115: undefined reference to `mysqlx::string::Impl::to_utf8[abi:cxx11](mysqlx::string const&)'
/tmp/cccFQIzq.o: In function `mysqlx::SessionSettings::SessionSettings(mysqlx::string const&)':
/usr/include/mysqlx/devapi/settings.h:339: undefined reference to `mysqlx::common::Settings_impl::set_from_uri(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
Am I lost something?

Boost linking fail

I have the following code which i m trying to compile:
#include <boost/filesystem/convenience.hpp>
#include <boost/foreach.hpp>
#include <boost/range.hpp>
#include <iostream>
int main(int, char**)
{
namespace bf = boost::filesystem;
BOOST_FOREACH(bf::path path,
boost::make_iterator_range(
bf::recursive_directory_iterator(bf::path("/home")),
bf::recursive_directory_iterator())) {
std::cout << path.string() << std::endl;
}
return 0;
}
My boost library is in /home/foo/include . and the include files are actually there.
when i run the following:
g++ -I/home/foo/include/ test.cc
I get the following error. how can i resolve this. what should i follow?
/tmp/ccvDmFNL.o(.text+0x502): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `boost::system::get_system_category()'
/tmp/ccvDmFNL.o(.text+0x51b): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `boost::system::get_generic_category()'
/tmp/ccvDmFNL.o(.text+0x534): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `boost::system::get_generic_category()'
/tmp/ccvDmFNL.o(.text+0x54d): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `boost::system::get_generic_category()'
/tmp/ccvDmFNL.o(.text+0x566): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `boost::system::get_system_category()'
/tmp/ccvDmFNL.o(.gnu.linkonce.t._ZN5boost10filesystem24basic_directory_iteratorINS0_10basic_pathISsNS0_11path_traitsEEEE6m_initERKS4_+0x2e): In function `boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::m_init(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)':
: undefined reference to `boost::filesystem::detail::not_found_error()'
/tmp/ccvDmFNL.o(.gnu.linkonce.t._ZN5boost10filesystem24basic_directory_iteratorINS0_10basic_pathISsNS0_11path_traitsEEEE6m_initERKS4_+0xbe): In function `boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::m_init(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)':
: undefined reference to `boost::filesystem::detail::dir_itr_first(void*&, void*&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::filesystem::file_status&, boost::filesystem::file_status&)'
/tmp/ccvDmFNL.o(.gnu.linkonce.t._ZN5boost6system10error_codeC1Ev+0x14): In function `boost::system::error_code::error_code()':
: undefined reference to `boost::system::get_system_category()'
/tmp/ccvDmFNL.o(.gnu.linkonce.t._ZN5boost10filesystem24basic_directory_iteratorINS0_10basic_pathISsNS0_11path_traitsEEEE9incrementEv+0xde): In function `boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::increment()':
: undefined reference to `boost::filesystem::detail::dir_itr_increment(void*&, void*&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::filesystem::file_status&, boost::filesystem::file_status&)'
/tmp/ccvDmFNL.o(.gnu.linkonce.t._ZN5boost10filesystem6statusINS0_10basic_pathISsNS0_11path_traitsEEEEENS_9enable_ifINS0_13is_basic_pathIT_EENS0_11file_statusEE4typeERKS7_+0x34): In function `boost::enable_if<boost::filesystem::is_basic_path<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >, boost::filesystem::file_status>::type boost::filesystem::status<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)':
: undefined reference to `boost::filesystem::detail::status_api(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::system::error_code&)'
/tmp/ccvDmFNL.o(.gnu.linkonce.t._ZN5boost10filesystem6detail11dir_itr_impINS0_10basic_pathISsNS0_11path_traitsEEEED1Ev+0x1d): In function `boost::filesystem::detail::dir_itr_imp<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::~dir_itr_imp()':
: undefined reference to `boost::filesystem::detail::dir_itr_close(void*&, void*&)'
collect2: ld returned 1 exit status
EDIT:
Now i tried:
g++ -I/home/foo/include/ test.cc -lboost_system -lboost_filesystem
and get the following error:
/usr/bin/ld: cannot find -lboost_system
collect2: ld returned 1 exit status
I have the libboost_system-gcc34-1_38.so within
/home/foo/lib
how can I point to that?
from gcc man page:
-Ldir Add directory dir to the list of directories to be searched for -l.
So, is it that you are missing -L/home/foo/lib to the command line?
Your code compiled properly on my linux machine (Ubuntu 10.04, boost-filesystem 1.40) with the following command:
g++ test.cpp -lboost_filesystem
or
g++ test.cpp -lboost_system -lboost_filesystem
It gave me compile errors with g++ test.cpp -lboost_system
You must tell the compiler/linker where your libraries are as well, if they are not in the default location. For this you must use the -L flag to the compiler:
g++ -I/home/y/include/ test.cc -L/home/foo/lib -lboost_system -lboost_filesystem
Add -lboost_system (-lboost_system-mt if you're going to be threading) and -lboost_filesystem (-lboost_filesystem-mt if you're going to be threading) to the cmdline, before any input files.
Your boost libraries appear to be decorated with the suffix gcc34-1_38. Are you using gcc 3.4? Is your boost library on the library path? If not you may need to add the the path to your boost libraries using the -L flag to g++ or you can add the path to the LD_LIBRARY_PATH environment variable. In any case, you can link to your boost libraries by using -lboost_system-gcc34-1_38 and -lboost_filesystem-gcc34-1_38.