OpenHarmony 使用httplib创建http-server失败
【背景】
根据这份指引:
https://gitee.com/openharmony-si ... cs/hap_integrate.md
已编译好OpenHarmony版本httplib库
【1.尝试在devecostudio中调用httplib】
native 调用httplib主要代码如下:
- #include "napi/native_api.h"
- #include <hilog/log.h>
- #include <stdio.h>
- #include <iostream>
- #include "httplib.h"
- #define LOG_PRINT_DOMAIN 0xFF00
- ......
- Server svr;
-
- svr.Get("/hello", [](const Request &req, Response &res) {
- res.set_content("Hello World!", "text/plain");
- });
-
- if (svr.listen("0.0.0.0", 8080)) {
- OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "OPENHARMONY-SERVER","Server is running at http://localhost:8080");
- } else {
- OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "OPENHARMONY-SERVER","Failed to start server");
- }
- ....
复制代码
报错内容:
E C015b0/NetsysNativeService: [HookSocket-(netsys_sock_client.cpp:46)]musl create socket failed, errno 1
I A0ff00/OPENHARMONY-SERVER: Failed to start server
【2.单独写一个测试文件】
server.cpp:
- /* server.cpp*/
- #include <stdio.h>
- #include <iostream>
- #include "httplib.h"
- using namespace httplib;
- void http_callback(const Request& req, Response& resp)
- {
- (void)req;
- std::string body = "<html>linux so easy</html>";
- resp.set_content(body.c_str(),body.size(),"text/html");
- }
- int main()
- {
- Server svr;
- svr.Get("/aaa",http_callback);
- svr.listen("0.0.0.0",19998);
- return 0;
- }
复制代码 CMakeLists.txt:
- cmake_minimum_required(VERSION 3.1)
- project(httplib)
- add_executable(httplib server.cpp)
- target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/brotli/${OHOS_ARCH}/lib/libbrotlicommon-static.a)
- target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/brotli/${OHOS_ARCH}/lib/libbrotlidec-static.a)
- target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/brotli/${OHOS_ARCH}/lib/libbrotlienc-static.a)
- target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cpp-httplib/${OHOS_ARCH}/lib/libhttplib.a)
- target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/googletest/${OHOS_ARCH}/lib/libgmock.a)
- target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/googletest/${OHOS_ARCH}/lib/libgmock_main.a)
- target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/googletest/${OHOS_ARCH}/lib/libgtest.a)
- target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/googletest/${OHOS_ARCH}/lib/libgtest_main.a)
- target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/openssl/${OHOS_ARCH}/lib/libcrypto.a)
- target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/openssl/${OHOS_ARCH}/lib/libssl.a)
- target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/zlib/${OHOS_ARCH}/lib/libz.a)
- target_include_directories(httplib PRIVATE brotli/${OHOS_ARCH}/include)
- target_include_directories(httplib PRIVATE cpp-httplib/${OHOS_ARCH}/include)
- target_include_directories(httplib PRIVATE googletest/${OHOS_ARCH}/include)
- target_include_directories(httplib PRIVATE openssl/${OHOS_ARCH}/include)
- target_include_directories(httplib PRIVATE zlib/${OHOS_ARCH}/include)
复制代码 编译指令:/home/openharmony/ohos-sdk/linux/native/build-tools/cmake/bin/cmake -DCMAKE_TOOLCHAIN_FILE=/home/openharmony/ohos-sdk/linux/native/build/cmake/ohos.toolchain.cmake -stdlib=libc++ -DOHOS_ARCH=arm64-v8a .. -L //加不加-stdlib=libc++对生成的文件没什么影响
输出:
- root@LAPTOP-U9BLNV2M:/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build# /home/openharmony/ohos-sdk/linux/native/build-tools/cmake/bin/cmake -DCMAKE_TOOLCHAIN_FILE=/home/openharmony/ohos-sdk/linux/native/build/cmake/ohos.toolchain.cmake -stdlib=libc++ -DOHOS_ARCH=arm64-v8a .. -L
- -- The C compiler identification is Clang 15.0.4
- -- The CXX compiler identification is Clang 15.0.4
- -- Check for working C compiler: /home/openharmony/ohos-sdk/linux/native/llvm/bin/clang
- -- Check for working C compiler: /home/openharmony/ohos-sdk/linux/native/llvm/bin/clang -- works
- -- Detecting C compiler ABI info
- -- Detecting C compiler ABI info - done
- -- Detecting C compile features
- -- Detecting C compile features - done
- -- Check for working CXX compiler: /home/openharmony/ohos-sdk/linux/native/llvm/bin/clang++
- -- Check for working CXX compiler: /home/openharmony/ohos-sdk/linux/native/llvm/bin/clang++ -- works
- -- Detecting CXX compiler ABI info
- -- Detecting CXX compiler ABI info - done
- -- Detecting CXX compile features
- -- Detecting CXX compile features - done
- -- Configuring done
- -- Generating done
- -- Build files have been written to: /home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build
- -- Cache values
- CMAKE_ASM_FLAGS:STRING=
- CMAKE_ASM_FLAGS_DEBUG:STRING=
- CMAKE_ASM_FLAGS_RELEASE:STRING=
- CMAKE_BUILD_TYPE:STRING=
- CMAKE_INSTALL_PREFIX:PATH=/usr/local
- CMAKE_TOOLCHAIN_FILE:FILEPATH=/home/openharmony/ohos-sdk/linux/native/build/cmake/ohos.toolchain.cmake
- root@LAPTOP-U9BLNV2M:/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build# make
- make[1]: Entering directory '/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build'
- make[2]: Entering directory '/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build'
- Scanning dependencies of target httplib
- make[2]: Leaving directory '/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build'
- make[2]: Entering directory '/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build'
- [ 50%] Building CXX object CMakeFiles/httplib.dir/server.cpp.o
- clang++: warning: argument unused during compilation: '--gcc-toolchain=/home/openharmony/ohos-sdk/linux/native/llvm' [-Wunused-command-line-argument]
- [100%] Linking CXX executable httplib
- make[2]: Leaving directory '/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build'
- [100%] Built target httplib
- make[1]: Leaving directory '/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build
复制代码 将httplib推到openharmony开发板上运行,出现以下报错:
- # ./httplib
- Error loading shared library libc++_shared.so: No such file or directory (needed by ./httplib)
- Error relocating ./httplib: _ZNSt4__n112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED1Ev: symbol not found
- Error relocating ./httplib: _ZNSt4__n112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEPKcm: symbol not found
- Error relocating ./httplib: _ZdlPv: symbol not found
- Error relocating ./httplib: __cxa_allocate_exception: symbol not found
- Error relocating ./httplib: _ZNSt20bad_array_new_lengthC1Ev: symbol not found
- Error relocating ./httplib: __cxa_throw: symbol not found
- Error relocating ./httplib: _Znwm: symbol not found
- Error relocating ./httplib: __cxa_begin_catch: symbol not found
- Error relocating ./httplib: _ZSt9terminatev: symbol not found
- Error relocating ./httplib: _ZNSt4__n112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE9push_backEc: symbol not found
- Error relocating ./httplib: _ZNKSt4__n112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE7compareEmmPKc: symbol not found
- Error relocating ./httplib: _ZNSt4__n18ios_base4initEPv: symbol not found
- Error relocating ./httplib: _ZNSt4__n115basic_streambufIcNS_11char_traitsIcEEEC2Ev: symbol not found
- Error relocating ./httplib: _ZNKSt4__n18ios_base6getlocEv: symbol not found
- Error relocating ./httplib: _ZNKSt4__n16locale9use_facetERNS0_2idE: symbol not found
- Error relocating ./httplib: _ZNSt4__n16localeD1Ev: symbol not found
- Error relocating ./httplib: _ZNSt4__n115basic_streambufIcNS_11char_traitsIcEEED2Ev: symbol not found
- Error relocating ./httplib: _ZNSt4__n113basic_ostreamIcNS_11char_traitsIcEEED2Ev: symbol not found
- Error relocating ./httplib: _ZNSt4__n19basic_iosIcNS_11char_traitsIcEEED2Ev: symbol not found
- Error relocating ./httplib: _ZNSt4__n112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6appendEPKc: symbol not foun
- ...
复制代码
【3.使用websocket】
目前也在尝试使用websocket创建http server,不知行不行。
https://docs.openharmony.cn/page ... ketserverinstance10
【运行环境】
硬件:RK3588
ROM版本:Openharmony 4.0 Release
DevEvoStudio版本:DevEco Studio 3.1.1 Release
SDK版本:API10 (4.0.10.5)
|
|
|
|
|
您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱
symbol not found 就是库没找到,你库放哪了,是不是系统默认地方,若不是,自己设置下library path |
|
|
|
|
我也遇到一样的问题, 报 _ZNSt4__n112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED1Ev 符号库没找到,关键是我在其他库里也没搜索到这个符号的位置,我再找找 |
|
|
|
|