0

I'm trying to build a project which links to openssl Using CMake. I'm getting errors at the linking stage. One of the shared libraries cannot find certain openssl functions even tho that shared library is build successfully.

The portion of the error output that highlights the errors is:

[ 61%] Building C object CMakeFiles/uSockets.dir/_deps/usockets_content-src/src/quic.c.o
[ 66%] Building C object CMakeFiles/uSockets.dir/_deps/usockets_content-src/src/socket.c.o
[ 72%] Building C object CMakeFiles/uSockets.dir/_deps/usockets_content-src/src/udp.c.o
[ 77%] Linking C shared library libuSockets.so
[ 77%] Built target uSockets
[ 83%] Building CXX object CMakeFiles/client.dir/client_ws.cc.o
[ 88%] Linking CXX executable client
[ 88%] Built target client
[ 94%] Building CXX object CMakeFiles/server.dir/server_ws.cc.o
[100%] Linking CXX executable server
/usr/bin/ld: libuSockets.so: undefined reference to `us_internal_ssl_socket_context_on_long_timeout'
/usr/bin/ld: libuSockets.so: undefined reference to `us_internal_ssl_socket_get_native_handle'
/usr/bin/ld: libuSockets.so: undefined reference to `us_internal_create_child_ssl_socket_context'
/usr/bin/ld: libuSockets.so: undefined reference to `us_internal_ssl_socket_get_sni_userdata'
/usr/bin/ld: libuSockets.so: undefined reference to `us_internal_ssl_socket_context_connect_unix'

The full cmakelists.txt is at https://pastebin.com/TVrYhMVc

The full cmake output is at https://pastebin.com/1Bvd8GzQ

Any ideas what I'm missing?

9
  • 1
    Do not use include_directories. It was valid in old cmake. Use modern cmake patterns: target_link_libraries(your Taqrget PRIVATE OpenSSL::SSL OpenSSL::Crypto). Commented Nov 12, 2024 at 14:55
  • Also you should not use pastebin especially content is short. Just copy paste this into a question. Commented Nov 12, 2024 at 14:56
  • The cmake output was too long for the question. I tried changing the target_link_libraries to use PRIVATE and nothing changed. Thanks for the help tho Commented Nov 12, 2024 at 15:00
  • Note that library github.com/uNetworking/uSockets is build by Make not CMake! So you have to import it in more complex way so Make is invoked. Commented Nov 12, 2024 at 15:00
  • 3
    On Stack Overflow we expect the relevant code to be in the question post itself. Links to pastebin or other sites do not count. See also How to Ask Commented Nov 12, 2024 at 17:07

2 Answers 2

1

This line:

target_compile_definitions(uSockets INTERFACE LIBUS_USE_OPENSSL)

defines the LIBUS_USE_OPENSSL symbol for targets that link to uSockets. Change the linking type to PUBLIC so it is used when building the library target itself, too.

EDIT: The usockets library also contains two .cpp files you need to include in your GLOB_RECURSIVE call:

FetchContent_Declare(
        uSockets_content
        GIT_REPOSITORY https://github.com/uNetworking/uSockets
        GIT_TAG v0.8.8
        GIT_SHALLOW ON
        GIT_SUBMODULES ""
)
FetchContent_MakeAvailable(uSockets_content)
file(GLOB_RECURSE USOCKETS_SOURCES ${usockets_content_SOURCE_DIR}/src/*.c ${usockets_content_SOURCE_DIR}/src/*.cpp)
add_library(uSockets SHARED ${USOCKETS_SOURCES})
target_include_directories(uSockets PUBLIC ${usockets_content_SOURCE_DIR}/src ${ZLIB_INCLUDE_DIRS} )
target_compile_definitions(uSockets PUBLIC LIBUS_USE_OPENSSL)
target_link_libraries(uSockets ${ZLIB_LIBRARIES} OpenSSL::SSL OpenSSL::Crypto)
Sign up to request clarification or add additional context in comments.

2 Comments

Tried this and it makes no difference. I think the problem is that I need to include dirs which contain the definitions of the funtions that are missing. These arent actually openssl functions, instead they are defined in code which is included using an ifdef when the openssl switch is passed.
And that symbol was not set because you used INTERFACE instead of PUBLIC. Edit your question with --verbose build output
0

Did some experimetns and if builds on my machine with this version of CMakeLists.txt

cmake_minimum_required(VERSION 3.28)
project(entitywind_uwebsockets_test LANGUAGES C CXX)

include(FetchContent)
set(CMAKE_CXX_STANDARD 23)
 
find_package(OpenSSL REQUIRED)
 
FetchContent_Declare(
        uSockets_content
        GIT_REPOSITORY https://github.com/uNetworking/uSockets
        GIT_TAG v0.8.8
        GIT_SHALLOW ON
        GIT_SUBMODULES ""
)
FetchContent_MakeAvailable(uSockets_content)
file(GLOB_RECURSE USOCKETS_SOURCES
    "${usockets_content_SOURCE_DIR}/src/*.c"
    "${usockets_content_SOURCE_DIR}/src/crypto/*.cpp"
)

add_library(uSockets SHARED ${USOCKETS_SOURCES})
target_include_directories(uSockets PUBLIC ${usockets_content_SOURCE_DIR}/src)
target_compile_definitions(uSockets INTERFACE LIBUS_USE_OPENSSL)
target_link_libraries(uSockets PUBLIC OpenSSL::SSL OpenSSL::Crypto)
target_compile_definitions(uSockets PRIVATE LIBUS_USE_OPENSSL)

FetchContent_Declare(
        uWebSockets_content
        GIT_REPOSITORY https://github.com/uNetworking/uWebSockets
        GIT_TAG v20.70.0
        GIT_SHALLOW ON
        GIT_SUBMODULES ""
)
FetchContent_MakeAvailable(uWebSockets_content)

add_library(uWebSockets INTERFACE)
target_include_directories(uWebSockets INTERFACE ${uwebsockets_content_SOURCE_DIR}/src/)
target_link_libraries(uWebSockets INTERFACE uSockets)
target_compile_options(uWebSockets INTERFACE -Wno-deprecated-declarations)
 
# Client exec
add_executable(client client_ws.cc)
target_link_libraries(client uWebSockets)
 
# Server exec
add_executable(server server_ws.cc)
target_link_libraries(server uWebSockets)

First of all I corrected USOCKETS_SOURCES. Note cpp files have to be included to have OpenSSL. I've found this based on reading uSocket Makefile:

# For now we do rely on C++17 for OpenSSL support but we will be porting this work to C11
ifeq ($(WITH_OPENSSL),1)
    $(CXX) $(CXXFLAGS) -std=c++17 -flto -O3 -c src/crypto/*.cpp
endif

Note also on this file turns out macro LIBUS_USE_OPENSSL have to be defined:

    ifeq ($(WITH_OPENSSL),1)
        override CFLAGS += -DLIBUS_USE_OPENSSL

I've also clean up a bit some cmake details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.