OpenHarmony 使用httplib创建http-server失败

yjka 显示全部楼层 发表于 2024-4-7 15:17:52
【背景】

根据这份指引:
https://gitee.com/openharmony-si ... cs/hap_integrate.md

已编译好OpenHarmony版本httplib库

【1.尝试在devecostudio中调用httplib】

native 调用httplib主要代码如下:
  1. #include "napi/native_api.h"
  2. #include <hilog/log.h>
  3. #include <stdio.h>
  4. #include <iostream>
  5. #include "httplib.h"
  6. #define LOG_PRINT_DOMAIN 0xFF00
  7.    ......
  8. Server svr;
  9.    
  10.       svr.Get("/hello", [](const Request &req, Response &res) {
  11.         res.set_content("Hello World!", "text/plain");
  12.       });
  13.    
  14.       if (svr.listen("0.0.0.0", 8080)) {
  15.         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "OPENHARMONY-SERVER","Server is running at http://localhost:8080");
  16.       } else {
  17.         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "OPENHARMONY-SERVER","Failed to start server");
  18.       }
  19. ....
复制代码


报错内容:
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:
  1. /* server.cpp*/
  2. #include <stdio.h>
  3. #include <iostream>

  4. #include "httplib.h"
  5. using namespace httplib;

  6. void http_callback(const Request& req, Response& resp)
  7. {
  8.   (void)req;
  9.   std::string body = "<html>linux so easy</html>";
  10.   resp.set_content(body.c_str(),body.size(),"text/html");
  11. }
  12. int main()
  13. {
  14.   Server svr;
  15.   svr.Get("/aaa",http_callback);

  16.   svr.listen("0.0.0.0",19998);
  17.   return 0;
  18. }
复制代码
CMakeLists.txt:
  1. cmake_minimum_required(VERSION 3.1)
  2. project(httplib)

  3. add_executable(httplib server.cpp)

  4. target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/brotli/${OHOS_ARCH}/lib/libbrotlicommon-static.a)
  5. target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/brotli/${OHOS_ARCH}/lib/libbrotlidec-static.a)
  6. target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/brotli/${OHOS_ARCH}/lib/libbrotlienc-static.a)
  7. target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cpp-httplib/${OHOS_ARCH}/lib/libhttplib.a)
  8. target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/googletest/${OHOS_ARCH}/lib/libgmock.a)
  9. target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/googletest/${OHOS_ARCH}/lib/libgmock_main.a)
  10. target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/googletest/${OHOS_ARCH}/lib/libgtest.a)
  11. target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/googletest/${OHOS_ARCH}/lib/libgtest_main.a)
  12. target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/openssl/${OHOS_ARCH}/lib/libcrypto.a)
  13. target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/openssl/${OHOS_ARCH}/lib/libssl.a)
  14. target_link_libraries(httplib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/zlib/${OHOS_ARCH}/lib/libz.a)
  15. target_include_directories(httplib PRIVATE brotli/${OHOS_ARCH}/include)
  16. target_include_directories(httplib PRIVATE cpp-httplib/${OHOS_ARCH}/include)
  17. target_include_directories(httplib PRIVATE googletest/${OHOS_ARCH}/include)
  18. target_include_directories(httplib PRIVATE openssl/${OHOS_ARCH}/include)
  19. 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++对生成的文件没什么影响
输出:
  1. 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
  2. -- The C compiler identification is Clang 15.0.4
  3. -- The CXX compiler identification is Clang 15.0.4
  4. -- Check for working C compiler: /home/openharmony/ohos-sdk/linux/native/llvm/bin/clang
  5. -- Check for working C compiler: /home/openharmony/ohos-sdk/linux/native/llvm/bin/clang -- works
  6. -- Detecting C compiler ABI info
  7. -- Detecting C compiler ABI info - done
  8. -- Detecting C compile features
  9. -- Detecting C compile features - done
  10. -- Check for working CXX compiler: /home/openharmony/ohos-sdk/linux/native/llvm/bin/clang++
  11. -- Check for working CXX compiler: /home/openharmony/ohos-sdk/linux/native/llvm/bin/clang++ -- works
  12. -- Detecting CXX compiler ABI info
  13. -- Detecting CXX compiler ABI info - done
  14. -- Detecting CXX compile features
  15. -- Detecting CXX compile features - done
  16. -- Configuring done
  17. -- Generating done
  18. -- Build files have been written to: /home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build
  19. -- Cache values
  20. CMAKE_ASM_FLAGS:STRING=
  21. CMAKE_ASM_FLAGS_DEBUG:STRING=
  22. CMAKE_ASM_FLAGS_RELEASE:STRING=
  23. CMAKE_BUILD_TYPE:STRING=
  24. CMAKE_INSTALL_PREFIX:PATH=/usr/local
  25. CMAKE_TOOLCHAIN_FILE:FILEPATH=/home/openharmony/ohos-sdk/linux/native/build/cmake/ohos.toolchain.cmake
  26. root@LAPTOP-U9BLNV2M:/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build# make
  27. make[1]: Entering directory '/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build'
  28. make[2]: Entering directory '/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build'
  29. Scanning dependencies of target httplib
  30. make[2]: Leaving directory '/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build'
  31. make[2]: Entering directory '/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build'
  32. [ 50%] Building CXX object CMakeFiles/httplib.dir/server.cpp.o
  33. clang++: warning: argument unused during compilation: '--gcc-toolchain=/home/openharmony/ohos-sdk/linux/native/llvm' [-Wunused-command-line-argument]
  34. [100%] Linking CXX executable httplib
  35. make[2]: Leaving directory '/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build'
  36. [100%] Built target httplib
  37. make[1]: Leaving directory '/home/hahman/httplib/tpc_c_cplusplus/lycium/usr/build
复制代码
将httplib推到openharmony开发板上运行,出现以下报错
  1. # ./httplib
  2. Error loading shared library libc++_shared.so: No such file or directory (needed by ./httplib)
  3. Error relocating ./httplib: _ZNSt4__n112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED1Ev: symbol not found
  4. Error relocating ./httplib: _ZNSt4__n112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEPKcm: symbol not found
  5. Error relocating ./httplib: _ZdlPv: symbol not found
  6. Error relocating ./httplib: __cxa_allocate_exception: symbol not found
  7. Error relocating ./httplib: _ZNSt20bad_array_new_lengthC1Ev: symbol not found
  8. Error relocating ./httplib: __cxa_throw: symbol not found
  9. Error relocating ./httplib: _Znwm: symbol not found
  10. Error relocating ./httplib: __cxa_begin_catch: symbol not found
  11. Error relocating ./httplib: _ZSt9terminatev: symbol not found
  12. Error relocating ./httplib: _ZNSt4__n112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE9push_backEc: symbol not found
  13. Error relocating ./httplib: _ZNKSt4__n112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE7compareEmmPKc: symbol not found
  14. Error relocating ./httplib: _ZNSt4__n18ios_base4initEPv: symbol not found
  15. Error relocating ./httplib: _ZNSt4__n115basic_streambufIcNS_11char_traitsIcEEEC2Ev: symbol not found
  16. Error relocating ./httplib: _ZNKSt4__n18ios_base6getlocEv: symbol not found
  17. Error relocating ./httplib: _ZNKSt4__n16locale9use_facetERNS0_2idE: symbol not found
  18. Error relocating ./httplib: _ZNSt4__n16localeD1Ev: symbol not found
  19. Error relocating ./httplib: _ZNSt4__n115basic_streambufIcNS_11char_traitsIcEEED2Ev: symbol not found
  20. Error relocating ./httplib: _ZNSt4__n113basic_ostreamIcNS_11char_traitsIcEEED2Ev: symbol not found
  21. Error relocating ./httplib: _ZNSt4__n19basic_iosIcNS_11char_traitsIcEEED2Ev: symbol not found
  22. Error relocating ./httplib: _ZNSt4__n112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6appendEPKc: symbol not foun
  23. ...
复制代码

【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)

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

精彩评论2

深开鸿_王石

沙发 发表于 2024-4-7 17:18:31
symbol not found 就是库没找到,你库放哪了,是不是系统默认地方,若不是,自己设置下library path

hocker

板凳 发表于 2024-11-3 07:52:24
我也遇到一样的问题, 报 _ZNSt4__n112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED1Ev  符号库没找到,关键是我在其他库里也没搜索到这个符号的位置,我再找找

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部