我可以编译一个只使用信号量的程序或一个只使用 sio_client websocket 库的程序。但是,当编译一个包含这两个库的简单程序时,我会处理我什至不知道如何描述的巨大编译问题。
#include <iostream>
#include <thread>
#include <chrono>
// #include <sio_client.h>
#include "socket.io-client-cpp/src/sio_client.h"
#include <semaphore>
using namespace sio;
using namespace std;
sio::client io;
counting_semaphore<1>* test;
//g++-11 main.cpp -std=c++17 -o app -L /usr/local/lib -l sioclient -I /usr/local/include
void OnMessage(sio::event &m){
auto messages = m.get_messages();
io.socket()->emit("key", string("Hello"));
test->release();
}
int main()
{
sio::client io;
io.connect("http://127.0.0.1:8081");
string command = "w";
test = new counting_semaphore<1>(0);
io.socket()->on("teste", &OnMessage);
while(true){
test->acquire();
cout << " :D " << endl;
this_thread::sleep_for(100ms);
}
}
如果我用 <sio_client.h> 替换包含“socket.io-client-cpp/src/sio_client.h”,则编译器找不到 sio_client 库,并且如果我手动包含库路径,因为它在代码中,误差比圣经还大。
需要指出的是,我根据库本身包含的 README.md 进行了 sio_client 安装过程,并且我已经在其他上下文中使用过它,效果很好。
要编译这个程序,我使用以下命令:
g++-11 main.cpp -std=c++2a -L. -lsioclient -o app
错误的开始:
ld: warning: dylib (/usr/local/Cellar/gcc/11.1.0_1/lib/gcc/11/libstdc++.dylib) was built for newer macOS version (11.3) than being linked (11.1)
Undefined symbols for architecture x86_64:
"__ZN3sio6client6socketERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", referenced from:
__Z9OnMessageRN3sio5eventE in ccKuChIE.o
_main in ccKuChIE.o
"__ZN3sio6client7connectERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", referenced from:
_main in ccKuChIE.o
"__ZN3sio6socket2onERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt8functionIFvRNS_5eventEEE", referenced from:
_main in ccKuChIE.o
"__ZN3sio6socket4emitERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_7message4listERKSt8functionIFvSC_EE", referenced from:
__Z9OnMessageRN3sio5eventE in ccKuChIE.o
"__ZNKSt3__110error_code7messageEv", referenced from:
__ZN11websocketpp9transport4asio8endpointINS_6config11asio_client16transport_configEE9init_asioEv in libsioclient.a(sio_client_impl.cpp.o)
__ZN3sio11client_impl12connect_implERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEES9_ in libsioclient.a(sio_client_impl.cpp.o)
__ZN11websocketpp10connectionINS_6config11asio_clientEE14replace_headerERKNSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEESC_ in libsioclient.a(sio_client_impl.cpp.o)
__ZN3sio11client_impl9send_implERKNSt3__110shared_ptrIKNS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEN11websocketpp5frame6opcode5valueE in libsioclient.a(sio_client_impl.cpp.o)
__ZN11websocketpp9exceptionC1ENSt3__110error_codeE in libsioclient.a(sio_client_impl.cpp.o)
__ZN11websocketpp8endpointINS_10connectionINS_6config11asio_clientEEES3_E17create_connectionEv in libsioclient.a(sio_client_impl.cpp.o)
__ZN11websocketpp10connectionINS_6config11asio_clientEE17handle_read_frameERKNSt3__110error_codeEm in libsioclient.a(sio_client_impl.cpp.o)
...
"__ZNKSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE13find_first_ofEPKcmm", referenced from:
__ZN3sio6packet5parseERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE in libsioclient.a(sio_packet.cpp.o)
"__ZNKSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE4findEcm", referenced from:
__ZN3sio11client_impl12connect_implERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEES9_ in libsioclient.a(sio_client_impl.cpp.o)
__ZN3sio6packet5parseERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE in libsioclient.a(sio_packet.cpp.o)
"__ZNKSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE7compareEmmPKcm", referenced from:
__ZN3sio11client_impl12connect_implERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEES9_ in libsioclient.a(sio_client_impl.cpp.o)
__ZN3sio11client_impl6socketERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE in libsioclient.a(sio_client_impl.cpp.o)
__ZN11websocketpp4http6parser6parser12prepare_bodyEv in libsioclient.a(sio_client_impl.cpp.o)
__ZN11websocketpp3uriC2ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEERKS7_S9_ in libsioclient.a(sio_client_impl.cpp.o)
__ZN11websocketpp3uriC2ERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEES9_S9_S9_ in libsioclient.a(sio_client_impl.cpp.o)
__ZNK11websocketpp9processor6hybi00INS_6config11asio_clientEE18validate_handshakeERKNS_4http6parser7requestE in libsioclient.a(sio_client_impl.cpp.o)
__ZNK11websocketpp9processor6hybi13INS_6config11asio_clientEE18validate_handshakeERKNS_4http6parser7requestE in libsioclient.a(sio_client_impl.cpp.o)
...