C How to link MySQL to Code::Blocks?

So how do I need to setup CB to compile C++ code involving MySQL stuff?
I have mysql80-server package installed, headers seem to be in usr/local/include/mysql folder

I've tried putting mysql_config --cflags into 'other compiler options' and mysql_config --libs into "other linker options" but it just throws error that 'mysql_config' file isn't found - even though when I call it through the terminal with the same flags - it returns stuff for both.
 
Just a wild guess: Do you need to specify mysql_config with the full path? Something like /usr/local/bin/mysql_config?
 
Just a wild guess: Do you need to specify mysql_config with the full path? Something like /usr/local/bin/mysql_config?
I did try to include the full path but it then throws a error during compiling that "--libs" flag is an unrecognized commandline option.
I did try to just put both full set of flags in a Linker config returned through "mysql_config --libs" like "-L/usr/local/lib/mysql -lmysqlclient -pthread -lz -lzstd -lssl -lcrypto -lm -lrt -lexecinfo -L/usr/local/lib -lunwind" and a basic set like "-L/usr/local/lib/mysql -lmysqlclient" - and the program compiles all right - however it always crashes with a "segmentation fault(core dumped)" on launch.
Even if it's as simple as the most basic hello world program that doesn't even have "include mysql/mysql.h" in it.

And yeah it doesn't recognize "--cflags" commandline option as well when it comes to Compiler but replacing it with "-I/usr/local/include/mysql" that gets returned through "mysql_config --cflags" does not lead to crashes and the 'hello world' would compile, but when adding Linker parameters - the segmentation fault happens.

Help?
 
The compiler (include) & linker (lib) flags are given by:
Code:
mysql_config --cflags --libs
When I add -L/usr/local/lib/mysql -lmysqlclient to Linker - then compiling and trying to run the program - it always results in a crash with a 'segmentation fault (core dumped)'
same happens if I just use the full set of flags that "--libs" returns

The same crash happens even if I just compile with
Code:
g++ main.cpp -o testrun -L/usr/local/lib/mysql -lmysqlclient
Compiles without errors, but results in segmentation fault
 
If it is compiling and segfaulting when you are running it; then it means that it is linking properly; and more likely an issue with the program logic. Could you possibly post a small test program that shows the issue (so we can possibly see where it is crashing)?
 
If it is compiling and segfaulting when you are running it; then it means that it is linking properly; and more likely an issue with the program logic. Could you possibly post a small test program that shows the issue (so we can possibly see where it is crashing)?
the program is literally:
Code:
#include <iostream>

int main()
{
    std::cout << "Hello World!" << std::endl;
    return 0;
}

compiled with
Code:
g++ main.cpp -o testrun -L/usr/local/lib/mysql -lmysqlclient

Yes I tried writing MySQL-based program too - regardless of complexity it will crash with the same segmentation fault on launch. I take it there's some issue with mysqlclient library? Again it's a default one with mysql80-server package
 

Attachments

  • testrun.zip
    5.7 KB · Views: 87
Works fine
Code:
c++ helloworld.cpp `mysql_config --cflags --libs`
Code:
$ g++ main.cpp `mysql_config --cflags --libs`
$ ./a.out
Segmentation fault (core dumped)

I take it there's some issue with mysqlclient library then?

MySQL itself running just fine, I can access any database with both phpmyadmin and CLI

What can be the solution?
 
Maybe the development files include/lib are not inline with the server i.e. a different version.
A check for "old software"
Code:
pkg version -v | grep -v up-to-date
&
Code:
pkg which /usr/local/include/mysql/mysql.h
&
Code:
mysql -V
 
pkg version -v | grep -v up-to-date
mysql80-client-8.0.26 > succeeds port (port has 8.0.23)
mysql80-server-8.0.26 > succeeds port (port has 8.0.23)

/usr/local/include/mysql/mysql.h was installed by package mysql80-client-8.0.26

mysql Ver 8.0.26 for FreeBSD13.0 on amd64 (Source distribution)

uname -a
FreeBSD fbsdCode 13.0-RELEASE-p4 FreeBSD 13.0-RELEASE-p4 #0: Tue Aug 24 07:33:27 UTC 2021 root@amd64-builder.daemonology.net:/usr/obj/usr/src/amd64.amd64/sys/GENERIC amd64
 
Something must be broken on your system.
I'm running mariadb105 server&client compiled from ports, so i cannot fully compare , but the following works fine for me:
Code:
g++8 helloworld.cpp `mysql_config --cflags --libs`
g++9 helloworld.cpp `mysql_config --cflags --libs`
g++10 helloworld.cpp `mysql_config --cflags --libs`
Three things you could try:
-Backup the database
-Reinstall mysql client & server
-Analyse the core dump
-Build & install from ports
 
...
-Build & install from ports
yeah pkg removed the newest mysql server and decided to build from ports. After 2 hours:
Code:
pkg-static: cannot load keyword from /usr/ports/Keywords/postexec.ucl: No such file or directory
pkg-static: unknown keyword postexec: @postexec
pkg-static: cannot load keyword from /usr/ports/Keywords/postunexec.ucl: No such file or directory
pkg-static: unknown keyword postunexec: @postunexec
*** Error code 1

Stop.
make[3]: stopped in /usr/ports/devel/llvm90
*** Error code 1

Stop.
make[2]: stopped in /usr/ports/devel/llvm90
*** Error code 1

Stop.
make[1]: stopped in /usr/ports/databases/mysql80-server
*** Error code 1

Stop.
make: stopped in /usr/ports/databases/mysql80-server

Sigh. Why is it so broken?
And yes there's no Keywords directory in /usr/ports
 
Normally the compiler which comes with the kernel is untouched. That one is located in /usr/bin.
With that one you can normally compile all other compilers.
If you can remove completely /usr/ports :
Code:
pkg upgrade -f pkg
pkg install git
cd /usr/ports
rm -r /usr/ports/* /usr/ports/.??*
Repopulate /usr/ports :
Code:
git clone --branch 2021Q4 https://git.FreeBSD.org/ports.git /usr/ports
mkdir /usr/ports/distfiles
mkdir /usr/ports/packages
Add to make.conf
Code:
DEFAULT_VERSIONS+=mysql=8.0
Rebuild mysql-server
 
well before trying that I've also tried reinstalling mysql80-server - then I've tried mysql57-server with its corresponding older mysqlclient library - the error is still the same segmentation fault
 
It looks like your buildtool/compiler is broken, because you cannot compile llvm90 in your previous post, and this should work without any problem.
Verify freebsd-update fetch/upgrade/install.
Verify the output of
Code:
freebsd-version -kru
Maybe compile the compiler from scratch , followed by mysql-server.
 
It looks like your buildtool/compiler is broken, because you cannot compile llvm90 in your previous post, and this should work without any problem.
Verify freebsd-update fetch/upgrade/install.
Verify the output of
Code:
freebsd-version -kru
Maybe compile the compiler from scratch , followed by mysql-server.

as I pointed out llvm90 isn't compiled because the whole directory Keywords that should be in /usr/ports simply isn't there.
My ports are installed as is during fresh FreeBSD installation from an image

I also tried another thing - installing MariaDB. Code compiles and works as it should even with MySQL related data by using MariaDB's "-lmysqlclient"
I then uninstalled MariaDB and reinstalled the latest MySQL 8.0 - and the compiled program still works without any segmentation faults (well I needed to put MariaDB's additional shared library in with the rest).
But when I try compiling with stock mysqlclient - segmentation fault is back. And yet MariaDB-compiled program with literally the same code works as it should even with stock MySQL.

Makes little sense
 
`My ports are installed as is during fresh FreeBSD installation from an image`
Are your sure your ports are in sync with quarterly , i.e.:
If you don't sync you will not be able to have stable compilations.
The output of :
Code:
cd /usr/ports
git log | head | grep Date
Should be something like "Date: Thu Nov 25 20:11:50 2021 +0000"
 
Your problem is nothing to do with mysql; you're not even invoking it in your code so the linker won't even source the library. Setting a path to a non-included include unit is also superfluous.

Add -g to your command line then use lldb to invoke a.out and you'll have your answer.
 
Why bother guessing when you can find out precisely. Use ldd on it, use lldb/gdb. Either way his program doesn't use mysql so his supposed issue with mysql is irrelevant.
 
Why bother guessing when you can find out precisely. Use ldd on it, use lldb/gdb. Either way his program doesn't use mysql so his supposed issue with mysql is irrelevant.

This is what I get from GDB

Code:
Program received signal SIGSEGV, Segmentation fault.
Invalid permissions for mapped object.
0x0000000801b48938 in vtable for __cxxabiv1::__si_class_type_info () from /lib/libcxxrt.so.1

And libcxxrt.so.1 is NOT used when compiling with MariaDB client (with which program runs fine) as per ldd.

I then googled for the solution based on this error message and, well:

adding -static-libgcc -static-libstdc++ flags to g++ compilation commands indeed solved the issue and stock mysqlclient based program runs without issues now, same as with MariaDB.
Still scratching my head as to why this is happening without statically linking objects
 
Back
Top