Compilation issue while using Folly Futures - c++

#include <folly/futures/Future.h>
#include<iostream>
using namespace folly;
using namespace std;
void foo(int x)
{
// do something with x
cout << "foo(" << x << ")" << endl;
}
// ...
int main()
{
cout << "making Promise" << endl;
Promise<int> p;
Future<int> f = p.getFuture();
return 0;
}
LD_LIBRARY_PATH is set to /usr/local/lib
When I compile, using
g++ -std=c++11 sample.cpp -pthread -llzma -lz -lsnappy -llz4 -liberty \
-ljemalloc -levent -ldouble-conversion -lssl -lgflags -lglog -lboost_system
I get this error message:
/tmp/ccuDCM19.o: In function `main':
sample.cpp:(.text+0x1a7): undefined reference to `folly::Future<int>::~Future()'
/tmp/ccuDCM19.o: In function `folly::RequestContext::setContext(std::shared_ptr<folly::RequestContext>)':
sample.cpp:(.text._ZN5folly14RequestContext10setContextESt10shared_ptrIS0_E[_ZN5folly14RequestContext10setContextESt10shared_ptrIS0_E]+0x11): undefined reference to `folly::RequestContext::getStaticContext()'
/tmp/ccuDCM19.o: In function `folly::Promise<int>::getFuture()':
sample.cpp:(.text._ZN5folly7PromiseIiE9getFutureEv[_ZN5folly7PromiseIiE9getFutureEv]+0x36): undefined reference to `folly::Future<int>::Future(folly::detail::Core<int>*)'
collect2: error: ld returned 1 exit status

Issue got resolved by adding -lfolly.

Related

Issue with boost library linking in c++

problem with implementing the time based event "validate", like wait methode takes the current time and time after 1 hour and sleep for 1 hour then calling this validate function.
but when i run this code it shows error
boost library version is: 1.71
In main.cpp i am trying to make the two touch file and clock and synchronize, then implement the get_latest_clock_entry which print the last modified time of the clock file. and then wait function waits for 1 hour and run the validate function. wait function should invokes the validates methode every hour.
main.cpp
#include <iostream>
#include <fstream>
#include <time.h>
#include <sys/stat.h>
#include <boost/date_time.hpp>
#include <boost/thread/thread.hpp>
using namespace std;
constexpr char clock_file[] = "/home/saman/Desktop/clock";
constexpr char sync_file[] = "/home/saman/Desktop/synchronized";
class TimeEvent {
public:
void receive_time_event(timespec& time) {
// Create clock file
ofstream clockFile(clock_file);
if (!clockFile.is_open()) {
cout << "Failed to open file" << endl;
}
clockFile.close();
// Create synchronized file
ofstream synchronizedFile(sync_file);
if (!synchronizedFile.is_open()) {
cout << "Failed to open file" << endl;
}
synchronizedFile.close();
}
timespec get_latest_clock_entry(){
string clockFilePath = clock_file;
struct stat fileStat;
if(stat(clockFilePath.c_str(), &fileStat) == 0){
time_t mtime = fileStat.st_mtime;
timespec time;
time.tv_sec = mtime;
time.tv_nsec = 0;
char timeString[50];
strftime(timeString, sizeof(timeString), "%Y-%m-%d %H:%M:%S", gmtime(&time.tv_sec));
cout << "Last modified time "<< clockFilePath << " : " << timeString << endl;
return time;
}
else{
cout << "Can't retrieve last modified time " << clockFilePath << endl;
return {0,0};
}
}
void wait(){
boost::posix_time::ptime timeLocal = boost::posix_time::second_clock::local_time();
cout << "Now Time" << timeLocal << endl;
boost::posix_time::ptime counter = timeLocal + boost::posix_time::hours(1);
cout << "update Time" << timeLocal << endl;
boost::this_thread::sleep(counter);
}
void validate(){
cout << "hi" << endl;
}
};
int main() {
TimeEvent timeEvent;
timespec time;
timespec_get(&time, TIME_UTC);
timeEvent.receive_time_event(time);
auto lastModifiedTime = timeEvent.get_latest_clock_entry();
while(1){
timeEvent.wait();
timeEvent.validate();
}
return 0;
}
Error:
/usr/bin/ld: /tmp/ccJEJpvk.o: in function `boost::condition_variable::condition_variable()':
2.cpp:(.text._ZN5boost18condition_variableC2Ev[_ZN5boost18condition_variableC5Ev]+0xc5): undefined reference to `pthread_condattr_setclock'
/usr/bin/ld: /tmp/ccJEJpvk.o: in function `boost::detail::interruption_checker::interruption_checker(pthread_mutex_t*, pthread_cond_t*)':
2.cpp:(.text._ZN5boost6detail20interruption_checkerC2EP15pthread_mutex_tP14pthread_cond_t[_ZN5boost6detail20interruption_checkerC5EP15pthread_mutex_tP14pthread_cond_t]+0x29): undefined reference to `boost::detail::get_current_thread_data()'
/usr/bin/ld: /tmp/ccJEJpvk.o: in function `boost::condition_variable::do_wait_until(boost::unique_lock<boost::mutex>&, boost::detail::mono_platform_timepoint const&)':
2.cpp:(.text._ZN5boost18condition_variable13do_wait_untilERNS_11unique_lockINS_5mutexEEERKNS_6detail23mono_platform_timepointE[_ZN5boost18condition_variable13do_wait_untilERNS_11unique_lockINS_5mutexEEERKNS_6detail23mono_platform_timepointE]+0x135): undefined reference to `boost::this_thread::interruption_point()'
collect2: error: ld returned 1 exit status
Update:
after compiling with g++ -o main main.cpp -lpthread
/usr/bin/ld: /tmp/cccOLyt1.o: in function `boost::detail::interruption_checker::interruption_checker(pthread_mutex_t*, pthread_cond_t*)':
2.cpp:(.text._ZN5boost6detail20interruption_checkerC2EP15pthread_mutex_tP14pthread_cond_t[_ZN5boost6detail20interruption_checkerC5EP15pthread_mutex_tP14pthread_cond_t]+0x29): undefined reference to `boost::detail::get_current_thread_data()'
/usr/bin/ld: /tmp/cccOLyt1.o: in function `boost::condition_variable::do_wait_until(boost::unique_lock<boost::mutex>&, boost::detail::mono_platform_timepoint const&)':
2.cpp:(.text._ZN5boost18condition_variable13do_wait_untilERNS_11unique_lockINS_5mutexEEERKNS_6detail23mono_platform_timepointE[_ZN5boost18condition_variable13do_wait_untilERNS_11unique_lockINS_5mutexEEERKNS_6detail23mono_platform_timepointE]+0x135): undefined reference to `boost::this_thread::interruption_point()'
collect2: error: ld returned 1 exit status
You need to link against both boost_thread and pthread.
$ g++ boost.cpp -o boost -lboost_thread -lpthread
$ ldd ./boost
linux-vdso.so.1 (0x00007ffc1bd79000)
libboost_thread.so.1.71.0 => /lib/x86_64-linux-gnu/libboost_thread.so.1.71.0 (0x00007fc4b7a51000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc4b7a2e000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fc4b784c000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fc4b7831000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc4b763f000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc4b7aee000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fc4b74f0000)
$ ./boost
Failed to open file
Failed to open file
Can't retrieve last modified time /home/saman/Desktop/clock
Now Time2023-Jan-19 03:43:47
update Time2023-Jan-19 03:43:47
hi
FYI first you have to find what is missing, in this case boost::detail::get_current_thread_data.
Use the line below
$ find /usr/lib -name 'libboost*' | xargs -i /bin/bash -x -c "nm -C {}" | grep boost::detail::get_current_thread_data
Then look for the line that has the status "T" after the address. The library above it is the one that you need to include.
+ nm -C /usr/lib/x86_64-linux-gnu/libboost_thread.a
0000000000000210 T boost::detail::get_current_thread_data()
You need to link with Boost.Thread library. For example, if you're using clang or gcc, add -lboost_thread to your compiler command line.

Eclipse issue linking sqlite3 library

Installed https://www.mingw-w64.org/ and added the libs but still getting this error:
Info: Internal Builder is used for build
g++ -std=c++11 -O3 -Wall -c -fmessage-length=0 -o main.o "..\\main.cpp"
g++ -std=c++11 "-LF:\\MinGW\\lib" -o test.exe main.o -lpsapi -lpthread -lm -ldl
main.o:main.cpp:(.text.startup+0x1f): undefined reference to `sqlite3_open'
main.o:main.cpp:(.text.startup+0x36): undefined reference to `sqlite3_errmsg'
f:/mingw/bin/../lib/gcc/i686-w64-mingw32/4.8.3/../../../../i686-w64-mingw32/bin/ld.exe: main.o: bad reloc address 0x36 in section `.text.startup'
collect2.exe: error: ld returned 1 exit status
Used the installer and choosed "Native Windows" + "i686".
The code:
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
#include <sqlite3.h>
sqlite3 *db;
int main() {
int open = sqlite3_open("test.db", &db);
if( open ) {
cerr << "Can't open database: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
exit(1);
}
return 0;
}

SFML Networking Error: undefined references des when linking

I recently started learning SFML, but when I got to the section of Networking, I always get a lot of errors:
obj\Release\main.o:main.cpp:(.text$_ZN2sf11TcpListenerD1Ev[__ZN2sf11TcpListenerD1Ev]+0x1): undefined reference to `_imp___ZTVN2sf11TcpListenerE'
obj\Release\main.o:main.cpp:(.text$_ZN2sf11TcpListenerD1Ev[__ZN2sf11TcpListenerD1Ev]+0xb): undefined reference to `_imp___ZN2sf6SocketD2Ev'
obj\Release\main.o:main.cpp:(.text$_ZN2sf9TcpSocketD1Ev[__ZN2sf9TcpSocketD1Ev]+0x1): undefined reference to `_imp___ZTVN2sf9TcpSocketE'
obj\Release\main.o:main.cpp:(.text$_ZN2sf9TcpSocketD1Ev[__ZN2sf9TcpSocketD1Ev]+0x25): undefined reference to `_imp___ZN2sf6SocketD2Ev'
obj\Release\main.o:main.cpp:(.text$_ZN2sf9TcpSocketD1Ev[__ZN2sf9TcpSocketD1Ev]+0x2f): undefined reference to `_imp___ZN2sf6SocketD2Ev'
obj\Release\main.o:main.cpp:(.text$_ZN2sf11TcpListenerD0Ev[__ZN2sf11TcpListenerD0Ev]+0x7): undefined reference to `_imp___ZTVN2sf11TcpListenerE'
obj\Release\main.o:main.cpp:(.text$_ZN2sf11TcpListenerD0Ev[__ZN2sf11TcpListenerD0Ev]+0x12): undefined reference to `_imp___ZN2sf6SocketD2Ev'
obj\Release\main.o:main.cpp:(.text$_ZN2sf9TcpSocketD0Ev[__ZN2sf9TcpSocketD0Ev]+0x7): undefined reference to `_imp___ZTVN2sf9TcpSocketE'
obj\Release\main.o:main.cpp:(.text$_ZN2sf9TcpSocketD0Ev[__ZN2sf9TcpSocketD0Ev]+0x23): undefined reference to `_imp___ZN2sf6SocketD2Ev'
obj\Release\main.o:main.cpp:(.text.startup+0x60): undefined reference to `_imp___ZN2sf9IpAddress15getLocalAddressEv'
obj\Release\main.o:main.cpp:(.text.startup+0x72): undefined reference to `_imp___ZN2sf9TcpSocketC1Ev'
obj\Release\main.o:main.cpp:(.text.startup+0x129): undefined reference to `_imp___ZN2sf9TcpSocket4sendEPKvj'
obj\Release\main.o:main.cpp:(.text.startup+0x159): undefined reference to `_imp___ZN2sf9TcpSocket7receiveEPvjRj'
obj\Release\main.o:main.cpp:(.text.startup+0x1f7): undefined reference to `_imp___ZN2sf9TcpSocket7connectERKNS_9IpAddressEtNS_4TimeE'
obj\Release\main.o:main.cpp:(.text.startup+0x226): undefined reference to `_imp___ZN2sf11TcpListenerC1Ev'
obj\Release\main.o:main.cpp:(.text.startup+0x243): undefined reference to `_imp___ZN2sf11TcpListener6listenEt'
obj\Release\main.o:main.cpp:(.text.startup+0x259): undefined reference to `_imp___ZN2sf11TcpListener6acceptERNS_9TcpSocketE'
I'm using CodeBlocks and the compiler arguments are:
mingw32-g++.exe -LC:\Users\Justin\AppData\Roaming\CodeBlocks\SFML-2.2\lib -o "bin\Release\SFML Test.exe" obj\Release\main.o -s -lsfml-graphics -lsfml-window -lsfml-system
This only happens when I use Networking features, nothing else (but I haven't tried audio).
Thanks in advance.
Edit: Here's my code:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
using namespace sf;
#include <iostream>
#include <string>
#define cout std::cout
#define endl std::endl
int main(){
IpAddress ip = IpAddress::getLocalAddress();
TcpSocket socket;
char connectionType, mode;
char buffer[2000];
std::size_t recieved;
std::string text = "Connected to: ";
cout << "Enter (s) for server; Enter (c) for client: ";
std::cin >> connectionType;
if(connectionType == 's'){
TcpListener listener;
listener.listen(123456);
listener.accept(socket);
text += "Server";
}else if(connectionType == 'c'){
socket.connect(ip, 123456);
text += "Client";
}else{
cout << "ERROR: Wrong Connection Type";
}
socket.send(text.c_str(), text.length());
socket.receive(buffer, sizeof(buffer), recieved);
cout << buffer << endl;
system("pause");
}
Link with networking module:
mingw32-g++.exe -LC:\Users\Justin\AppData\Roaming\CodeBlocks\SFML-2.2\lib -o "bin\Release\SFML Test.exe" obj\Release\main.o -s -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network

g++ compilation error

So i've got this very basic OOP example and i want to compile it in Xubuntu but I get errors
the CThermo.h file
class CThermo
{
public:
void SetTemp(int newTemp);
int ReturnTemp();
void ChangeTemp(int deltaTemp);
private:
int m_temp;
};
the CThermo.cpp file
#incude "CThermo.h"
void CThermo::SetTemp(int newTemp)
{
m_temp = newTemp;
}
int CThermo::ReturnTemp()
{
return m_temp;
}
void CThermo::ChangeTemp(int deltaTemp)
{
m_temp += deltaTemp;
}
the main.cpp file
#include "CThermo.h"
#include <iostream>
using std::cout;
int main()
{
CThermo roomTemp;
roomTemp.SetTemp(20);
cout << "the temp is : "<< roomTemp.ReturnTemp() << "\n";
roomTemp.ChangeTemp(5);
cout << "after changing the temp, the room temp is : " << roomTemp.ReturnTemp();
cout << "test";
return 0;
}
the command to compile is "g++ main.cpp -o Main"
and this are the errors I get
/tmp/ccXajxEY.o: In function `main':
main.cpp:(.text+0x1a): undefined reference to `CThermo::SetTemp(int)'
main.cpp:(.text+0x26): undefined reference to `CThermo::ReturnTemp()'
main.cpp:(.text+0x6c): undefined reference to `CThermo::ChangeTemp(int)'
main.cpp:(.text+0x78): undefined reference to `CThermo::ReturnTemp()'
collect2: error: ld returned 1 exit status
You have to compile both main.cpp and CThermo.cpp using:
g++ CThermo.cpp main.cpp -o Main

qt mysql connection error

i am testing out a database connection on QT but i get this error
[root#server env]# make
g++ -c -m64 -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../../../lib64/qt4/mkspecs/linux-g++-64 -I. -I../../../include/QtCore -I../../../include/QtGui -I../../../include -I. -I. -o test.o test.cpp
test.cpp: In function ‘int main()’:
test.cpp:20: error: cannot convert ‘QSqlDatabase’ to ‘int’ in return
test.cpp: At global scope:
test.cpp:23: error: ‘Database’ has not been declared
make: *** [test.o] Error 1
this is the code:
#include <stdlib.h>
#include <iostream>
#include "/usr/include/mysql/mysql.h"
#include <QDebug>
#include <QtSql/QtSql>
using namespace std;
int main() {
cout << "SHELL = " << getenv("SHELL") << endl;
cout << "SHELL = " << getenv("TERM") << endl;
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("xxx.xxxx.xxxx.xxx");
db.setDatabaseName("SAMS");
db.setUserName("xxxxxxxx");
db.setPassword("xxxxxxxxxx");
if (!db.open()) qDebug() << "Failed to connect to mysql as user testuser";
return db;
}
void Database::closeDb(){
QSqlDatabase::removeDatabase("QMYSQL");
}
any idea???
UPDATE:
#include <stdlib.h>
#include <iostream>
#include "/usr/include/mysql/mysql.h"
#include <QDebug>
#include <QtSql/QtSql>
using namespace std;
void conn() {
cout << "SHELL = " << getenv("SHELL") << endl;
cout << "SHELL = " << getenv("TERM") << endl;
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("xxxxxxxx");
db.setDatabaseName("xxxxxxxx");
db.setUserName("xxxxx");
db.setPassword("xxxxxxxxxx");
if (!db.open()) qDebug() << "Failed to connect to mysql as user testuser";
}
void closeDb(){
QSqlDatabase::removeDatabase("QMYSQL");
}
[root#server env]# make
g++ -c -m64 -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../../../lib64/qt4/mkspecs/linux-g++-64 -I. -I../../../include/QtCore -I../../../include/QtGui -I../../../include -I. -I. -o test.o test.cpp
g++ -m64 -Wl,-O1 -o env test.o -lQtGui -lQtCore -lpthread
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
test.o: In function `closeDb()':
/usr/local/dev/env/test.cpp:23: undefined reference to `QSqlDatabase::removeDatabase(QString const&)'
test.o: In function `QString':
/usr/include/QtCore/qstring.h:683: undefined reference to `QSqlDatabase::defaultConnection'
test.o: In function `conn()':
/usr/local/dev/env/test.cpp:14: undefined reference to `QSqlDatabase::addDatabase(QString const&, QString const&)'
/usr/local/dev/env/test.cpp:15: undefined reference to `QSqlDatabase::setHostName(QString const&)'
/usr/local/dev/env/test.cpp:16: undefined reference to `QSqlDatabase::setDatabaseName(QString const&)'
/usr/local/dev/env/test.cpp:17: undefined reference to `QSqlDatabase::setUserName(QString const&)'
/usr/local/dev/env/test.cpp:18: undefined reference to `QSqlDatabase::setPassword(QString const&)'
/usr/local/dev/env/test.cpp:19: undefined reference to `QSqlDatabase::open()'
/usr/local/dev/env/test.cpp:19: undefined reference to `QSqlDatabase::~QSqlDatabase()'
/usr/local/dev/env/test.cpp:19: undefined reference to `QSqlDatabase::~QSqlDatabase()'
/usr/local/dev/env/test.cpp:14: undefined reference to `QSqlDatabase::~QSqlDatabase()'
collect2: ld returned 1 exit status
make: *** [env] Error 1
update:
this worked..
#include <stdlib.h>
#include <iostream>
#include "/usr/include/mysql/mysql.h"
#include <QDebug>
#include <QtSql/QtSql>
using namespace std;
void conn() {
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("xxxxxxxxxxx");
db.setDatabaseName("SAMS");
db.setUserName("xxxxxxxx");
db.setPassword("xxxxxxxx");
if (!db.open()) {
qDebug() << "Failed to connect to mysql as user testuser";
}
else {
qDebug() << "hello";
}
}
void closeDb() {
QSqlDatabase::removeDatabase("QMYSQL");
}
int main() {
cout << "SHELL = " << getenv("SHELL") << endl;
cout << "SHELL = " << getenv("TERM") << endl;
conn();
return 0;
}
i had to add
QT += core gui sql
to the project file..
You are returning your QSqlDatabase in your int main() function and this is invalid because QSqlDatabase is not int. Why are you returning it? If you want the return type to describe success then use -1 and otherwise 0, for instance.
As for the second error, you haven't any class defined called Database so you can't define the method Database::closeDb(). Instead create a function to do what you want:
void closeDb() {
QSqlDatabase::removeDatabase("QMYSQL");
}
But I don't see the purpose of closeDb() when you are not using it anyway.