I am using SQLPP11 for sql queries and results and SQLPP11-Connector-mysql to establish connection with database.
And compiling my program using
g++ -std=c++1y main.cpp -I ../date -lsqlpp-mysql -lmysqlclient -lboost_system -lpthread
And here is the sample code i am using.
bool db_connection()
{
auto config = std::make_shared<mysql::connection_config>();
config->user = "root";
config->password = "";
config->database = "test";
config->debug = true;
sqlpp::mysql::connection db(config);
try
{
sqlpp::mysql::connection db(config);
std::cout << "Database connection establish...!!\n";
std::cout << "Now executing a very simple select query in table using sqlpp11 \n";
const auto g = changestreet::Goals{};
for(const auto& row : db(select(all_of(g)).from(g).unconditionally()))
{
std::cerr << row.goalId << "\n";
std::cerr << row.goalName << "\n";
std::cerr << row.goalAmount << "\n";
}
}
catch (const sqlpp::exception& e)
{
std::cerr << "No such database exits, you'll need to create it. \n";
std::cerr << e.what() << std::endl;
return false;
}
return true;
}
And the errors are
/tmp/ccxRheKs.o: In function `db_connection_cs()':
main.cpp:(.text+0x39d): undefined reference to `sqlpp::mysql::connection::connection(std::shared_ptr<sqlpp::mysql::connection_config> const&)'
main.cpp:(.text+0x3c8): undefined reference to `sqlpp::mysql::connection::~connection()'
main.cpp:(.text+0x400): undefined reference to `sqlpp::mysql::connection::~connection()'
/tmp/ccxRheKs.o: In function `db_connection_nav()':
main.cpp:(.text+0x4bf): undefined reference to `sqlpp::mysql::connection::connection(std::shared_ptr<sqlpp::mysql::connection_config> const&)'
main.cpp:(.text+0x4ea): undefined reference to `sqlpp::mysql::connection::~connection()'
main.cpp:(.text+0x522): undefined reference to `sqlpp::mysql::connection::~connection()'
/tmp/ccxRheKs.o: In function `sqlpp::mysql::serializer_t::escape(std::string)':
main.cpp:(.text._ZN5sqlpp5mysql12serializer_t6escapeESs[_ZN5sqlpp5mysql12serializer_t6escapeESs]+0x2a): undefined reference to `sqlpp::mysql::connection::escape(std::string const&) const'
/tmp/ccxRheKs.o: In function `sqlpp::result_t<sqlpp::mysql::char_result_t, sqlpp::result_row_t<sqlpp::mysql::connection, sqlpp::field_spec_t<changestreet::Goals_::GoalId::_alias_t, sqlpp::integral, false, false>, sqlpp::field_spec_t<changestreet::Goals_::GoalName::_alias_t, sqlpp::text, true, false>, sqlpp::field_spec_t<changestreet::Goals_::GoalAmount::_alias_t, sqlpp::floating_point, true, false>, sqlpp::field_spec_t<changestreet::Goals_::GoalStartTime::_alias_t, sqlpp::day_point, true, false>, sqlpp::field_spec_t<changestreet::Goals_::GoalEndTime::_alias_t, sqlpp::day_point, true, false>, sqlpp::field_spec_t<changestreet::Goals_::GoalMonthlyContribution::_alias_t, sqlpp::floating_point, true, false>, sqlpp::field_spec_t<changestreet::Goals_::GoalStatus::_alias_t, sqlpp::text, true, false>, sqlpp::field_spec_t<changestreet::Goals_::UsersUserId::_alias_t, sqlpp::integral, true, false> > >::~result_t()':
main.cpp:(.text._ZN5sqlpp8result_tINS_5mysql13char_result_tENS_12result_ro
Here is the build logs of both the libraries on my 64 bit debian machine.
Finally, i have figured out the solution.
It was the problem with g++ version. Recent versions g++-5 and g++-6 have such problems, But when i get back to old g++ version 4.9.2 everything is running smooth.
"undefined reference to" usually means that the linker can not find the required library. Make sure your PATH environment has those libs sqlpp-mysql mysqlclient in scope.
Related
I have a small piece of code which (should) allows me to connect to an MySQL database, here is the code:
#include <iostream>
#include <mariadb/mysql.h> // /usr/includes/mariadb/mysql.h
struct connection_details
{
const char *server, *user, *password, *database;
};
MYSQL* mysql_connection_setup(struct connection_details mysql_details){
MYSQL *connection = mysql_init(NULL); // mysql instance
//connect database
if(!mysql_real_connect(connection, mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)){
std::cout << "Connection Error: " << mysql_error(connection) << std::endl;
exit(1);
}
return connection;
}
// mysql_res = mysql result
MYSQL_RES* mysql_perform_query(MYSQL *connection, const char *sql_query){
//send query to db
if(mysql_query(connection, sql_query)){
std::cout << "MySQL Query Error: " << mysql_error(connection) << std::endl;
exit(1);
}
return mysql_use_result(connection);
}
int main(int argc, char const *argv[])
{
MYSQL *con; // the connection
MYSQL_RES *res; // the results
MYSQL_ROW row; // the results row (line by line)
struct connection_details mysqlD;
mysqlD.server = "localhost"; // where the mysql database is
mysqlD.user = "netser"; // the root user of mysql
mysqlD.password = "root"; // the password of the root user in mysql
mysqlD.database = "mydatabase"; // the databse to pick
// connect to the mysql database
con = mysql_connection_setup(mysqlD);
// assign the results return to the MYSQL_RES pointer
res = mysql_perform_query(con, "show tables");
std::cout << ("MySQL Tables in mysql database:") << std::endl;
while ((row = mysql_fetch_row(res)) !=NULL)
std::cout << row[0] << std::endl;
/* clean up the database result set */
mysql_free_result(res);
/* clean up the database link */
mysql_close(con);
return 0;
}
When I try to compile it using:
g++ connectdb.cpp -o output && ./output
I get the next few errors:
/usr/bin/ld: /tmp/ccDwpmw3.o: in function `mysql_connection_setup(connection_details)':
connectdb.cpp:(.text+0xf): undefined reference to `mysql_init'
/usr/bin/ld: connectdb.cpp:(.text+0x3c): undefined reference to `mysql_real_connect'
/usr/bin/ld: connectdb.cpp:(.text+0x6c): undefined reference to `mysql_error'
/usr/bin/ld: /tmp/ccDwpmw3.o: in function `mysql_perform_query(st_mysql*, char const*)':
connectdb.cpp:(.text+0xc4): undefined reference to `mysql_query'
/usr/bin/ld: connectdb.cpp:(.text+0xef): undefined reference to `mysql_error'
/usr/bin/ld: connectdb.cpp:(.text+0x125): undefined reference to `mysql_use_result'
/usr/bin/ld: /tmp/ccDwpmw3.o: in function `main':
connectdb.cpp:(.text+0x1ca): undefined reference to `mysql_fetch_row'
/usr/bin/ld: connectdb.cpp:(.text+0x213): undefined reference to `mysql_free_result'
/usr/bin/ld: connectdb.cpp:(.text+0x21f): undefined reference to `mysql_close'
{By the way, I use Parrot OS with Mariadb and use VSCode}
I have never worked with databases in C++ before, so I have barely any idea what could be the problem, but I can assure you that the database does exit and that the connection to mysql.h has no problems...
The problem was how I was executing it, I had to use
g++ connectdb.cpp -o output -L/usr/include/mariadb/mysql -lmariadbclient
And not
g++ connectdb.cpp -o output
For future people with the same problem, here's a breakdown:
g++ connectdb.cpp -o output (normal compile)
-L/usr/include/mariadb/mysql (depending on what OS you have and if you use mariadb, you may have to change this to something like -L/usr/include/mysql instead, if you do use mariadb and still have a problem, remember to apt-get install libmariadb-dev)
-lmariadbclient (You have to apt-get install mariadb... If you're using MySQL, use -lmysqlclient instead)
So I have OpenCV libraries downloaded from here
https://sourceforge.net/projects/opencvlibrary/files/3.4.6/opencv-3.4.6-vc14_vc15.exe/download
CMake gui with cmakelist.txt like this
cmake_minimum_required(VERSION 2.8)
project(Display)
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library
status:") message(STATUS " config:
${OpenCV_DIR}") message(STATUS "
version: ${OpenCV_VERSION}")
message(STATUS " libraries:
${OpenCV_LIBS}") message(STATUS "
include path: ${OpenCV_INCLUDE_DIRS}")
add_executable(Display
DisplayImage.cpp)
target_link_libraries(Display
${OpenCV_LIBS})
and have a simple cpp like this
#include <stdio.h>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv )
{
// cout << "You have entered " << argc
// << " arguments:" << "\n";
// for (int i = 0; i < argc; ++i)
// cout << argv[i] << "\n";
// cout << "a" << argv[1];
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
printf("Hello World!");
return 0;
}
already configure it with cmake-gui and this shows up
OpenCV library status:
config: D:/opencv/build/x64/vc15/lib
version: 3.4.6
libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_world
include path: D:/opencv/build/include;D:/opencv/build/include/opencv;D:/opencv/build/include/opencv2
Configuring done Generating done
seems good right ? and then on the build directory from command prompt, i typed mingw32-make
and this shows up
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text+0x72):
undefined reference to
cv::imread(cv::String const&, int)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text+0xe3):
undefined reference to
cv::namedWindow(cv::String const&,
int)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text+0x129):
undefined reference to
cv::imshow(cv::String const&,
cv::_InputArray const&)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text+0x149):
undefined reference to
cv::waitKey(int)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv6StringC1EPKc[__ZN2cv6StringC1EPKc]+0x42):
undefined reference to
cv::String::allocate(unsigned int)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv6StringD1Ev[__ZN2cv6StringD1Ev]+0xf):
undefined reference to
cv::String::deallocate()'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$ZN2cv6StringaSERKS0[__ZN2cv6StringaSERKS0_]+0x1c):
undefined reference to
cv::String::deallocate()'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]+0x2d): undefined reference to
cv::fastFree(void*)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv3Mat7releaseEv[__ZN2cv3Mat7releaseEv]+0x40):
undefined reference to
cv::Mat::deallocate()'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv3MataSEOS0_[__ZN2cv3MataSEOS0_]+0xb4):
undefined reference to
cv::fastFree(void*)' collect2.exe:
error: ld returned 1 exit status
mingw32-make[2]: *
[CMakeFiles\Display.dir\build.make:104:
Display.exe] Error 1 mingw32-make[1]:
* [CMakeFiles\Makefile2:72: CMakeFiles/Display.dir/all] Error 2
mingw32-make: *** [Makefile:83: all]
Error 2
how do i fix this ? or is it because that i use the library straight from the website ?
i also try something like putting a wrong parameter on the imread function and then type mingw32-make and it did tell me that i use a wrong parameter. If they know i used the wrong parameter, then why they can't tell what is imread for (or why is it undefined reference ? ) ? I'm using windows.
I'm very new to these libraries, mingw, and cmake, so i'm sorry if my question is stupid.
this question is not duplicate because, i use windows and mingw32-make, everyone that asks this question did not use that and the question is in a different situation than mine.
I want to verify the leveldb installation. I have a main function only containing levelDB::DB::Open() function. I installed both snappy and leveldb by using brew install. I have boost 1.67 installed too. I have GCC 8.1. I am running macOS 10.13.5.
my source file looks like this:
int main(void) {
leveldb::DB *db;
leveldb::Options options;
options.create_if_missing = true;
auto err = leveldb::DB::Open(options, "../tmpDB", &db);
if (err.ok()) {
std::cout << "success" << std::endl;
}else {
std::cout << "failed" << std::endl;
}
delete db;
return 0;
}
I compile my code I use g++ main.cpp -lleveldb -lsnappy -o test. The compiler generate the error like:
Undefined symbols for architecture x86_64:
"leveldb::DB::Open(leveldb::Options const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, leveldb::DB**)", referenced from:
_main in ccYcksfh.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
How can I solve this link error?
You can install leveldb with compiler version.
brew reinstall leveldb --cc=gcc-4.8
Can't link against leveldb on OSX
I am successfully running opencv python code on raspberry pi 2 (raspbian).
Now I want to try to compile opencv C++ code on raspberry pi 2 by using this command:
g++ -std=c++0x test_colour_tracking_1.cpp -otest_colour
The C++ coding as below.
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cap(0); //capture the video from web cam
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
while (true)
{
Mat imgOriginal;
bool bSuccess = cap.read(imgOriginal); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
imshow("image",imgOriginal);
}
return 0;
}
But it show error as below.
/tmp/ccHcCqSm.o: In function `main':
test_colour_tracking_1.cpp:(.text+0x70): undefined reference to `cv::VideoCapture::VideoCapture(int)'
test_colour_tracking_1.cpp:(.text+0x7c): undefined reference to `cv::VideoCapture::isOpened() const'
test_colour_tracking_1.cpp:(.text+0xd8): undefined reference to `cv::VideoCapture::read(cv::Mat&)'
test_colour_tracking_1.cpp:(.text+0x150): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
test_colour_tracking_1.cpp:(.text+0x164): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
test_colour_tracking_1.cpp:(.text+0x1a4): undefined reference to `cv::VideoCapture::~VideoCapture()'
test_colour_tracking_1.cpp:(.text+0x1f0): undefined reference to `cv::VideoCapture::~VideoCapture()'
/tmp/ccHcCqSm.o: In function `cv::Mat::~Mat()':
test_colour_tracking_1.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3c): undefined reference to `cv::fastFree(void*)'
/tmp/ccHcCqSm.o: In function `cv::Mat::release()':
test_colour_tracking_1.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x58): undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
And I want to ask how to check frame rate per second?
Try to use the following command:
g++ -std=c++0x test_colour_tracking_1.cpp -o test_colour `pkg-config --cflags --libs opencv`
I'm trying to compile a simple test program using the Trilino package but something is wrong. The install of Trilino have went fine as far as I know but there must be something wrong with the linking or something. Below is my makefile:
include /home/jacob/Trilinos/trilinos-build/Makefile.export.Trilinos
CXX=$(Trilinos_CXX_COMPILER)
CC=$(Trilinos_C_COMPILER)
FORT=/usr/bin/gfortran
CXX_FLAGS=$(Trilinos_CXX_COMPILER_FLAGS) $(USER_CXX_FLAGS)
C_FLAGS=$(Trilinos_C_COMPILER_FLAGS) $(USERC_FLAGS)
FORT_FLAGS=$(Trilinos_Fortran_COMPILER_FLAGS) $(USER_FORT_FLAGS)
INCLUDE_DIRS=$(Trilinos_INCLUDE_DIRS) $(Trilinos_TPL_INCLUDE_DIRS)
LIBRARY_DIRS=$(Trilinos_LIBRARY_DIRS) $(Trilinos_TPL_LIBRARY_DIRS)
LIBRARIES=$(Trilinos_LIBRARIES) $(Trilinos_TPL_LIBRARIES)
LINK_FLAGS=$(Trilinos_EXTRA_LD_FLAGS)
DEFINES=-DMYAPP_EPETRA
default: print_info vector.x
vector.x: libmyappLib.a
$(CXX) $(CXX_FLAGS) libMyappLib.a -o vector.x $(LINK_FLAGS) $(INCLUDE_DIRS) $(DEFINES) $(LIBRARY_DIRS) $(LIBRARIES)
libmyappLib.a: es.o
$(Trilinos_AR) cr libMyappLib.a es.o
es.o:
$(CXX) -c $(CXX_FLAGS) $(INCLUDE_DIRS) $(DEFINES) es.cpp
And here is the program:
#include "Epetra_SerialComm.h"
#include "Epetra_Map.h"
#include "Epetra_Vector.h"
#include "Epetra_Version.h"
int main(int argc, char *argv[])
{
std::cout << Epetra_Version() << std::endl << std::endl;
Epetra_SerialComm Comm;
int NumElements = 1000;
Epetra_Map Map(NumElements, 0, Comm);
Epetra_Vector x(Map);
Epetra_Vector b(Map);
b.Random();
x.Update(2.0, b, 0.0); // x = 2*b
double bnorm, xnorm;
x.Norm2(&xnorm);
b.Norm2(&bnorm);
std::cout << "2 norm of x = " << xnorm << std::endl
<< "2 norm of b = " << bnorm << std::endl;
return 0;
}
However, when running make it just gives back:
es.cpp:(.text.startup+0x91): undefined reference to Epetra_SerialComm::Epetra_SerialComm()
es.cpp:(.text.startup+0xa7): undefined reference to `Epetra_Map::Epetra_Map(int,int, Epetra_Comm const&)'
es.cpp:(.text.startup+0xbb): undefined reference to `Epetra_Vector::Epetra_Vector(Epetra_BlockMap const&, bool)'
.
.
.
collect2: error: ld returned 1 exit status
make: *** [vector.x] Fel 1
For every piece of code inside main, so it seems like it cannot find the Epetra package even though itÅ› not complaining about the includes. Does anyone have a clue of what might be the problem? I'm fairly new to C++/C and handling the Trilino package is rather complicated so any tip is highly appreciated.