DEV Community

Discussion on: C/C++ code in React using WebAssembly

Collapse
 
joyhughes profile image
Joy Hughes

I got the first part working! Now I just need to drop in my complex C++ program on one end and my React interface on the other and I should be good to go...

Collapse
 
aloth_naveen_4db184e14452 profile image
Aloth Naveen

how did you do for multiple c codes, if i have dependencies on libcurl.so openssl.so then how to resolve it

Collapse
 
iprosk profile image
Igor Proskurin

I would say you'll need those libraries compiled to target wasm architecture, and then link them statically into your binaries. There might be a way to do it with shared libraries as well, but you'll still need a version compiled for wasm. I never tried it.

Thread Thread
 
aloth_naveen_4db184e14452 profile image
Aloth Naveen

cmake_minimum_required(VERSION 3.13)
project((*********)

Set C standard to C99 and enforce its usage

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

Add required compiler flags

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWASM=1 -Wall -Wimplicit-function-declaration")

Include directories

include_directories(
${CMAKE_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/libs/openssl/arm64-v8a/include
${CMAKE_SOURCE_DIR}/libs/curl/arm64-v8a/include
)

Add library dependencies

file(GLOB OPENSSL_LIBS "${CMAKE_SOURCE_DIR}/libs/openssl/arm64-v8a/lib/.a")
file(GLOB CURL_LIBS "${CMAKE_SOURCE_DIR}/libs/curl/arm64-v8a/lib/
.a")

message(STATUS "OPENSSL_LIBS: ${OPENSSL_LIBS}")
message(STATUS "CURL_LIBS: ${CURL_LIBS}")

Add source files

add_executable((*********

***.c
*****.c
******.c
******.c
********.c
Enter fullscreen mode Exit fullscreen mode

)
target_link_libraries((*********
${OPENSSL_LIBS}
${CURL_LIBS}
)

Link libraries

target_link_libraries((*********

${OPENSSL_LIBS}

${CURL_LIBS}

)

Add definitions to avoid implicit declarations

target_compile_definitions((********* PRIVATE
-D_POSIX_C_SOURCE=200809L
-D_GNU_SOURCE
)

Include headers globally

target_include_directories(********* PRIVATE

# ${CMAKE_SOURCE_DIR}/libs/openssl/arm64-v8a/include
# ${CMAKE_SOURCE_DIR}/libs/curl/arm64-v8a/include

)

Debug output for diagnostics

message(STATUS "CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")
message(STATUS "CMAKE_C_STANDARD: ${CMAKE_C_STANDARD}")

wasm-ld: warning: libs/curl/arm64-v8a/lib/libcurl.a: archive member 'libcurl_la-libssh2.o' is neither Wasm object file nor LLVM bitcode
wasm-ld: warning: libs/curl/arm64-v8a/lib/libcurl.a: archive member 'libcurl_la-wolfssh.o' is neither Wasm object file nor LLVM bitcode

i given static library of opensll and libcurl... but final wasm out out is 4kb only is it library are included or not
can you help me to link those libraries