Giter Site home page Giter Site logo

Comments (12)

baziorek avatar baziorek commented on May 25, 2024

After night of sleeping I've tried something else.


There is information in official MySQL documentation how to have static linking:
image

Unfortunately doing this steps with SOCI didn't work (still problem with dynamic linking):

# first:
cmake -DCMAKE_INSTALL_PREFIX="$installPath" -DSOCI_SHARED=OFF -DSOCI_TESTS=OFF -DSOCI_CXX11=ON ..
make install -j12
cd -

#then:
g++ -isystem"${installPath}/include" -isystem/usr/include/mysql/ --std=c++23 test.cpp -c && g++ test.o `mysql_config --variable=pkglibdir`/libmysqlclient.a `pkg-config --static --libs mysqlclient` -L"${installPath}/lib"  -lsoci_core -lsoci_mysql  && ./a.out
# ... a lot of linkage errors...

But I tried to use just MySQL code (C++ code without SOCI) and I succedded:

g++ -isystem"${installPath}/include" -isystem/usr/include/mysql/ --std=c++23 test2.cpp -c && g++ test2.o `mysql_config --variable=pkglibdir`/libmysqlclient.a `pkg-config --static --libs mysqlclient` && ./a.out 

1, Mamre, %                                                                                                                                                                                                                 

# then ldd command:
ldd a.out 
linux-vdso.so.1 (0x00007ffd839a3000)
libzstd.so.1 => /lib/x86_64-linux-gnu/libzstd.so.1 (0x00007f9e476db000)
libssl.so.3 => /lib/x86_64-linux-gnu/libssl.so.3 (0x00007f9e46f5c000)
libcrypto.so.3 => /lib/x86_64-linux-gnu/libcrypto.so.3 (0x00007f9e46a00000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f9e46600000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f9e476bb000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9e46200000)
/lib64/ld-linux-x86-64.so.2 (0x00007f9e477d1000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f9e46e75000)

The code is:

#include <iostream>
#include <mysql.h>
using namespace std;

constexpr char DATABASE_NAME[] = "stack";
constexpr char USERNAME[] = "someuser";
constexpr char PASSWORD[] = "somepass";

int main()
{
    MYSQL *conn = mysql_init(nullptr);

    // Connect to MySQL database
    if (!mysql_real_connect(conn, "127.0.0.1", USERNAME, PASSWORD, DATABASE_NAME, 0, nullptr, 0)) {
        std::cerr << "Error: " << mysql_error(conn) << std::endl;
        exit(1);
    }

    // Execute SQL query
    if (mysql_query(conn, "SELECT * FROM communities")) {
        std::cerr << "Error: " << mysql_error(conn) << std::endl;
        exit(1);
    }

    // Get result set
    MYSQL_RES *result = mysql_store_result(conn);

    // Print data from table
    MYSQL_ROW row;
    while ((row = mysql_fetch_row(result))) {
        for (unsigned int i = 0; i < mysql_num_fields(result); ++i) {
            if (i > 0) std::cout << ", ";
            std::cout << row[i];
        }
        std::cout << std::endl;
    }

    // Free result set
    mysql_free_result(result);

    // Close connection
    mysql_close(conn);
}

So there is some kind of bug in SOCI (or I'm doing something wrong?)

from soci.

zann1x avatar zann1x commented on May 25, 2024

It seems like you're linking the wrong targets. In order to use SOCI as a static library, you should link soci_core_static and soci_mysql_static.

from soci.

baziorek avatar baziorek commented on May 25, 2024

It seems like you're linking the wrong targets. In order to use SOCI as a static library, you should link soci_core_static and soci_mysql_static.

But it is not generating that files to link, only:

 ls -lah libs/lib
total 6.1M
drwxrwxr-x 3 agh agh 4.0K Mar 17 17:34 .
drwxrwxr-x 4 agh agh 4.0K Mar 17 17:34 ..
drwxrwxr-x 3 agh agh 4.0K Mar 17 17:34 cmake
-rw-r--r-- 1 agh agh 3.9M Mar 17 17:32 libsoci_core.a
-rw-r--r-- 1 agh agh 149K Mar 17 17:32 libsoci_empty.a
-rw-r--r-- 1 agh agh 1.1M Mar 17 17:32 libsoci_mysql.a
-rw-r--r-- 1 agh agh 1.2M Mar 17 17:32 libsoci_postgresql.a

All steps to reproduce:

sudo apt install libmysqlclient-dev libmysql++-dev libmysqlcppconn-dev

git clone --depth=1 -b v4.0.3 https://github.com/SOCI/soci.git

export installPath="$(pwd)/libs"

mkdir soci/building
mkdir "$installPath"
cd soci/building
cmake -DCMAKE_INSTALL_PREFIX="$installPath" -DSOCI_SHARED=OFF -DSOCI_TESTS=OFF -DSOCI_CXX11=ON ..
make -j12 && make install
cd - # going back to previous directory

I'm also attaching full output of cmake command:
cmakeOutput.txt
More interesting part:

cat cmakeOutput.txt| grep -i mysql 
-- MySQL:
-- Performing Test HAVE_MYSQL_OPT_EMBEDDED_CONNECTION
-- Performing Test HAVE_MYSQL_OPT_EMBEDDED_CONNECTION - Failed
-- Found MySQL: /usr/include/mysql, /usr/lib/x86_64-linux-gnu/libmysqlclient.so
-- MySQL Embedded not found.
-- MYSQL_INCLUDE_DIR                        = /usr/include/mysql
-- MYSQL_LIBRARIES                          = /usr/lib/x86_64-linux-gnu/libmysqlclient.so
-- Found MySQL: /usr/include/mysql, /usr/lib/x86_64-linux-gnu/libmysqlclient.so
-- MySQL Embedded not found.
-- MySQL - SOCI backend for MySQL
-- SOCI_MYSQL                               = ON
-- SOCI_MYSQL_TARGET                        = soci_mysql
-- SOCI_MYSQL_OUTPUT_NAME                   = soci_mysql
-- SOCI_MYSQL_COMPILE_DEFINITIONS           = SOCI_ABI_VERSION="4.0" HAVE_DL=1 BOOST_ALL_NO_LIB
-- SOCI_MYSQL_INCLUDE_DIRECTORIES           = /usr/include /home/agh/Nextcloud/AGH/progranistyczneDlaStudentowWspolne/sprawdzarkaDomowe/sociTest/soci/building /home/agh/Nextcloud/AGH/progranistyczneDlaStudentowWspolne/sprawdzarkaDomowe/sociTest/soci/include /home/agh/Nextcloud/AGH/progranistyczneDlaStudentowWspolne/sprawdzarkaDomowe/sociTest/soci/building/include /home/agh/Nextcloud/AGH/progranistyczneDlaStudentowWspolne/sprawdzarkaDomowe/sociTest/soci/include/private /home/agh/Nextcloud/AGH/progranistyczneDlaStudentowWspolne/sprawdzarkaDomowe/sociTest/soci/include/private /usr/include/mysql
MYSQL_EMBEDDED_LIBRARIES:FILEPATH=MYSQL_EMBEDDED_LIBRARIES-NOTFOUND
MYSQL_INCLUDE_DIR:PATH=/usr/include/mysql
MYSQL_LIBRARIES:FILEPATH=/usr/lib/x86_64-linux-gnu/libmysqlclient.so
SOCI_MYSQL:BOOL=ON
WITH_MYSQL:BOOL=ON

from soci.

baziorek avatar baziorek commented on May 25, 2024

I checked also installing SOCI with MySQL support with VCPKG package manager - it succeed with static building.


Details:

So first I'm installing with soci:

export vcpkg_path="$(pwd)"

git clone --depth=1 -b 2024.02.14 https://github.com/microsoft/vcpkg

./vcpkg/bootstrap-vcpkg.sh -disableMetrics

vcpkgBinaryPath="${vcpkg_path}/vcpkg/vcpkg"
$vcpkgBinaryPath install 'soci[mysql]'

Everything is success, then I see:

    find_package(SOCI CONFIG REQUIRED)
    # Using core (loading backends at runtime)
    target_link_libraries(main PRIVATE $<IF:$<TARGET_EXISTS:SOCI::soci_core>,SOCI::soci_core,SOCI::soci_core_static>)

    # Using the mysql backend directly
    target_link_libraries(main PRIVATE $<IF:$<TARGET_EXISTS:SOCI::soci_mysql>,SOCI::soci_mysql,SOCI::soci_mysql_static>)

But I want to use CMakeLists.txt, so I need to have link:

$vcpkgBinaryPath integrate install
Applied user-wide integration for this vcpkg root.
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=/home/agh/Desktop/sociMySQL_withVcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake"

Then I've created sample CMakeLists.txt file:

cmake_minimum_required(VERSION 3.20)

set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake"  CACHE STRING "")

project(sociMySQL_withVcpkg VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(SOCI CONFIG REQUIRED static)


set(PROJECT_SOURCES
    main.cpp
)

add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})


# Using core (loading backends at runtime)
target_link_libraries(${PROJECT_NAME} PRIVATE $<IF:$<TARGET_EXISTS:SOCI::soci_core>,SOCI::soci_core,SOCI::soci_core_static>)

# Using the mysql backend directly
target_link_libraries(${PROJECT_NAME} PRIVATE $<IF:$<TARGET_EXISTS:SOCI::soci_mysql>,SOCI::soci_mysql,SOCI::soci_mysql_static>)

And sample C++ code (with name main.cpp):

#include <iostream>
#include <string>
#include <exception>
#include <soci/soci.h>
#include <soci/soci-config.h>
#include <soci/mysql/soci-mysql.h>

#if __has_include(<format>)
    #include <format>
    namespace fmt = std;
#elif __has_include(<fmt/format.h>)
    #define FMT_HEADER_ONLY
    #include <fmt/format.h>
#else
    #error "No format library!"
#endif

using namespace std;
using namespace soci;

constexpr char DATABASE_NAME[] = "stack";
constexpr char USERNAME[] = "someuser";
constexpr char PASSWORD[] = "somepass";


int main()
{
    auto connectionString = fmt::format("host=127.0.0.1 db={} user={} password='{}'", DATABASE_NAME, USERNAME, PASSWORD);

    try
    {
        soci::session sql(soci::mysql, connectionString);

        sql << R"(
CREATE TABLE IF NOT EXISTS stack.communities (
    community_id INT PRIMARY KEY AUTO_INCREMENT NOT NULL COMMENT 'Unique identifier for each community',
    community_name_pl VARCHAR(100) NOT NULL COMMENT 'Name of the community in Polish',
    community_original_name VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL COMMENT 'Original name of the community if different from Polish',
    creators VARCHAR(100) NOT NULL COMMENT 'List of original founders of the community, comma-separated'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
COMMENT='Table containing main information about Catholic communities';
)";

        sql << "INSERT INTO stack.communities (community_name_pl, creators) VALUES ('Mamre', 'ks. dr Włodzimierz Cyran'); ";

        soci::rowset<soci::row> rows = sql.prepare << "SELECT * FROM stack.communities";
        for(auto& row: rows )
        {
            for( std::size_t i = 0; i != row.size(); ++i )
            {
                const column_properties & props = row.get_properties( i );

                cout << props.get_name() << " = ";

                switch( props.get_data_type() )
                {
                case dt_string:
                    cout << row.get < std::string >( i ) << '\n';
                    break;
                case dt_double:
                    cout << row.get < double >( i ) << '\n';
                    break;
                case dt_integer:
                    cout << row.get < int >( i ) << '\n';
                    break;
                default:
                    cerr << "(unknown type!)\n";
                }
            }
        }
    }
    catch(const soci_error& e)
    {
        cerr << "Error: " << e.what() << '\n';
    }
}

After everything is ready I can generate the project:

mkdir building
cd building
cmake ..
make -j12
./sociMySQL_withVcpkg

Result of program is:

./sociMySQL_withVcpkg
community_id = 1
community_name_pl = Mamre
community_original_name = Error: Null value not allowed for this type

from soci.

zann1x avatar zann1x commented on May 25, 2024

The libsoci_*.a files are the static library files of SOCI. Although the files are named differently, you should still link against the targets soci_*_static -- or at least that's what works on my end.

In your test with vcpkg you're linking against exactly these targets as well. There, it should also be sufficient to only link soci_core_static and soci_mysql_static. soci_core and soci_mysql are the targets for dynamically linking SOCI.

from soci.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.