andreacasalino / minimal-socket Goto Github PK
View Code? Open in Web Editor NEWMinimal cross platform C++ tcp, udp socket implementation
License: GNU General Public License v3.0
Minimal cross platform C++ tcp, udp socket implementation
License: GNU General Public License v3.0
Hang when trying to shut down the tcp server while one tcp client is connected.
I have a MacBook Pro with Apple Silicon Chip (M1 Pro), CMake version 3.22.2 from Homebrew, and C++ compilers from Xcode.
I tried compiling the project by creating a "CMakeLists.txt" file, copying the contents of the "CMAKE SUPPORT" part (all at once), and running "cmake ./CMakeLists.txt" - works fine so far. than I run "make" and got that error:
[ 45%] Building CXX object _deps/min_sock-build/samples/CMakeFiles/README.dir/README.cpp.o
/Users/username/Developer/C++/projectname/_deps/min_sock-src/samples/README.cpp:27:5: error: redefinition of 'main'
int main() {
^
/Users/username/Developer/C++/projectname/_deps/min_sock-src/samples/README.cpp:3:5: note: previous definition is here
int main() {
^
/Users/username/Developer/C++/projectname/_deps/min_sock-src/samples/README.cpp:78:15: error: redefinition of 'message_max_size'
std::size_t message_max_size = 1000;
^
/Users/username/Developer/C++/projectname/_deps/min_sock-src/samples/README.cpp:60:15: note: previous definition is here
std::size_t message_max_size = 1000;
^
/Users/username/Developer/C++/projectname/_deps/min_sock-src/samples/README.cpp:80:7: error: redefinition of 'received_message' with a different type: 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char>>') vs 'std::optional<MinimalSocket::ReceiverUnkownSender::ReceiveStringResult>'
received_message // resized to the nunber of bytes actually received
^
/Users/username/Developer/C++/projectname/_deps/min_sock-src/samples/README.cpp:61:8: note: previous definition is here
auto received_message = udp_socket.receive(message_max_size);
^
3 errors generated.
make[2]: *** [_deps/min_sock-build/samples/CMakeFiles/README.dir/README.cpp.o] Error 1
make[1]: *** [_deps/min_sock-build/samples/CMakeFiles/README.dir/all] Error 2
make: *** [all] Error 2
Hi Andrea,
today I am trying to compile and run the examples of the tcp server <-> client applications (CMake based within VS2022).
Unfortunatly it fails and I don´t know why?
My build steps were:
It would be great if you could take a look at it.
Thanks in advance!
Extends the library to support bluetooth connections
When using the library as a CMake dependency as described in the documentation the build process stalls as it also builds and runs all the example programs and the TCP server/client example does not exit.
Hi, we are looking to use this code in our projects, and it would be really helpful if you could add a license to clarify the usage.
Thanks!
Elaborating further on the usage as a CMake dependency, consider a simple project.
CMakeLists.txt:
project(test)
cmake_minimum_required(VERSION 3.11)
include(FetchContent)
set(CMAKE_CXX_STANDARD 20)
add_executable(test test.cpp)
# add Minimal-Socket
include(FetchContent)
set(BUILD_MinimalCppSocket_SAMPLES OFF CACHE BOOL "" FORCE) # you don't want the samples in this case
FetchContent_Declare(
min_sock
GIT_REPOSITORY https://github.com/andreacasalino/Minimal-Socket
GIT_TAG master
)
FetchContent_MakeAvailable(min_sock)
target_link_libraries(${TARGET_NAME}
MinimalSocket
)
test.cpp:
#include <MinimalSocket/udp/UdpSocket.h>
int main()
{
MinimalSocket::udp::UdpBinded udp_socket(1234, MinimalSocket::AddressFamily::IP_V4);
return 0;
}
When trying to compile this two issues will come up:
undefined reference to 'vtable for MinimalSocket::udp::UdpBinded'
I realise that this may be "CMake 101 for dumbies" material but we dumbies still need some support :)
I'm using Windows 11 for ARM VM with Parallels, on a M1 Pro MacBook Pro.
TL;DR: I just want to compile for Windows and I can't get my head around how am I supposed to do so.
I tried compiling with Cygwin, after I ran "make" I got:
In file included from /cygdrive/c/Users/username/Documents/Developer/C++/projectname/Minimal-Socket-master/src/src/Error.cpp:10:
/cygdrive/c/Users/username/Documents/Developer/C++/projectname/Minimal-Socket-master/src/src/SocketId.h:26:2: error: #error "Not supported system by MinimalSocket"
26 | #error "Not supported system by MinimalSocket"
| ^~~~~
make[2]: *** [src/CMakeFiles/MinimalSocket.dir/build.make:76: src/CMakeFiles/MinimalSocket.dir/src/Error.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:177: src/CMakeFiles/MinimalSocket.dir/all] Error 2
make: *** [Makefile:136: all] Error 2
Which makes sense because neither _WIN32
nor __linux__
are defined using Cygwin.
I tried MSYS2, after compiling I got:
'mutex' in namespace 'std' does not name a type
I tried using MinGW binaries from this repository: https://github.com/niXman/mingw-builds-binaries/releases
When I use Windows threads and CMake (v3.24.1) I run:
C:\CMake\bin\cmake.exe ./CMakeLists.txt -G "MinGW Makefiles"
mingw32-make
and I get the above mutex error.
When I use POSIX threads and CMake (v3.24.1) I run:
C:\CMake\bin\cmake.exe ./CMakeLists.txt -G "MinGW Makefiles"
mingw32-make
The library compiled successfully, I get a libMinimalSocket.a file instead of the expected .lib file, I think it's related to the POSIX thread model. Whenever I try to compile a program including the library, everything's fine but as soon as I try to use a class from it I get the following linker error:
C:/Users/username/Desktop/mingw64POSIX/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\username\AppData\Local\Temp\cctkxgqD.o:C:\Users\username\Desktop\project/main.cpp:7: undefined reference to MinimalSocket::tcp::TcpServer::TcpServer(unsigned short, MinimalSocket::AddressFamily const&)' C:/Users/username/Desktop/mingw64POSIX/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\username\AppData\Local\Temp\cctkxgqD.o: in function MinimalSocket::tcp::TcpServer::~TcpServer()': C:\Users\username\Desktop\project/./Deps/MinimalSocket/tcp/TcpServer.h:37: undefined reference to MinimalSocket::Socket::~Socket()' C:/Users/username/Desktop/mingw64POSIX/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\username\AppData\Local\Temp\cctkxgqD.o:main.cpp:(.rdata$.refptr._ZTTN13MinimalSocket3tcp9TcpServerE[.refptr._ZTTN13MinimalSocket3tcp9TcpServerE]+0x0): undefined reference to VTT for MinimalSocket::tcp::TcpServer' C:/Users/username/Desktop/mingw64POSIX/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\username\AppData\Local\Temp\cctkxgqD.o:main.cpp:(.rdata$.refptr._ZTVN13MinimalSocket3tcp9TcpServerE[.refptr._ZTVN13MinimalSocket3tcp9TcpServerE]+0x0): undefined reference to vtable for MinimalSocket::tcp::TcpServer' collect2.exe: error: ld returned 1 exit status mingw32-make: *** [makefile:14: main] Error 1
The file's content is just: #include <MinimalSocket/tcp/TcpServer.h> int main() { MinimalSocket::Port port = 12345; MinimalSocket::tcp::TcpServer tcpserver(port, MinimalSocket::AddressFamily::IP_V4); }
Compiled by: g++ -std=c++20 -I./Deps -L./Deps/WindowsLib -lMinimalSocket -g main.cpp -o main
I became desperate and installed VS Build Tools for ARM, successfully compiled the library using their CMake by running cmake --build .
it compiled to the expected .lib file.
Then I tried compiling server.cpp with MinGW, still the same linker error.
However, when I'm compiling with cl.exe I get a different linker error:
cl /std:c++20 /I.\Deps\ .\Deps\MinimalSocket.lib
server.cpp
C:\Program Files (x86)\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.34.31721\include\chrono(2206): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
Microsoft (R) Incremental Linker Version 14.34.31721.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:server.exe
server.obj
.\Deps\MinimalSocket.lib
server.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall MinimalSocket::Socket::~Socket(void)" (??1Socket@MinimalSocket@@UAE@XZ) referenced in function "public: void __thiscall MinimalSocket::tcp::TcpServer::vbase destructor'(void)" (??_DTcpServer@tcp@MinimalSocket@@QAEXXZ)
server.obj : error LNK2019: unresolved external symbol "public: __thiscall MinimalSocket::tcp::TcpServer::TcpServer(unsigned short,enum MinimalSocket::AddressFamily const &)" (??0TcpServer@tcp@MinimalSocket@@QAE@GABW4AddressFamily@2@@Z) referenced in function _main
.\Deps\MinimalSocket.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86'
server.exe : fatal error LNK1120: 2 unresolved externals
How else should I compile the project for Windows?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.