c++11 threads, SFML and code::blocks - c++

I'm trying to run this code using c++11 std::thread and the SFML library for networking, however I just cant get it to build. I'm pretty sure it has something to do with how I'm setting up the compiler, but I don't know... How can I get this to work? I've been messing about trying to get this to work, going through tutorials and such for hours now, so I have turned to Stack Overflow for answers. I'm using the code::blocks IDE and the x32-4.8.1-posix-dwarf-rev5 compiler on a windows7 64-bit machine.
#include <iostream>
#include <thread>
#include <string>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
using namespace std;
void doNetwork() {
cout << "Starting..." << endl;
sf::TcpListener listener;
listener.listen(88);
// Wait for a connection
sf::TcpSocket socket;
listener.accept(socket);
cout << "New client connected: " << socket.getRemoteAddress() << endl;
// Receive a message from the client
char buffer[1024];
size_t received = 0;
socket.receive(buffer, sizeof(buffer), received);
cout << "The client said: " << buffer << endl;
// Send an answer
string message = "Welcome, client";
socket.send(message.c_str(), message.size() + 1);
}
int main()
{
thread task(doNetwork);
task.join();
return 0;
}
This is what the build debug log is saying:
-------------- Build: Debug in bang ---------------
Compiling: main.cpp
Linking console executable: bin\Debug\bang.exe
obj\Debug\main.o: In function `Z9doNetworkv':
C:/Users/user/CppProjects/bang/main.cpp:12: undefined reference to `sf::TcpListener::TcpListener()'
C:/Users/user/CppProjects/bang/main.cpp:13: undefined reference to `sf::TcpListener::listen(unsigned short)'
C:/Users/user/CppProjects/bang/main.cpp:16: undefined reference to `sf::TcpSocket::TcpSocket()'
C:/Users/user/CppProjects/bang/main.cpp:17: undefined reference to `sf::TcpListener::accept(sf::TcpSocket&)'
C:/Users/user/CppProjects/bang/main.cpp:18: undefined reference to `sf::TcpSocket::getRemoteAddress() const'
C:/Users/user/CppProjects/bang/main.cpp:18: undefined reference to `sf::operator<<(std::ostream&, sf::IpAddress const&)'
C:/Users/user/CppProjects/bang/main.cpp:23: undefined reference to `sf::TcpSocket::receive(void*, unsigned int, unsigned int&)'
C:/Users/user/CppProjects/bang/main.cpp:28: undefined reference to `sf::TcpSocket::send(void const*, unsigned int)'
obj\Debug\main.o: In function `ZN2sf11TcpListenerD1Ev':
C:/Users/user/CppProjects/SFML-2.1/include/SFML/Network/TcpListener.hpp:43: undefined reference to `sf::Socket::~Socket()'
obj\Debug\main.o: In function `ZN2sf9TcpSocketD1Ev':
C:/Users/user/CppProjects/SFML-2.1/include/SFML/Network/TcpSocket.hpp:46: undefined reference to `sf::Socket::~Socket()'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
10 errors, 0 warnings
The settings I am using with the compiler:
Compiler Flags:
-std=C++0x
Other options:
-std=gnu++0x -U__STRICT_ANSI__
defines:
SFML_STATIC
Linker Settings:
sfml-system-s-d
sfml-network-s-d
Search Directories - Compiler:
C:\Users\user\CppProjects\SFML-2.1\include
C:\Users\user\CppProjects\bang\include
Search Directories - Linker:
C:\Users\user\CppProjects\SFML-2.1\lib\
Toolchain Executables:
-Compilers instalation dir:
C:\Program Files (x86)\mingw-builds\x32-4.8.1-posix-dwarf-rev5\mingw32
-C compiler:
i686-w64-mingw32-gcc-4.8.1.exe
-C++ Compiler:
i686-w64-mingw32-c++.exe
-Linker for dynamic libs:
i686-w64-mingw32-c++.exe
-Linker for static libs:
ar.exe
-Debugger:
gdb.exe
-Resource Compiler:
windres.exe
-Make Program:
mingw32-make.exe

I had a simular problem and how i solved this on a Linux system was: Project / build options
Go to linker settings tab.
there i added: sfml-network
Done.
Hope it helps

Related

g++ failing when trying to use GDAL library [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 months ago.
I just want to compile this easy example of the GDAL library in my Ubuntu 22.04 system using the system-packed g++, version 11.3.0:
#include <iostream>
#include "gdal_priv.h"
#include "cpl_conv.h"
#include "gdal.h"
using namespace std;
int main(int argc, char* argv[])
{
GDALDataset *poDataset;
GDALAllRegister();
poDataset = (GDALDataset *) GDALOpen(argv[1], GA_ReadOnly);
if (poDataset == NULL)
{
cout << "No dataset loaded for file " << argv[1] << ". Exiting." << endl;
return 1;
}
cout << "Driver: "
<< poDataset->GetDriver()->GetDescription()
<< "/"
<< poDataset->GetDriver()->GetMetadataItem(GDAL_DMD_LONGNAME)
<< endl;
cout << "Size: "
<< poDataset->GetRasterXSize() << "x"
<< poDataset->GetRasterYSize() << "x"
<< poDataset->GetRasterCount()
<< endl;
if (poDataset->GetProjectionRef() != NULL)
{
cout << "Projection: " << poDataset->GetProjectionRef() << endl;
}
}
Of course I installed the GDAL libraries, as it can be seen here:
~$ dpkg -l | grep gdal
ii gdal-bin 3.4.1+dfsg-1build4 amd64 Geospatial Data Abstraction Library - Utility programs
ii gdal-data 3.4.1+dfsg-1build4 all Geospatial Data Abstraction Library - Data files
ii libgdal-dev 3.4.1+dfsg-1build4 amd64 Geospatial Data Abstraction Library - Development files
ii libgdal30 3.4.1+dfsg-1build4 amd64 Geospatial Data Abstraction Library
ii python3-gdal 3.4.1+dfsg-1build4 amd64 Python 3 bindings to the Geospatial Data Abstraction Library
Everything seems to be settled and ready to go, but then, when I trigger this g++ command to compile my little program
g++ -I/usr/include/gdal -L/usr/lib -lgdal open_file.cpp -o open_file -g
it fails with this output:
/usr/bin/ld: /tmp/ccU6PwuP.o: in function `main':
/home/jose/Code/concepts/gdal/open_file.cpp:13: undefined reference to `GDALAllRegister'
/usr/bin/ld: /home/jose/Code/concepts/gdal/open_file.cpp:14: undefined reference to `GDALOpen'
/usr/bin/ld: /home/jose/Code/concepts/gdal/open_file.cpp:29: undefined reference to `GDALDataset::GetRasterXSize()'
/usr/bin/ld: /home/jose/Code/concepts/gdal/open_file.cpp:30: undefined reference to `GDALDataset::GetRasterYSize()'
/usr/bin/ld: /home/jose/Code/concepts/gdal/open_file.cpp:31: undefined reference to `GDALDataset::GetRasterCount()'
/usr/bin/ld: /home/jose/Code/concepts/gdal/open_file.cpp:34: undefined reference to `GDALDataset::GetProjectionRef() const'
/usr/bin/ld: /home/jose/Code/concepts/gdal/open_file.cpp:36: undefined reference to `GDALDataset::GetProjectionRef() const'
collect2: error: ld returned 1 exit status
Which doesn't make any sense, because I am indeed passing the GDAL libraries in -I/usr/include/gdal and the definition of the "undefined" references do exist in the multiple .h files there.
Moreover, this works using clang++:
clang++ -I/usr/include/gdal -L/usr/lib -lgdal open_file.cpp -o open_file -g
Did anyone have a similar issue, or can give some hint on where the problem might be? Thank you.
Include paths have nothing to do with the symbols.
-I/usr/include/gdal -L/usr/lib both are not necessary as they are set by default. But you should use #include <gdal/gdal.h>, not just <gdal.h> and certainly not "gdal.h".
Move -lgdal after all other cpp/object files.
In general, it should be g++ <OPTIONS> <OBJECTS> <LIBRARIES> where library A which uses symbols from lib B should appear after B i.e. -lB -lA, the order matters for ld. Because it will use the library to resolve just the currently missing symbols and then will promptly forget the library ever existed. So any newly found unresolved symbols will not be resolved, hence shifting the library arguments "right". One can resolve circular dependencies by repeating libraries more than once.

Trying to build C++ Dlib example fails to link with hundreds of undefined reference errors

I'm trying to compile a DLIB example: http://dlib.net/image_ex.cpp.html
and I copied it word-for-word:
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>
#include <fstream>
using namespace std;
using namespace dlib;
// ----------------------------------------------------------------------------
int main(int argc, char** argv)
{
try
{
// make sure the user entered an argument to this program
if (argc != 2)
{
cout << "error, you have to enter a BMP file as an argument to this program" << endl;
return 1;
}
And I used this command:
g++ dlib1.cpp -o dlib1 -std=c++11 -O3 -I ~/Packages/dlib/ -lpthread -lX11 -ljpeg -lpng -DDLIB_JPEG_SUPPORT -DDLIB_PNG_SUPPORT
And despite that command working fine months ago: [Can't include the JPEG_SUPPORT headers in a dlib cpp file ] this time, it gave me hundreds of errors:
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x7e): undefined reference to `dlib::png_loader::is_gray() const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x94): undefined reference to `dlib::png_loader::is_gray() const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0xaa): undefined reference to `dlib::png_loader::is_graya() const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0xd0): undefined reference to `dlib::png_loader::get_row(unsigned int) const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x176): undefined reference to `dlib::png_loader::get_row(unsigned int) const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x1ce): undefined reference to `dlib::png_loader::is_graya() const'
What exactly is going wrong here?
Some of the samples need a command line such as ...
g++ -std=c++11 -O3 -I.. ../dlib/all/source.cpp -lpthread -lX11 example_program_name.cpp
I guess source.cpp is a "library as cpp code" link : dlib compile

Fixing undefined reference to dlopen() and dlcose()

I have created a simple C++ application. I can compile it, and it works fine. But now I need to load the library dynamically, and I have added dlfnc.h to my project and added some more code:
#include <iostream>
#include <dlfcn.h>
void *mylib;
int eret;
using namespace std;
int main() {
mylib = dlopen("mylib.so", RTLD_LOCAL | RTLD_LAZY);
eret = dlclose(mylib);
cout << "!!!Hello, World!!!" << endl; // Prints !!!Hello, World!!!
return 0;
}
Compiling:
cd ~/workspace/LinuxGcc/src
g++ LinuxGcc.cpp
And I got a compilation error:
/tmp/ccxTLiGY.o: In function `main':
LinuxGcc.cpp:(.text+0xf): undefined reference to `dlopen'
LinuxGcc.cpp:(.text+0x25): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
dlfcn.h exist in /usr/include/.
Where is the problem?
From dlopen(3):
Link with -ldl.
so
g++ LinuxGcc.cpp -ldl
will be OK.
The solution is very simple. Add the -ldl flag for linking.
In case of the Bazel build system, linkopts = ['-ldl'].

Errors linking to Boost system

I've built Boost 1.59 for MinGW 4.9.2 32bit as follows:
bootstrap.bat mingw
b2 --prefix=%USERPROFILE%\Code\Libraries\boost toolset=gcc install (these are static libraries)
When trying to compile:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.wait();
cout << "Hello World!" << endl;
return 0;
}
With:
g++ -o test -L %USERPROFILE%\Code\Libraries\boost\lib -l boost_system-mgw49-mt-1_59 -I %USERPROFILE%\Code\Libraries\boost\include\boost-1_59 main.cpp
I get this error:
C:\Users\Brady\AppData\Local\Temp\ccsbGjrk.o:main.cpp:(.text+0x193): undefined reference to `boost::system::generic_category()'
C:\Users\Brady\AppData\Local\Temp\ccsbGjrk.o:main.cpp:(.text+0x19d): undefined reference to `boost::system::generic_category()'
C:\Users\Brady\AppData\Local\Temp\ccsbGjrk.o:main.cpp:(.text+0x1a7): undefined reference to `boost::system::system_category()'
C:/Program Files (x86)/QT/Tools/mingw492_32/bin/../lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\Brady\AppData\Local\Temp\ccsbGjrk.o: bad reloc address 0xe in section `.text$_ZN5boost6system14error_categoryD2Ev[__ZN5boost6system14error_categoryD2Ev]'
collect2.exe: error: ld returned 1 exit status
I'm getting this same error across 2 computers. Any help is appreciated!
It's a linker error. You need to link to the Boost System library.
Do this
g++ -o test -L %USERPROFILE%\Code\Libraries\boost\lib -lboost_system -I %USERPROFILE%\Code\Libraries\boost\include\boost-1_59 main.cpp

Using Boost::filesystem in Ubuntu 12.04

I need to iterate through files in a folder using C++ and I am using Boost filesystem library for that purpose.
I am trying to run the tutorial code in http://www.boost.org/doc/libs/1_54_0/libs/filesystem/doc/tutorial.html
After installing boost, running bootstrap.sh to build all libraries followed by ./b2, I am able to run some simple code. But when I try to compile the following code from the tutorial
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if(argc < 2)
{
std::cout << "Usage: a.out path\n";
return 1;
}
path my_path(argv[1]);
std::cout << argv[1] << " " << file_size(my_path) << '\n';
return 0;
}
using
g++ f1.cpp -lboost_filesystem -lboost_system
I get the following error:-
/tmp/cc3IJurw.o: In function `boost::filesystem::file_size(boost::filesystem::path const&)':
f1.cpp:(.text._ZN5boost10filesystem9file_sizeERKNS0_4pathE[boost::filesystem::file_size(boost::filesystem::path const&)]+0x19): undefined reference to `boost::filesystem::detail::file_size(boost::filesystem::path const&, boost::system::error_code*)'
/tmp/cc3IJurw.o: In function `boost::filesystem::path::path<char*>(char* const&, boost::enable_if<boost::filesystem::path_traits::is_pathable<boost::decay<char*>::type>, void>::type*)':
f1.cpp:(.text._ZN5boost10filesystem4pathC2IPcEERKT_PNS_9enable_ifINS0_11path_traits11is_pathableINS_5decayIS4_E4typeEEEvE4typeE[_ZN5boost10filesystem4pathC5IPcEERKT_PNS_9enable_ifINS0_11path_traits11is_pathableINS_5decayIS4_E4typeEEEvE4typeE]+0x22): undefined reference to `boost::filesystem::path::codecvt()'
collect2: ld returned 1 exit status
Does anyone know a fix to this?