1
votes

I'm now trying to implement a server using c++, I try to save the information of the users into database, but I came across some problem while connecting to the database. I can't build my program because of the following errors generated by code blocks

    -------------- Build: Debug in server (compiler: GNU GCC Compiler)---------------

    mingw32-g++.exe -Wall -fexceptions -g -lpthread -lmysql -I"D:\Program Files\MariaDB 10.1\include\mysql" -c D:\Projects\server\main.cpp -o obj\Debug\main.o
    mingw32-g++.exe -L"D:\Program Files\MariaDB 10.1\lib" -o bin\Debug\server.exe obj\Debug\main.o   "D:\Program Files (x86)\CodeBlocks\MinGW\lib\libwinpthread.a" "D:\Program Files (x86)\CodeBlocks\MinGW\lib\libws2_32.a"
    obj\Debug\main.o: In function `main':
    D:/Projects/server/main.cpp:13: undefined reference to `mysql_init@4'
    D:/Projects/server/main.cpp:14: undefined reference to `mysql_real_connect@32'
    collect2.exe: error: ld returned 1 exit status
    Process terminated with status 1 (0 minute(s), 2 second(s))
    3 error(s), 3 warning(s) (0 minute(s), 2 second(s))

I downloaded MariaDB Connector/ODBC 2.0.10 Stable from its official website and I've tried many different solutions that I found on the Internet, but it still doesn't work. Can anybody help me solve this? Any reply will be appreciated.

1
Your problem are lines 13 and 14 and the error is "undefined reference". Search for that error and you'll find lots of information. Apparently your program wants to use a function which it cannot find.Potaito
Thanks for replying, mysql_init is supposed to be defined in mysql.h and implemented in libmysql.lib and I did add the path that include mysql.h and libmysql.lib into the search directories in code blocks' settings.SolskGaer
Did you also link the library? "Includes" only allow it to be compiled, but you need to tell the linker to link your program with libmysql, which has been installed somewhere on your machine.Potaito
yes, I added the option -lmysql when compiling the programSolskGaer
You were supposed to add it for linking the program, not compiling its individual modules.Lightness Races in Orbit

1 Answers

1
votes

GNU linker is sensitive to option order. If you are using libmysql in main, option -lmysql shall go after main.o

UPD: I noticed you even haven't libs in second linking string. In first string it is useless, add to second, where actual linking takes place.