LaVOZs

The World’s Largest Online Community for Developers

'; c++ - Segmentation fault in visual studio cmake remote linux develop mysql - LavOzs.Com

I am developing a mysql project with visual studio cmake remote linux.

My version is vs2017,cmake 3.8(by vs installer install),mysql 8.0.18,mysql connector c++ 8.0,boost_1_64_0,

and my linux os is centos 8.

First I test the code in Windows-debug in vs,it's run successfully,

then I link my VMWave centos8,error happended.

con = driver->connect(host.c_str(), user.c_str(), password.c_str());

Vs tell me Segmentation fault in thie line.

There are my cmakelist.txt

cmake_minimum_required (VERSION 3.8)

IF(WIN32)
    INCLUDE_DIRECTORIES("C:/Program Files/MySQL/MySQL Connector C++ 8.0/include")
ENDIF(WIN32)

IF(UNIX)
    INCLUDE_DIRECTORIES("/usr/local/mysql-connector-c++/include")
ENDIF(UNIX)

FIND_PACKAGE(Boost)
IF(Boost_FOUND)
    INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
    LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
ENDIF(Boost_FOUND)

IF(WIN32)
    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")
ENDIF(WIN32)

IF(UNIX)
    LINK_DIRECTORIES("/usr/local/mysql-connector-c++/lib64")
    LINK_DIRECTORIES("/usr/local/mysql/lib")
ENDIF(UNIX)

add_executable (CMakeLinkLinux "CMakeLinkLinux.cpp" "CMakeLinkLinux.h")

IF(WIN32)
    TARGET_LINK_LIBRARIES(CMakeLinkLinux mysqlcppconn.dll libmysql.dll)
ENDIF(WIN32)

IF(UNIX)
    TARGET_LINK_LIBRARIES(CMakeLinkLinux libmysqlcppconn.so libmysqlclient.so)
ENDIF(UNIX)

main.cpp

#include "CMakeLinkLinux.h"
#include <mutex>
using namespace std;
int main()
{   
    mutex mtx;
    int flag;
    cout << "Hello CMake." << endl;

    try {
        sql::Driver *driver=NULL;
        sql::Connection *con=NULL;
        sql::Statement *stmt=NULL;
        sql::ResultSet *res=NULL;
        //tcp://192.168.248.128:3306
        string host = "tcp://192.168.248.128:3306";
        string user = "root";
        string password = "123";
        string database = "INFORMATION_SCHEMA";
        mtx.lock();
        driver = get_driver_instance();
        if (!driver) {
            cout << "driver error" << endl;
        }
        mtx.unlock();
        driver->threadInit();
        cout << "connecting......mysql" << endl;
        con = driver->connect(host.c_str(), user.c_str(), password.c_str());
        cout << "connected successfully" << endl;
        con->setSchema(database);
        stmt = con->createStatement();
        res = stmt->executeQuery("SELECT TABLE_SCHEMA,TABLE_NAME,CREATE_OPTIONS FROM TABLES WHERE CREATE_OPTIONS LIKE '%ENCRYPTION%'");
        while (res->next()) {
            cout << "TABLE_SCHEMA:";
            cout << res->getString("TABLE_SCHEMA").c_str() << endl;
            cout << "TABLE_NAME:";
            cout << res->getString("TABLE_NAME").c_str() << endl;
            cout << "CREATE_OPTIONS:";
            cout << res->getString("CREATE_OPTIONS").c_str() << endl;
        }

        delete res;
        delete stmt;
        delete con;
        driver->threadEnd();
    }
    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;
    }

    cin >> flag;
    return 0;
}

I find ans by google and many ways,I didn't know why Segmentation fault in my code,someone tell lock the driver code can run in stackoverflow,but I still Segmentation fault in my code.

Update:

gdb output:

Type "apropos word" to search for commands related to "word".
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
=cmd-param-changed,param="pagination",value="off"
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Breakpoint 1, main () at /var/tmp/src/2d31707c-c3da-e131-91d2-9c506bc1ae02/Linux-Debug/CMakeLinkLinux/CMakeLinkLinux.cpp:9
9       cout << "Hello CMake." << endl;

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff5f9ae88 in __memmove_avx_unaligned_erms () from /lib64/libc.so.6
Segmentation fault

Related
Debugging with command-line parameters in Visual Studio
Using CMake to generate Visual Studio C++ project files
What is a segmentation fault?
How to catch segmentation fault in Linux?
cmake and visual studio
How to use Boost in Visual Studio 2010
How to allow remote connection to mysql
CMake Noob segmentation fault issue
CMake does not find Visual C++ compiler