How to connect to Mariadb using C language in Windows 10? - c

Windows version - 10
gcc version - 6.3.0
MariaDB version - 10.9
Also, I have installed the c\c++ extension in visual code.
I am using Visual Code to run the below C code but facing the error maybe they are a problem with the linker. I am not sure what I am doing wrong.
#include <stdlib.h>
#include <windows.h>
#include "C:\Program Files\MariaDB 10.9\include\mysql\server\mysql.h"
int main(int argc, char** argv)
{
MYSQL* con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "localhost", "USERNAME", "PASSWD",
NULL, 0, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
if (mysql_query(con, "CREATE DATABASE testdb"))
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
mysql_close(con);
exit(0);
}
Command
gcc .\mysqlcon.c -o mysqlcon -L"C:\Program Files\MariaDB 10.9\lib\" -l"libmariadb.lib"
Error
PS C:\Users\Pan\Desktop\Mysqlcon> gcc .\mysqlcon.c -o mysqlcon -L"C:\Program Files\MariaDB 10.9\lib\" -l"libmariadb.lib"
C:\Users\Pan\AppData\Local\Temp\ccYX5KZT.o:mysqlcon.c:(.text+0x1e): undefined reference to `mysql_init#4'
C:\Users\Pan\AppData\Local\Temp\ccYX5KZT.o:mysqlcon.c:(.text+0x35): undefined reference to `mysql_error#4'
C:\Users\Pan\AppData\Local\Temp\ccYX5KZT.o:mysqlcon.c:(.text+0xa3): undefined reference to `mysql_real_connect#32'
C:\Users\Pan\AppData\Local\Temp\ccYX5KZT.o:mysqlcon.c:(.text+0xb5): undefined reference to `mysql_error#4'
C:\Users\Pan\AppData\Local\Temp\ccYX5KZT.o:mysqlcon.c:(.text+0xdf): undefined reference to `mysql_close#4'
C:\Users\Pan\AppData\Local\Temp\ccYX5KZT.o:mysqlcon.c:(.text+0x101): undefined reference to `mysql_query#8'
C:\Users\Pan\AppData\Local\Temp\ccYX5KZT.o:mysqlcon.c:(.text+0x113): undefined reference to `mysql_error#4'
C:\Users\Pan\AppData\Local\Temp\ccYX5KZT.o:mysqlcon.c:(.text+0x13d): undefined reference to `mysql_close#4'
C:\Users\Pan\AppData\Local\Temp\ccYX5KZT.o:mysqlcon.c:(.text+0x157): undefined reference to `mysql_close#4'
collect2.exe: error: ld returned 1 exit status

Include file:
#include "C:\Program Files\MariaDB 10.9\include\mysql\server\mysql.h"
This should generate a compilation error, since by default the MSI installer installs mysql.h in directory C:\Program Files\MariaDB 10.9\include\mysql.
Preferable you should include only mysql.h and specify the include directory instead.
Client library
The libraries shipped with MariaDB Server (and MariaDB Connector/C) are intended to use with VisualStudio compiler, not for MinGW.
If you want to build with msys/gcc, you have to install the MariaDB client libraries via pacman.

Related

Execute C script needing ext lib

Refering to Connect Postgresql in C
I asked some help to compile my script of Postgres database connection. This is now compiling well but now I have a problem when I execute it.
There is the code :
#include <stdio.h>
#include "libpq-fe.h"
#include <stdlib.h>
int main() {
PGconn *conn;
PGresult *res;
int rec_count;
int row;
int col;
conn = PQconnectdb("dbname=ljdata host=localhost user=dataman password=supersecret");
if (PQstatus(conn) == CONNECTION_BAD) {
puts("We were unable to connect to the database");
exit(0);
}
res = PQexec(conn,
"update people set phonenumber=\'5055559999\' where id=3");
res = PQexec(conn,
"select lastname,firstname,phonenumber from people order by id");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
puts("We did not get any data!");
exit(0);
}
rec_count = PQntuples(res);
printf("We received %d records.\n", rec_count);
puts("==========================");
for (row=0; row<rec_count; row++) {
for (col=0; col<3; col++) {
printf("%s\t", PQgetvalue(res, row, col));
}
puts("");
}
puts("==========================");
PQclear(res);
PQfinish(conn);
return 0;
}
And this is the compilation line it's actually working :
gcc -m64 -I "C:\Program Files\PostgreSQL\9.6\include" -L "C:\Program Files\PostgreSQL\9.6\lib" test.c -lpq -o test.exe
Now when I double-click on the .exe I have the following error :
"Unable to start the program because LIBPQ.dll is missing"
So it clearly means that I have to link the libpq.dll
But what I'm trying to do is compiling all the methods from the .c/.dll etc in the binary .exe which I plan to distribute and the postgresql libs will not be installed on the target machines.
After some web search, I found the "-static" parameter but I'm not sure that it's what I'm looking for and I'm not able to have it compiling with this parameter.
Then I tried to add "-static-libgcc" which results as :
gcc -m64 -static-libgcc -I "C:\Program Files\PostgreSQL\9.6\include" -L "C:\Program Files\PostgreSQL\9.6\lib" test.c -lpq -o test.exe
But I still have the "System error" when I try to run it :
"Unable to run the script because LIBPQ.dll is missing on your computer"
(Not sure if my french translate is exact)
Do you know the parameter or the way I can compile my C program into a binary .exe which would be totally standalone and without any library requirement ?
EDIT
Screenshot :
Stick with dynamic linking.
Your problem is that the system cannot find libpq.dll at runtime.
There are two solutions:
Add C:\Program Files\PostgreSQL\9.6\lib to the PATH environment variable.
Put a copy of libpq.dll in the same path as your executable.

problems compiling c code with libusb on linux

I'm trying to use libusb for a project but i'm unable to get the library working properly. Here is some source code i'm trying to compile. It doesn't do anything special. It's just a dummy program that gets the USB driver list then frees it.
#include <stdio.h>
#include <usb.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
struct libusb_device **devs;
struct libusb_context *context = NULL;
size_t list;
size_t i;
int ret;
ret = libusb_init(&context);
if(ret < 0)
{
perror("libusb_init");
exit(1);
}
list = libusb_get_device_list(context, &devs);
if(list < 0)
{
fprintf(stderr, "error in getting device list\n");
libusb_free_device_list(devs, 1);
libusb_exit(context);
exit(1);
}
libusb_free_device_list(devs, 1);
libusb_exit(context);
return 0;
}
I compile with
gcc -o test test.c -lusb
I get the error
/tmp/cc2hwzii.o: in function 'main:
test.c:(.text+0x24): undefined reference to 'libusb_init'
test.c:(.text+0x59): undefined reference to 'libusb_get_device_list'
test.c:(.text+0x8e): undefined reference to 'libusb_free_device_list'
test.c:(.text+0x9f): undefined reference to 'libusb_exit'
collect2: error: ld returned 1 exit status
I'm running ubuntu 14.04.3
I've installed libusb by sudo apt-get install libusb-dev
I've searched for my header file and it is called usb.h
I've looked to make sure I have the correct flag and it's -lusb
any ideas? I'd appreciate the help. If any more information is needed just ask.
those libusb_init are included in libusb-1.0.
you have to install libusb-1.0-0-dev
$ sudo apt-get install libusb-1.0-0-dev
$ gcc -o test test.c -lusb-1.0
/usr/lib/x86_64-linux-gnu/pkgconfig/libusb.pc which is included in libusb-dev says that the version is 0.1.12
and
/usr/lib/x86_64-linux-gnu/pkgconfig/libusb-1.0.pc which is included in libusb-1.0-0-dev says that the version is 1.0.17.
http://www.libusb.org/ says that 0.1 is legacy, and seems that API is different from 1.0.
You forgot to include the file that defines the functions, such as libusb_init. Have you tried including libusb.h?

DRMAA- Cant' link drmaa library when compiling c file

I wrote a small c file to test DRMAA but it keeps telling me that the DRMAA functions I used are not defined. I included the drmaa.h file in the C code. When I use -idrmaa I get this error:
[mkatouzi#argo-1 ~]$ cc -o drmtest -I$SGE_ROOT/include/ -ldrmaa -ldl drmtest.c
/usr/bin/ld: cannot find -ldrmaa
the DRMAA header file is in this path: $SGE_ROOT/include/
If I compile the file without -ldrmaa I get this error:
[mkatouzi#argo-1 ~]$ cc -o drmtest -I$SGE_ROOT/include/ drmtest.c
/tmp/cclsPr9O.o: In function `main':
drmtest.c:(.text+0x3c): undefined reference to `drmaa_init'
drmtest.c:(.text+0x83): undefined reference to `drmaa_exit'
collect2: ld returned 1 exit status
I am using my school's UNIX system and I am very new to it. Can anyone help me with this?
This is my drmtest.c file:
#include <stdio.h>
#include "drmaa.h"
int main (int argc, char **argv) {
char error[DRMAA_ERROR_STRING_BUFFER];
int errnum = 0;
errnum = drmaa_init (argv[0], error, DRMAA_ERROR_STRING_BUFFER);
if (errnum != DRMAA_ERRNO_SUCCESS) {
fprintf (stderr, "Couldn't init DRMAA library: %s\n", error);
return 1; }
/* Do Stuff */
errnum = drmaa_exit (error, DRMAA_ERROR_STRING_BUFFER);
if (errnum != DRMAA_ERRNO_SUCCESS) {
fprintf (stderr, "Couldn't exit DRMAA library: %s\n", error);
return 1; }
return 0;
}
In the first case, the linker is you telling it does not know where to find the drmaa library. In the second case, since you have not included the drmaa library, the linker is telling you it does not know how to resolve the drmaa functions you are using.
You need to figure out where the drmaa library files are, i.e. in which directory.
Once you know that, you can specify -L/path/to/drmaa/directory when compiling/linking to resolve the problem.
As per Brian Cain's answer, the library (drmaa.a or drmaa.so) is probably under $SGE_ROOT/lib.
Finally, since the directory where the library is stored is not in the system's standard library search path, you have to tell the dynamic linker where to find the library when running the executable. There are two ways to achieve this:
Set (and export) the LD_LIBRARY_PATH environment variable to the library's directory (e.g. $SGE_ROOT/lib)
Or add the -R/path/to/drmaa/directory option when compiling/linking.
You likely need to specify the library path at which libdrmaa.so is found.
e.g.
cc -o drmtest -I$SGE_ROOT/include/ -L$SGE_ROOT/lib/ -ldrmaa -ldl drmtest.c
If you encounter a run-time problem linking against the library, you should check your system configuration.
The LD_LIBRARY_PATH environment variable can be used in a pinch, but on many modern systems you can/should use ld.so.conf.
e.g.
echo <<EOF > /etc/ld.so.conf.d/sge.conf
/usr/sge/lib
EOF

Unzip one file with libzip

I need to create an application to extract one file from zip archive, after which I want to compile it for Android.
I'm using Ubuntu, with libzip-0.10.1 pre-installed.
I created C project in Eclipse, added include path and found simple script for extracting file. Unfortunately I cannot get the following to build and I could use some advice.
// zip.c file
#include <stdio.h>
#include <stdlib.h>
#include <zip.h>
int main(int argc, char **argv) {
struct zip *zip_file;
struct zip_file *file_in_zip;
int err;
int files_total;
int file_number;
int r;
char buffer[10000];
if (argc < 3) {
fprintf(stderr,"usage: %s <zipfile> <fileindex>\n",argv[0]);
return -1;
};
zip_file = zip_open(argv[1], 0, &err);
if (!zip_file) {
fprintf(stderr,"Error: can't open file %s\n",argv[1]);
return -1;
};
file_number = atoi(argv[2]);
files_total = zip_get_num_files(zip_file);
if (file_number > files_total) {
printf("Error: we have only %d files in ZIP\n",files_total);
return -1;
};
file_in_zip = zip_fopen_index(zip_file, file_number, 0);
if (file_in_zip) {
while ( (r = zip_fread(file_in_zip, buffer, sizeof(buffer))) > 0) {
printf("%s",buffer);
};
zip_fclose(file_in_zip);
} else {
fprintf(stderr,"Error: can't open file %d in zip\n",file_number);
};
zip_close(zip_file);
return 0;
};
Also I added few .h files to include directory in my project and few .c files to directory with zip.c file. After that all dependences was good, but I have an error:
‘struct zip’ has no member named ‘default_password’ in file zip_fopen_index.c
The file zip_fopen_index.c is:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "zipint.h"
ZIP_EXTERN struct zip_file *
zip_fopen_index(struct zip *za, zip_uint64_t fileno, int flags)
{
return zip_fopen_index_encrypted(za, fileno, flags, za->default_password); // error here
}
First of all allow me some comments:
Your program is not compiled and linked by Eclipse.
Compiling is done by the compiler (gcc using option -c):
make all
Building file: ../zip.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"zip.d" -MT"zip.d" -o "zip.o" "../zip.c"
Finished building: ../zip.c
Linking is done by the linker (via the compiler using option -o):
Invoking: GCC C Linker
gcc -o "unzipper" ./zip.o
./main.o: In function `zip':
/home/alk/workspace/unzipper/Debug/../zip.c:20: undefined reference to `zip_open'
/home/alk/workspace/unzipper/Debug/../zip.c:27: undefined reference to `zip_get_num_files'
/home/alk/workspace/unzipper/Debug/../zip.c:33: undefined reference to `zip_fopen_index'
/home/alk/workspace/unzipper/Debug/../zip.c:35: undefined reference to `zip_fread'
/home/alk/workspace/unzipper/Debug/../zip.c:38: undefined reference to `zip_fclose'
/home/alk/workspace/unzipper/Debug/../zip.c:43: undefined reference to `zip_close'
collect2: ld returned 1 exit status
Eclipse provides a framework helping you in managing all sources and their references as also spawing compiler and linker tasks and setting their options.
When the linker told you there where undefined references to the zip_*function during the build of your program, the cause for this was, you were missing to tell the linker (via the compiler, via Eclipse) where those zip_* functions could be found.
Those zip_* functions are located in a library, namely libzip.
So what you as the programmer need to tell the linker (via the compiler, via Eclipse) is to link those functions against what the compiler compiled from your sources.
As the result the linker is able to create a runnable program from your compiled sources together with all libraries needed. Certain libraries are know to Eclipse (and therfore to the linker) by default, for example the one containing the C standard functions, namely libc.
To get things going:
1 Remove the source files you pulled from the libzip librarie's sources from your project. Those sources had been compiled into the library libzip, which you will use in your project.
2 Tell the linker (via Eclipse) to use libzip for your project.
Do so by following the steps below:
open the project's properties
click 'C/C++ General'
click 'Path and Symbols', on the left select the 'Libraries' tab, there click 'Add' and enter zip
finally click 'OK'
3 Then try to build your program:
Building target: unzipper
Invoking: GCC C Linker
gcc -o "unzipper" ./zip.o -lzip
Finished building target: unzipper
(Please note additional option -lzip!)
If the developement version of 'libzip' had been installed properly before, you should be fine.
PS: unzipper was the name I used for the Eclispe project to produce the examples.
PSS: I used Eclipse Juno SR1

Compile Attempt Gives crt1.o/'start'/undefined reference to 'main'/exit status message

I am working from a book: TCP/IP Sockets in C and its website code.
I am trying to build a client and server based on those files. My make gives lots of
error related to not being able to find functions from DieWithMessage.c
Here it is:
#include <stdio.h>
#include <stdlib.h>
#include "Practical.h"
void DieWithUserMessage(const char *msg, const char *detail) {
fputs(msg, stderr);
fputs(": ", stderr);
fputs(detail, stderr);
fputc('\n', stderr);
exit(1);
}
void DieWithSystemMessage(const char *msg) {
perror(msg);
exit(1);
}
When I do gcc DieWithMessage.c, I get the following error:
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o: In function _start':
(.text+0x18): undefined reference tomain'
collect2: ld returned 1 exit status
How do I compile this by itself so that the errors will stop happening when using the makefile?
Thanks for any help.
Your C code needs a main function if you're going to try an link/run it. This is a requirement for hosted C applications under the standard.
That error message indicates what's wrong. The C runtime/startup code (CRT) has an entry point of start which sets up the environment then calls your main. Since you haven't provided a main, it complains.
If you only want to generate an object file for later linking with a main (see here for one description of the process), use something like:
gcc -c -o DieWithMessage.o DieWithMessage.c
(-c is the "compile but don't link" flag). You can then link it later with your main program with something like (although there are other options):
gcc -o myProg myProg.c DieWithMessage.o
If you want a placeholder main to update later with a real one, you can add the following to your code:
int main (void) { return 0; }

Resources