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.
Related
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;
}
#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.
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
I have three files:
Two .cpp files (Reader.cpp and algo.cpp) and one header file (algo.h).
In Reader.cpp file, its function PacketHandler calls a function present in algo.cpp.
Reader.cpp
void PacketHandler(Packet* sniff_packet, void* user) {
std::string payload;
RawLayer* raw_payload = sniff_packet->GetLayer<RawLayer>();
if (raw_payload) {
/* Summarize some data */
cout << "[+] ------- [+]" << endl;
TCP* tcp_layer = sniff_packet->GetLayer<TCP>();
cout << "[#] TCP packet from source port: " << dec << tcp_layer->GetSrcPort() << endl;
cout << "[#] With Payload: " << endl;
payload = raw_payload->GetStringPayload();
SPPM(payload); // Function present in algo.cpp
}
}
void main() { // }
algo.h
#ifndef algo_H_
#define algo_H_
#include <string>
using namespace std;
void SPPM (std::string input);
#endif
algo.cpp
#include "algo.h"
#define byte uint8_t
using namespace std;
void SPPM(std::string input){ //definition }
When I compile the code using g++, I get this error:
In function `PacketHandler(Crafter::Packet*, void*)':
/home/maleeha/libcrafter_latest/libcrafter-master/libcrafter/pcap_reader.cpp:32: undefined reference to `SPPM(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'collect2: ld returned 1 exit status
I am executing my code this way:
g++ -c Reader.cpp -o Reader.o -g -lcrafter
g++ -c algo.cpp -o algo.o
g++ -o program Reader.o algo.o -lcrafter
./program Packets.pcap
Why is this error coming?
I'm trying to compile my project in Eclipse.
However, it says that the main() is defined more than once. I grep'd my project dir and it found only one definition of main(), in main.cpp.
Apparently it is somewhere else.maybe a dir I linked to.
The only dirs I linked to are:
-ljson_linux-gcc-4.5.2_libmt
The compiler output is:
make all
Building file: ../src/main.cpp
Invoking: GCC C++ Compiler
g++ -Ijson_linux-gcc-4.5.2_libmt -I/usr/include/mysql -I/usr/include/jsoncpp-src-0.5.0/include -O0 -g3 -Wall -c -fmessage-length=0 -Ijson_linux-gcc-4.5.2_libmt -MMD -MP -MF"src/main.d" -MT"src/main.d" -o"src/main.o" "../src/main.cpp"
Finished building: ../src/main.cpp
Building target: Atms
Invoking: GCC C++ Linker
g++ -L-L/usr/include/jsoncpp-src-0.5.0/include/ -o"Atms" ./src/atmstypes.o ./src/base64.o ./src/hregex.o ./src/libparser.o ./src/log.o ./src/main.o ./src/serv.o ./src/sqlfeeder.o ./src/teleindex.o ./src/telepipe.o ./src/telesharedobject.o ./src/treet.o ./src/ttable.o -l-ljson_linux-gcc-4.5.2_libmt
./src/serv.o: In function `main':
/usr/include/c++/4.4/new:101: multiple definition of `main'
./src/main.o:/home/idan/workspaceCpp/Atms/Debug/../src/main.cpp:12: first defined here
/usr/bin/ld: cannot find -l-ljson_linux-gcc-4.5.2_libmt
collect2: ld returned 1 exit status
make: *** [Atms] Error 1
main.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include <iostream>
#include <string.h>
#include <string>
#include "../h/hregex.h"
using namespace std;
string s = "this and7 that";
int main(int argc,char** argv){
cout << hregex::InitRegex() << endl;
cout << hregex::CheckHostnameField(s)<< "= this and7 that" << endl;
s = "this and7 that";
cout << hregex::CheckURLField(s)<< "= this and7 that" << endl;
s = "/lol/idan.html";
cout << hregex::CheckURLField(s)<< "= /lol/idan.html" << endl;
s = "/lol2#/idan.html";
cout << hregex::CheckURLField(s)<< "= /lol2#/idan.html" << endl;
return 0;
}
How can I prevent the error from appearing?
g++ says serv.o has a main function.
If there actually is no main() it serv.cpp, check the includes, maybe you did a bad #include and included a .cpp instead of a .h ?
As an extra remark :
it tries to bind against the library "-ljson_linux-gcc-4.5.2_libmt"
So there is "-l-ljson_linux-gcc-4.5.2_libmt" in the link command line. Remove the -l in your configuration