include mysql into cmake - c++

I'm writing a console application in kdevelop (integrated with cmake) in which I want to connect to mysql. I have installed libmysqlclient16-dev. My main.cpp file looks like this:
#include <stdlib.h>
#include <iostream>
#include <mysql/mysql.h>
int main(int argc, char **argv) {
MYSQL *conn_ptr;
conn_ptr = mysql_init(NULL);
if (!conn_ptr) {
std::cout << "mysql init failed\n";
exit(1);
}
conn_ptr = mysql_real_connect (conn_ptr, "localhost", "user", "pass", "db", 0, NULL, 0);
if (conn_ptr) {
std::cout << "connection success\n";
} else {
std::cout << "connection failed\n";
}
mysql_close(conn_ptr);
return 0;
}
and it compiles and works correctly, when I compile it manually:
g++ main.cpp -lmysqlclient -o main
But I want to include it into cmake somehow. The CMakeLists.txt, generated by kdevelop, looks like the following:
project(finances)
add_executable(finances main.cpp)
What should I add to cmake to make it include mysqlclient library?

target_link_libraries(finances mysqlclient)
Seems to work.

target_link_libraries(projectName mysqlclient)
Change projectName with your current project name

Related

log4cplus ConsoleAppender does not work in container

I'm trying to containerize my application but I'm having trouble getting log4cplus working. Bottom line up front, logging works when running on the host, but not in the container when I'm logging from long running loops. Its seems like a buffer somewhere is not getting flushed. A minimal example follows.
Additionally, removing the long lived loop removes the issue, presumably log4cplus flushes one last time before tearing down. Lengthening the sleep duration did not seem to help.
main.cpp
#include <iostream>
#include <unistd.h>
#include <log4cplus/logger.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/configurator.h>
#include <log4cplus/initializer.h>
int main(int argc, char **argv)
{
std::cout << "Sanity Check" << std::endl;
auto log4cplus = ::log4cplus::Initializer();
std::string logconfig("log4cplus_configure.ini");
::log4cplus::PropertyConfigurator::doConfigure(logconfig);
auto logger = ::log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("main"));
while (true) {
LOG4CPLUS_ERROR(logger, "Sleeping...");
// std::cout << "cout sleep..." << std::endl; // uncomment to get log messages working
sleep(1);
}
return 0;
}
log4cplus_configure.ini
log4cplus.rootLogger=INFO, MyConsoleAppender
log4cplus.appender.MyConsoleAppender=log4cplus::ConsoleAppender
log4cplus.appender.MyConsoleAppender.layout=log4cplus::PatternLayout
log4cplus.appender.MyConsoleAppender.layout.ConversionPattern=[%-5p][%d] %m%n
Dockerfile
FROM rockylinux:latest
RUN dnf install -y boost-system
COPY ./build/sandbox /
COPY ./log4cplus_configure.ini /
CMD ["/sandbox"]
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
set (CMAKE_CXX_STANDARD 17)
# Project executable and library
add_executable(sandbox main.cpp)
target_link_libraries(sandbox
PUBLIC liblog4cplus.a
PUBLIC pthread
PUBLIC boost_system
)
Not sure why, but adding log4cplus.appender.MyConsoleAppender.ImmediateFlush=true to log4cplus_configure.ini solved my issue.

Unable to initialise sdl2

Im trying to check if sdl is properly installed on my Ubuntu 20.04 and its not.
Running all on 64bit type.
This is my code:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>
bool initSDL()
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
std::cout << "Failed to init sdl: " << SDL_GetError() << std::endl;
return false;
}
if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG)
{
std::cout << "Failed to init sdl_image: " << IMG_GetError() << std::endl;
return false;
}
return true;
}
int main(int argc, char *argv[])
{
std::cout << "Code Starting..." << std::endl;
initSDL();
int winX = 900, winY = 600;
std::cout << "Code Exited Properly." << std::endl;
return 0;
}
And this is my output:
Code Starting...
Failed to init sdl: No available video device
Code Exited Properly.
Im running my code with this commands:
gcc -c src/*.cpp -I include -m64 -lstdc++ -std=c++11
gcc *.o -o out/main -lSDL2main -lSDL2 -lSDL2_image -m64 -lstdc++ -std=c++11
./out/main
My code strucure is this way:
Main directory 'sdl_hello_world'
->src -> contains 'main.cpp'
->out -> contains compiled 'main'
Some methods i tried was:
export DISPLAY=:0
export SDL_VIDEODRIVER=x11
So how do i fix this ?
EDIT: I also followed this tutorial (https://www.youtube.com/watch?v=P3_xhDIP7bc&list=PLvv0ScY6vfd-p1gSnbQhY7vMe2rng0IL0&ab_channel=MikeShah), and still it gave the same error. So i guess the problem is in some computer setting.
EDIT: SOLVED:
i followed almost all tutorials and fix methods on the internet to solve my issue which uses gcc in linux. Some solutions on stack overflow seemed to prefer to compile the source code on their own computer WHICH WORKED.
So im pretty sure the way ubuntu installs it on my computer/ idk im new to using sdl. Updating this as might come handy to anyone else.
Still curious, is there a chance i did something wrong, or could this be considered a bug ?
EDIT: followed this btw https://wiki.libsdl.org/Installation

cppcms can't work

My OS: Mac OSX
My gcc version: 4.2.1
My clang version: llvm 6.1.0
cppcms version: 1.0.5
I download the cppcms framework and install the framework.The commands:
cd cppcms-1.0.5
mkdir build & cd build
cmake ..
make
make test
make install
No error found.
The I write a cpp file named hello.cpp. The program is like this:
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <iostream>
using namespace std;
class hello : public cppcms::application{
public:
hello(cppcms::service &srv) : cppcms::application(srv){
}
virtual void main(std::string url);
};
void hello::main(std::string /*url*/)
{
response().out() <<
"<html>\n"
"<body>\n"
" <h1>Hello World</h1>\n"
"</body>\n"
"</html>\n";
}
int main(int argc,char ** argv) {
try {
cppcms::service srv(argc,argv);
srv.applications_pool().mount(
cppcms::applications_factory<hello>()
);
}
catch (std::exception const &e){
std::cerr << e.what() << std::endl;
}
return 0;
}
My config.js:
{
"service" : {
"api" : "http",
"port" : 8008
},
"http" : {
"script_names" : [ "/hello" ]
}
}
Compile commands:
c++ hello.cpp -lcppcms -o hello
./hello -c config.js
I visited the url "http://localhost:8008/hello",then web browser show me
"This webpage is not available".
What's wrong? How to fix the problem.
Most importantly you have to start your service after mounting it:
cppcms::service srv(argc,argv);
srv.applications_pool().mount(
cppcms::applications_factory<hello>()
);
srv.run();
I would also include the following two header files (at least on Linux
I got a compilation error otherwise):
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>

SDL 2.0 compiles, but failt to run

Im using the latest SDL 2.0 version on Xubuntu 64-bits. I installed through the provided install script on the source code.
Compiling works well, however when trying to open a font or image (regardless of its extension), it will always fail to open.
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
int main (int argc, char *argvp[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
cout << SDL_GetError() << endl;
}
if (TTF_Init() == -1)
{
std::cout << TTF_GetError() << std::endl;
return 2;
}
TTF_Font *font1 = NULL;
font1 = TTF_OpenFont("SourceSansPro-Regular.ttf", 20);
if (font1 == NULL)
{
std::cout << "ERROR OPENING FONT = " << TTF_GetError() << std::endl;
}
TTF_CloseFont(font1);
SDL_Quit();
return 0;
}
I compiled with
g++ -Wall fontTEST.cpp -o TEST -lSDL2 -lSDL_ttf (NOTE that SDL_ttf installs as such, not as SDL2_ttf)
And get the following error: Failed to load font: 0 Couldn't load font file
This happens with images as well. I've already tried with different fonts and images, apparently it works if I compile with SDL 1.2, just not with 2.0.
Also why does the provided install script installs the lib and include folders in /user/local/?
I moved them to /usr/ but the problem persists.
Remember the following:
On Unix, file paths are case-sensitive
As said in Xonar's comment, tilde '~' expansion is a shell feature, it does not work in C/C++ programs, you should use the real path instead.
The strace log says clearly that something is wrong with the path.
You should try the following:
Rename your font file to "font.ttf"
put it in /home/user/font.ttf
use "/home/user/font.ttf" as the path in your code.

Linking shared library mysql on ubuntu

I've been trying to create a small test application to start working with CMake and linking libraries.
The following is done:
- I installed the libmysqlcppconn5 library on Ubuntu (ie. it is now located in /usr/lib/libmysqlcppconn5.so, just the default installation)
- I created a small .cpp file with the following content
#include <stdlib.h>
#include <iostream>
#include <mysql/mysql.h>
int main(int argc, char **argv) {
MYSQL *conn_ptr;
conn_ptr = mysql_init(NULL);
if (!conn_ptr) {
std::cout << "mysql init failed\n";
exit(1);
}
conn_ptr = mysql_real_connect (conn_ptr, "localhost", "root", "pw", "db", 0, NULL, 0);
if (conn_ptr) {
std::cout << "connection success\n";
} else {
std::cout << "connection failed\n";
}
mysql_close(conn_ptr);
How should I construct my CMakeLists.txt so it will use this shared library?
How about something like:
# Find and make sure the system have the header file
find_path(MYSQL_HEADER mysql/mysql.h)
if(MYSQL_HEADER STREQUAL "MYSQL_HEADER-NOTFOUND")
message(FATAL_ERROR "Could not find the mysql/mysql.h header file")
endif()
# Find and make sure the system have the library
find_library(MYSQL_LIBMYSQLCPPCONN5 mysqlcppconn5)
if(MYSQL_LIBMYSQLCPPCONN5 STREQUAL "MYSQL_LIBMYSQLCPPCONN5-NOTFOUND")
message(FATAL_ERROR "Could not find the mysqlcppconn5 library")
endif()
# Link the library to the target
target_link_libraries(your_target ${MYSQL_LIBMYSQLCPPCONN5})