CMakeList about MySQL Connector c++ 8.0 Configuration


i am not good at english, Please forgive me for the syntax error. i want to make a mysql project in linux/windows by cmake,so i use visual studio to edit. And i create a cmake project in visual studio, first,i only test boost in my project ,it's run successfully.

if (Boost_FOUND)
    message(STATUS "find Boost:\"${Boost_INCLUDE_DIRS}\",ENABLE_BOOST is opened")
    message(STATUS "find Boost:\"${Boost_LIBRARIES}\"")
endif (Boost_FOUND)

i can run a test by filesystem,so i want to set mysql connector c++ 8.0

include_directories("C:/Program Files/MySQL/MySQL Connector C++ 8.0/include")
link_directories("C:/Program Files/MySQL/MySQL Connector C++ 8.0/lib64/vs14")
link_directories("C:/Program Files/MySQL/mysql-8.0.18-winx64/lib")
add_executable (test "test.cpp")
target_link_libraries(test mysqlcppconn.dll libmysql.dll)

and i copy libmysql.dll and mysqlcppconn.lib(i can't find mysqlcppconn.dll,but i can use visual studio by a normal project run successfully through mysqlcppconn.lib) to my cmake project with test.cpp

so i run code,but a error show 0x00007FFEE1DBE710 (ucrtbased.dll),Access violation reading location in xstring.insert.h(a ms .h? i don't know this header)

there are my code,and i run successfully by a normal project through vs2017,but cmake can not run.

int main()
    cout << "Hello CMake。" << endl;
    cout << "You Finished." << endl;
    string exepath = boost::filesystem::initial_path<boost::filesystem::path>().string();
    cout << exepath << endl;
    try {
        sql::Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt;
        sql::ResultSet *res;
        cout << "create success" << endl;
        driver = get_driver_instance();
        con = driver->connect("localhost", "root", "123");
        stmt = con->createStatement();
        res = stmt->executeQuery("select * from detect");
        cout << "search..."<<endl;
        while (res->next()) {
            cout << "searhing..username:" << endl;
            cout << res->getString("username") << endl;
            cout << "searhing..password:" << endl;
            cout << res->getString("password") << 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 << "link failed" << endl;

return 0;

I debug code,and it's error in

cout << res->getString("username") << endl;

i promise my tables is ok

and i run code successfully by mysql c api through visual studio cmake project,but mysql connector c++ can not.

if you can tell me what shoud i do, thank you very much!

Update: @Alex i run the code ,then visual studio auto go the xstring_insert.h

// xstring_insert.h internal header
// Copyright (c) Microsoft Corporation. All rights reserved.
if (_State == ios_base::goodbit
            && _Ostr.rdbuf()->sputn(_Data, (streamsize)_Size)
                != (streamsize)_Size)
                _State |= ios_base::badbit;

my error is happend in if(). And visual studio output is

Exception thrown at 0x00007FFEF776E710 (ucrtbased.dll) in test.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

@seccpur I add .c_str() in my cout code,and i run successfully,could you tell my why i need do this.And i don't use .c_str() that can run code successfully in my general windows console.

asked on Stack Overflow Jan 13, 2020 by honeyhonglin • edited Jan 13, 2020 by honeyhonglin

1 Answer


The error is self-explanatory. It says:

Access violation reading location 0xFFFFFFFFFFFFFFFF.

In other words, you are trying to read address 0xFFFFFFFFFFFFFFFF (= -1), but you do not have permission to read it.

Let us now look at your if statement, since you′ve figured out that′s where the problem is.

if (_State == ios_base::goodbit
        && _Ostr.rdbuf()->sputn(_Data, (streamsize)_Size)
            != (streamsize)_Size)

Let us see which of these parts can cause that error: in

_State == ios_base::goodbit

you′re comparing one object to another. No such error should appear. In

    && _Ostr.rdbuf()->sputn(_Data, (streamsize)_Size)

you′re following the pointer of _Ostr.rdbuf(). If this value is -1, then it would cause the error you′ve quoted.

Finally, the last line, you′re again comparing two values, so that error is not possible. Now you know what the culprit of this error is. So the library in question would need to make sure that _Ostr.rdbuf() is a valid value before attempting to follow its pointer: apparently not all libraries do.

On the other hand, cstring() takes the value and converts it to a string, even if it is NOT valid. This is why using it works in this case.

Good job at improving your question. The initial question sounded terrible.

answered on Stack Overflow Jan 13, 2020 by Alex

User contributions licensed under CC BY-SA 3.0