-
Notifications
You must be signed in to change notification settings - Fork 478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SOCI with MySQL on Linux (Ubuntu) - impossible to link static #1131
Comments
After night of sleeping I've tried something else. There is information in official MySQL documentation how to have static linking: 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:
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?) |
It seems like you're linking the wrong targets. In order to use SOCI as a static library, you should link |
But it is not generating that files to link, only:
All steps to reproduce:
I'm also attaching full output of
|
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:
But I want to use CMakeLists.txt, so I need to have link:
Then I've created sample
And sample C++ code (with name #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:
Result of program is:
|
The In your test with vcpkg you're linking against exactly these targets as well. There, it should also be sufficient to only link |
Unfortunately when I even try with version from
and (after running make install) this is not building
Of course I can force CMake to create those files: All commands:
unfortunately is still fails! It is not compiling with commands, but fortunately version from VCPKG package manager works (link: #1131 (comment)). So for me it is enough. |
Same problem for me with Ubuntu 23.10, and backend SQLite3. |
Put soci_core after the backends, it will solve the link issue. |
Unfortunately it still didn't work after changing order (I tried steps described: #1131 (comment)). If You have sample code could You please provide for other users? |
in your comment, you put # compilation:
g++ -isystem/usr/include/mysql/ \
-isystem"${installPath}/include" -isystem./soci/include/ \
--std=c++23 test.cpp \
`mysql_config --variable=pkglibdir`/libmysqlclient.a \
`pkg-config --static --libs mysqlclient` -L"${installPath}/lib" \
-lsoci_mysql -lsoci_core && ./a.out |
Thanks for the suggestion, and using However, using |
use explicit static libraries : |
I'm trying to use soci with MySQL static (according to documentation it is possible).
I'll describe full process, because IMO there is too much "tricks", so probably I'm doing something wrong.
Preparation:
Running mysql:
To make sure connection is working I need to run MySQL, I'm using docker with command:
Then to make this is easy to reproduce I'm using docker to make all steps:
docker run -it --rm --network=host --name=ubuntusoci -v $(pwd):/home/ --workdir /home ubuntu:22.04
Rest of steps are inside docker:
Building soci:
Installing dependencies:
apt-get update && apt-get upgrade -y apt install build-essential libmysqlclient-dev git cmake libfmt-dev -y
Building SOCI:
Compilation using SOCI
I have sample code: test.txt (the extension is changed), but when I try to compile I see compilation problem:
1. This is first problem to report You.
Because as You see I'm using C++23, not C++11.
Dirty fix of the problem
I tried to rerun CMake with enabling C++11 support:
Now I'm able to compile:
But when I' trying to run I see problem with dynamic libraries:
Which are not visible with
ldd
:So I tried an answer for similar question and I've added
register_factory_mysql();
in the beginning ofmain
function.But this time it does not link:
Am I doing something wrong for now?
Building with shared libraries:
So I was unable to fix compilation, so I've tried to build with shared libraries.
Building program with RPATH:
Now it works:
But I prefer static linking
The text was updated successfully, but these errors were encountered: