Cannot link to MySQL libraries for C++ - c++

I've been trying to use MySQL and C++ together but can't seem to get started as I cannot seem to gain usage of the relevant libraries.
I am on Win7 using MinGW compiler and working in Netbeans.
I have the code:
#include <cstdlib>
#include <iostream>
#include <my_global.h>
#include <mysql.h>
using namespace std;
int main(int argc, char **argv)
{
cout << "MySQL client version: " << mysql_get_client_info();
}
But netbeans cannot find my_global.h or mysql.h.
In properties of the project I've linked to the library libmysql.dll.
Also present in the same directory is mysqlclient.lib but I can't find a way to link to that as the NetBeans linker doesn't seem to register that extension type.
Any help would be greatly appreciated.
C
---PROGRESS
I went into NetBeans' Properties->Build->C++ section and added the include directory of my MySQL installation in the 'Include Directories' section. This has solved the above issue of not finding my_global.h or mysql.h but now it cannot find crtdbg.h...
Actually had crtdbg.h in an old Visual Studio installation, moved it and all the other .h files there over to my MinGW includes folder. Seems to find the .hs now but fails with loads of errors, probably an issue with the Visual Studio .h files not being compatible with MinGW. Back to the drawing board.

Set the include directories, mate. It is under Tools->Options->C++->Code Assistance. Add the path where the my_global.h is.
See this forum post.

I'm not sure in Windows, but in Linux you can use the mysql_config tool to get the correct flags to compile a client application:
For compiling:
$ mysql_config --cflags
-I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -DUNIV_LINUX -DUNIV_LINUX
And for linking:
$ mysql_config --libs
-Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient
Now, if you are using MinGW, options should be similar, probably dropping the *_LINUX ones.
My bet is that you are simply missing the -I<path_to_include_dir> bit.

Related

<glad/glad.h>: No such file or directory

I'm following this tutorial to learn OpenGL, but I'm having trouble compiling since the compiler can't find one of the header files.
This is the file I'm trying to compile:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
int main() {
return 0;
}
To compile, I'm using
$ gcc -o sandbox sandbox.cpp -lGL -lGLU -lglut
and I get the following error:
sandbox.cpp:1:23: fatal error: glad/glad.h: No such file or directory
#include <glad/glad.h>
^
compilation terminated.
I followed the first two sections of this wiki
to install OpenGL and libraries.
I think the problem is either the wrong compile command or a flaw in my OpenGL installation.
GLAD is a function loader for OpenGL. This tutorial explains how to set it up.
The tutorial explains the purpose of GLAD:
Since there are many different versions of OpenGL drivers, the location of most of its functions is not known at compile-time and needs to be queried at run-time.
Setup of GLAD involves using a web server to generate source and header files specific to your GL version, extensions, and language. The source and header files are then placed in your project's src and include directories.
If you are looking at this simple GLFW example you can remove the glad/gl.h include, and the
gladLoadGL(glfwGetProcAddress);
line further down.
If you are on linux, ubuntu for example,
you don't need glad, just add these 2 headers instead:
#include <GLES2/gl2.h>
#include <EGL/egl.h>
If the example is saved as glfw_ex2.c you can compile it at the command line like this:
g++ glfw_ex2.c -lglfw -lGLESv2
Of course, linmath.h must be present in the same directory for this example.
If anything is missing, you can install it like this and try compiling again:
sudo apt install libglfw3-dev libgles2-mesa-dev libegl1-mesa-dev
sudo apt install build-essential
then run it like this:
./a.out

Compiling boost::asio example on Windows

I am trying to switch to Windows environment from Linux, but find it a very hard path.
This time I wanted to test if I can work with boost library.
I had problems with compiling boost on windows, so I downloaded precompiled version. I unpacked everything and tested positively that I can compile the header-only librariers.
Then I copied some simple boost::asio example. I set up everything in Eclipse. Compilation went fine, but during linking I got 'undefined reference' problem to 'boost::system' internal stuff.
C:/Users/jacek/cpp/boost_1_62_0/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
C:/Users/jacek/cpp/boost_1_62_0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
C:/Users/jacek/cpp/boost_1_62_0/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
So I added '-lboost_system', as well as the path to the libraries directory, to my linking options. But this did not help.
g++ "-LC:\\Users\\jacek\\cpp\\boost_1_62_0\\lib64-msvc-14.0" -o TestAsio.exe "src\\Main.o" -lboost_system
I checked the libraries directory and found there is a bunch of files containing 'boost_system' in the name. They are:
libboost_system-vc140-mt-1_62.lib
libboost_system-vc140-mt-gd-1_62.lib
libboost_system-vc140-mt-s-1_62.lib
libboost_system-vc140-mt-sgd-1_62.lib
libboost_system-vc140-s-1_62.lib
libboost_system-vc140-sgd-1_62.lib
I did not know which I should use. I tried adding 'libboost_system-vc140-mt-1_62' to the linking options, I tried all other files, I tried renaming the files to the linux pattern 'libboost_system.a', but nothing worked.
g++ "-LC:\\Users\\jacek\\cpp\\boost_1_62_0\\lib64-msvc-14.0" -o TestAsio.exe "src\\Main.o" -llibboost_system-vc140-mt-1_62 -llibboost_system-vc140-mt-gd-1_62 -llibboost_system-vc140-mt-s-1_62 -llibboost_system-vc140-mt-sgd-1_62 -llibboost_system-vc140-s-1_62 -llibboost_system-vc140-sgd-1_62
What am I doing wrong here?
Please help...
YotKay
I solved it myself with the help of a comment from this post: boost asio example compilation error
It looks like the precompiled version of Boost is created with Visual Studion and is NOT COMPATIBLE with G++. I if I decided to install MinGW then I cannot use the precompiled version of boost, but must compile it myself using g++.
I did that.
Now I have libraries compiled with G++.
I specify the path to the boost system library like that:
c:\Users\jacek\cpp\boost_1_62_0\libraries\boost\bin.v2\libs\system\build\gcc-mingw-6.2.0\debug\link-static\
and add this option:
-lboost_system-mgw62-d-1_62
Now the problem with boost::system disappears. However, another one pops up with boost asio, but luckily the answer is here: MinGW linker error: winsock
The example works fine now on my Windows 10 laptop.
#include <boost/asio/io_service.hpp>
#include <boost/asio/steady_timer.hpp>
#include <chrono>
#include <iostream>
using namespace boost::asio;
int main()
{
io_service ioservice;
steady_timer timer{ioservice, std::chrono::seconds{3}};
timer.async_wait([](const boost::system::error_code &ec)
{ std::cout << "3 sec\n"; });
ioservice.run();
}

Compiling SDL2 Error in Terminal (Confused about how to include SDL2 library)

I'm learning SDL2 right now and I'm working in XCode for the most part as I code. However, I want to run my program in terminal to use valgrind, but whenever I try, I get this error:
fatal error: 'SDL2/SDL.h' file not found
I'm compiling in a really simple way as follows:
g++ Camera.cpp LTexture.cpp LTimer.cpp LWindow.cpp Player.cpp Tile.cpp TileMap.cpp main.cpp -o main
I know that I'm supposed to be including the SDL2 library somehow, but I'm unsure of how to do that. Right now, the framework is sitting in the folder /Library/Frameworks/SDL2.framework
Any help on this would be greatly appreciated. Thanks in advance.
Add
-I/Library/Frameworks/SDL2.frameworks/Headers
to your g++ command and change
'SDL2/SDL.h'
include to just
'SDL.h'
in your header/(s) since /Library/Frameworks/SDL2.frameworks/Headers doesn't contain an SDL2 folder (usually)
Now that you changed the #include "SDL2/SDL.h" to #include "SDL.h" you want to go open your Xcode project, select your target, go to build settings and add /Library/Frameworks/SDL2.frameworks/Headers to Header Search Paths in order to fix Xcode's 'Couldn't find 'SDL.h'' error message.
Update:
Another way would be linking (using ln) /Library/Frameworks/SDL2.frameworks/Headers to /usr/local/include/SDL2, add -I/usr/local/include/SDL2 to your g++ command
and leave the #include as is ('SDL2/SDL.h')

Getting the MySQL C API working with MinGW

It's been days searching for a way to get the MySQL C API working with a MinGW compiler, and frankly I have to say this is getting very frustrating.
I'm trying to compile the minimal example:
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv)
{
printf("MySQL client version: %s\n", mysql_get_client_info());
exit(0);
}
and I keep on getting:
[Linker Error] undefined reference to `mysql_get_client_info#0'
I have compiled the libraries (including libmysql.a which contains mysql_get_client_info) and properly linked them, included all include and lib directories properly. Basiacly, everything is done by the letter according not only to the tutorials but also to forum posts which dealt specifically with this problem.
Now, what am I doing wrong?
Thanks
Having looked at your make-file, it would appear you've forgotten the mysql flags, required for your code to compile. On linux, you'd have to include the output of mysql_config --cflags --libs in the make file.
Add those to your makefile and the code should compile just fine.

how do i get rid of these compiler errors in glu.h?

trying to use this tutorial on 64-bit windows 8 with netbeans and cygwin 4.8.1.
i get many errors like this: /usr/include/w32api/GL/glu.h:68:79: error: expected ‘)’ before ‘*’ token.
on statements like this: void APIENTRY gluQuadricCallback(GLUquadric *qobj,GLenum which,void (CALLBACK *fn)());
the pointer on error message points to the * before the fn().
edit: including windef.h gets rid of the compiler error messages.
i am left with a bunch of undefined references like: glfwInit
edit2: using André Fischer's ideas, i can get a clean compile (you need to add the directory and a -l option for the linker).
i now have a: skipping incompatible ../../../../../Windows/SysWOW64/opengl32.dll when searching for -lopengl32 and: undefined reference to `_imp_vsnprintf'. so it looks like i have a 32/64 bit problems and an undefined external.
there must be a saner way to get opengl working on windows.
I assume you mean Tutorial 1: Opening a Window and are using Netbeans' builtin build system instead of CMake.
The order in which you include the header files is important (source). Try it like this:
#include <windef.h> // According to comments above
#include <GL/glew.h> // Before any gl headers
#include <GL/gl.h>
//#include <GL/glext.h> // Linux headers
//#include <GL/wglext.h> // Windows headers - Not sure which ones cygwin needs. Just try it
#include <GL/glu.h> // Always after gl.h
#include <GL/glfw.h> // When all gl-headers have been included
Create a directory named "include" in your project directory with a subfolder "GL".
Grab the binaries (32 bit, MinGW) from the GLFW Download Site and put the .dll/.so into your build-folder (Or extract them somewhere and add them to the search directories) and the header files into "include/GL".
Also the glfw code in the tutorial is slightly outdated; It does not work with glfw3 anymore.
You'll have to update it using GLFW's conversion guide/try this version (which I haven't been able to test, since I'm currently not at home) or use glfw2.
Finally download the GLEW sources and build it by following the instructions in the README.txt. Put the .dll/.so into your build-folder (or add to search directories) and the header files into "include/GL".
Add following to your Compiler-Flags:
-Iinclude/
Finally add following arguments to your Linker:
-L/lib -lglu32 -lopengl32 -lGL -lGLU -lglfw -lglew
You should be able to compile the tutorial now.
Edit: Added instructions for building GLEW, GLFW and completed my answer to include building everything from scratch.
Edit2: Linked glfw3-version of the tutorial-code.
Edit3: Added missing linker options.