I use uWs for Node.js in server to create websocket Server to send and receive message in real-time. It been great so far. However i would like to write an application in C++ for server later such at WebRTC Native + Websocket to send signal for start a video call or record WebRTC (Server will join as a peer connection) with Ffmpeg to video encoding…
so i did decided to use native c++ for writing this service. I ‘m also so new to C++ i spending time when i free to learn from basic. i ‘m on Mac/OS i use Xcode but i got problem to linking such at openssl libuv, boost, zlib those are dependence libraries and required to use uWs
here is first time i linking and use uWs in Xcode i got failure
Undefined symbols for architecture x86_64:
“_SSL_get_error”, referenced from:
uS::Socket::write(uS::Socket::Queue::Message*, bool&) in main.o
“_SSL_write”, referenced from:
uS::Socket::write(uS::Socket::Queue::Message*, bool&) in main.o
“uS::TLS::Context::~Context()”, referenced from:
_main in main.o
“uS::Node::run()”, referenced from:
_main in main.o
“uS::Node::Node(int, int, int, bool)”, referenced from:
uWS::Hub::Hub(int, bool, unsigned int) in main.o
“uS::Node::~Node()”, referenced from:
uWS::Hub::Hub(int, bool, unsigned int) in main.o
uWS::Hub::~Hub() in main.o
“uWS::Hub::allocateDefaultCompressor(z_stream_s*)”, referenced from:
uWS::Hub::Hub(int, bool, unsigned int) in main.o
“uWS::Hub::listen(int, uS::TLS::Context, int, uWS::Group<true>*)”, referenced from:
_main in main.o
“uWS::Group<false>::Group(int, unsigned int, uWS::Hub*, uS::NodeData*)”, referenced from:
uWS::Hub::Hub(int, bool, unsigned int) in main.o
“uWS::Group<true>::onHttpRequest(std::__1::function<void (uWS::HttpResponse*, uWS::HttpRequest, char*, unsigned long, unsigned long)>)”, referenced from:
_main in main.o
“uWS::Group<true>::onMessage(std::__1::function<void (uWS::WebSocket<true>*, char*, unsigned long, uWS::OpCode)>)”, referenced from:
_main in main.o
“uWS::Group<true>::Group(int, unsigned int, uWS::Hub*, uS::NodeData*)”, referenced from:
uWS::Hub::Hub(int, bool, unsigned int) in main.o
“uWS::WebSocket<true>::send(char const*, unsigned long, uWS::OpCode, void (*)(uWS::WebSocket<true>*, void*, bool, void*), void*, bool)”, referenced from:
main::$_0::operator()(uWS::WebSocket<true>*, char*, unsigned long, uWS::OpCode) const in main.o
“_deflateEnd”, referenced from:
uWS::Hub::~Hub() in main.o
“_inflateEnd”, referenced from:
uWS::Hub::~Hub() in main.o
“_inflateInit2_”, referenced from:
uWS::Hub::Hub(int, bool, unsigned int) in main.o
“_uv_async_send”, referenced from:
uS::Async::send() in main.o
“_uv_poll_start”, referenced from:
uS::Poll::start(uS::Loop*, uS::Poll*, int) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Finally i did resolve the problem and here is what i did. I hope this help to someone to use this library.
Install Open SSl use homebrew
We are going to use homebrew to install modules, if you have not install hopmebrew , just easy to install it by run this command:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
open terminal and run this command
brew install openssl
After installing openssl module should in directory /usr/local/opt/openssl now we do need link this into directory /usr/local/include
sudo ln -s /usr/local/opt/openssl /usr/local/include/openssl
Install Libuv & Zlib boost
brew install libuv zlib boost
Download and compile uWs
We need download uWebsocket and compile it to download uws visit the url : https://github.com/uNetworking/uWebSockets
click and download and extract zip file for example we extract file to ~/downloads/uWebSockets-master
cd ~/downloads/uWebSockets-master
make
sudo make install
After compiled the include of library uwebsocket is moved to :
/usr/local/include/uWs
and library of uWs is /usr/local/lib/libuWS.dylib
remember those path and we need add to Xcode later.
so we are done installing modules, now open Xcode and create new project Select MacOs -> Command line tool

Enter project name and select language is C++

Open main.cpp and enter sample code
#include <iostream>
#include <uWS/uWS.h>
using namespace uWS;
int main(int argc, const char * argv[]) {
Hub h;
std::string response = "Hello!";
h.onMessage([](WebSocket<SERVER> *ws, char *message, size_t length, OpCode opCode) {
ws->send(message, length, opCode);
});
h.onHttpRequest([&](HttpResponse *res, HttpRequest req, char *data, size_t length,
size_t remainingBytes) {
res->end(response.data(), response.length());
});
if (h.listen(3000)) {
h.run();
}
return 0;
}
now let run or build hit command + B you should get error like this

Don’t worry because we have not add header search path to fix it, navigate on your project name name: in this tutorial is “MyProject” -> build settings , and search for: header search paths
and double lick on header search paths

add + and enter path
/usr/local/include

Now search for Library Search paths and add /usr/local/opt/openssl/lib

Now hit command + B (build project) header import error is gone however you now getting more errors messages like this screenshot

To fix this we do need link uws library and include correctly
select your project name in this tutorial is “myproject” -> General

In Linked Frameworks libraries section click “+” to add new library is /usr/local/lib/libuWS.dylib

Click Add Other …
Now hit Command + shift + G enter path is: /usr/local/lib

find and select file is: libuWS.dylib

Now hit Command +B to build project we only see one error left

To fix last error we do need add linker flags navigate Select your project ->Build Settings now search for key word: Other Linker Flags
now doublick on it and add following lines
-I/usr/local/include/uWs
-L/usr/local/lib
-L/usr/local/opt/zlib/lib
-luWS
-lssl
-lcrypto
-lz
-luv

Now back to main.cpp and hit Command + R to run the project
open browser enter url: http://127.0.0.1:3000

Congratulations! Happy hacking code :)