SFML Networking Error: undefined references des when linking - c++

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

Related

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;
}

Build issue with Crypto++ 5.6.5 using Linux command line g++

I'm trying to run this example utilizing Crypto++library, and I downloaded/stored Crypto++ in /home/thirdparty/cryptopp. However, when run the following command line:
g++ -I/home/thirdParty channelSwitch.cpp -L/home/thirParty -o test-cs.exe
Can someone help check what is the problem?
/tmp/ccPnCG56.o: In function `main':
channelSwitch.cpp:(.text+0x178): undefined reference to `CryptoPP::StringSinkTemplate<std::string>::StringSinkTemplate(std::string&)'
channelSwitch.cpp:(.text+0x21b): undefined reference to `CryptoPP::DEFAULT_CHANNEL'
channelSwitch.cpp:(.text+0x221): undefined reference to `CryptoPP::DEFAULT_CHANNEL'
channelSwitch.cpp:(.text+0x237): undefined reference to `CryptoPP::HashFilter::HashFilter(CryptoPP::HashTransformation&, CryptoPP::BufferedTransformation*, bool, int, std::string const&, std::string const&)'
channelSwitch.cpp:(.text+0x292): undefined reference to `CryptoPP::StringSinkTemplate<std::string>::StringSinkTemplate(std::string&)'
channelSwitch.cpp:(.text+0x31d): undefined reference to `CryptoPP::DEFAULT_CHANNEL'
channelSwitch.cpp:(.text+0x323): undefined reference to `CryptoPP::DEFAULT_CHANNEL'
channelSwitch.cpp:(.text+0x339): undefined reference to `CryptoPP::HashFilter::HashFilter(CryptoPP::HashTransformation&, CryptoPP::BufferedTransformation*, bool, int, std::string const&, std::string const&)'
...
collect2: error: ld returned 1 exit status
The complete source code for channelSwitch.cpp is:
#include <iostream>
#include <cstring>
#include <cryptopp/channels.h>
#include <cryptopp/filters.h>
#include <cryptopp/sha.h>
#include <cryptopp/hex.h>
int main(int argc, char *argv[]) {
std::string message = "Now is the time for all good men to come to the aide of their country";
// Allow user to override default message from command line arg.
if(argc == 2 && argv[1] != NULL)
message = std::string(argv[1]);
// Set hash variables
std::string s1, s2, s3, s4;
CryptoPP::SHA1 sha1; CryptoPP::SHA224 sha224; CryptoPP::SHA256 sha256; CryptoPP::SHA512 sha512;
// Run hash functions
CryptoPP::HashFilter f1(sha1, new CryptoPP::HexEncoder(new CryptoPP::StringSink(s1)));
CryptoPP::HashFilter f2(sha224, new CryptoPP::HexEncoder(new CryptoPP::StringSink(s2)));
CryptoPP::HashFilter f3(sha256, new CryptoPP::HexEncoder(new CryptoPP::StringSink(s3)));
CryptoPP::HashFilter f4(sha512, new CryptoPP::HexEncoder(new CryptoPP::StringSink(s4)));
// Set route to default
CryptoPP::ChannelSwitch cs;
cs.AddDefaultRoute(f1);
cs.AddDefaultRoute(f2);
cs.AddDefaultRoute(f3);
cs.AddDefaultRoute(f4);
CryptoPP::StringSource ss(message, true /*pumpAll*/, new CryptoPP::Redirector(cs));
std::cout << "Message: " << message << std::endl;
std::cout << "SHA-1: " << s1 << std::endl;
std::cout << "SHA-224: " << s2 << std::endl;
std::cout << "SHA-256: " << s3 << std::endl;
std::cout << "SHA-512: " << s4 << std::endl;
return 0;
}
Environment information:
$ gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)
I know what the problem is. should make install first. Thanks!

Compilation issue while using Folly Futures

#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.

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.