paullouisageneau / libdatachannel

C/C++ WebRTC network library featuring Data Channels, Media Transport, and WebSockets
https://libdatachannel.org/
Mozilla Public License 2.0
1.81k stars 366 forks source link

Have an option to install either static or dynamic library #685

Closed Marwan-imverse closed 1 week ago

Marwan-imverse commented 2 years ago

It would be great to have the option to install either the static or the dynamic library version.

Since there are two different CMake targets, it's already possible to build either one, but the install target automatically builds both, we have no choice.

paullouisageneau commented 2 years ago

The install target should only build and install the dynamic library, not the static ones. Are you sure it builds both for you?

Marwan-imverse commented 2 years ago

I misspoke at the end of my first comment. If I choose to "install" after I built the static library, it builds and installs the dynamic library, which is not what I'd like. The install target should either install the dynamic or the static library, depending on how the project was generated with CMake.

paullouisageneau commented 2 years ago

OK I see. I think a preliminary step for this would be to bundle the static libraries of submodules into the static library of libdatachannel.

Marwan-imverse commented 2 years ago

Great, thanks! I'll keep an eye on this

ClayJay3 commented 2 weeks ago

@paullouisageneau I think this is still an issue, has any development been made? I'm currently able to build the static library just fine with make datachannel-static, but then running either make install or make install datachannel-static doesn't install the library statically, but instead dynamically.

I suppose I could manually copy everything needed for the static install, but I wanted to check if there's an 'official' way to do this.

ClayJay3 commented 2 weeks ago

I got the library to statically install with

cmake \
    -D USE_GNUTLS=0 -D USE_NICE=0 \
    -D BUILD_SHARED_LIBS=OFF \
    -D CMAKE_BUILD_TYPE=Release ..

Then, the normal make install will work.

However, it still seems stuff is broken, when I try to include the library in my project with find_package() I get this: image

paullouisageneau commented 2 weeks ago

@ClayJay3 The libraries are installed but I guess that for CMake to find installed dependencies, you also need to add them to the CMake export set. It should be fixed with https://github.com/paullouisageneau/libdatachannel/pull/1287

ClayJay3 commented 2 weeks ago

I checked out to cmake-add-install-deps-export and it seems to have fixed the issue with the make install command ignore that I want it to install the static libs, and I can go manually check /usr/local/lib/libdatachannel.a and it exists. however it seems like the install library is still broken :(

When I add the library to my project with find_package() like this: image image It gives me this: image Switching the link library to LibDataChannel instead of LibDataChannelStatic seems to find the library (is LibDataChannelStatic still relavant?), but complains about OpenSSL for some reason: image

For reference, this is how I'm building and installing libdatachannel:

# Download LibDataChannel
    git clone --recurse-submodules --depth 1 --branch cmake-add-install-deps-export https://github.com/paullouisageneau/libdatachannel.git libdatachannel
    mkdir libdatachannel/build
    cd libdatachannel/build

    # Build LibDataChannel
    cmake \
    -D USE_GNUTLS=0 -D USE_NICE=0 \
    -D BUILD_SHARED_LIBS=OFF \
    -D CMAKE_BUILD_TYPE=Release ..

    # Install LibDataChannel
    make datachannel-static
    make install datachannel-static
ClayJay3 commented 2 weeks ago

Forgot to mention that trying

## Find LibDataChannel
find_package(LibDataChannel-Static REQUIRED)

Does not find anything either.

paullouisageneau commented 2 weeks ago

The change introduced in https://github.com/paullouisageneau/libdatachannel/pull/1274 makes the LibDataChannel default target obey the CMake flag BUILD_SHARED_LIBS, so you can now build it statically and install it.

The LibDataChannelStatic target is still not installable, because CMake doesn't support optionally installing a configured target. Don't try to install it.

You simply have to run:

$ mkdir libdatachannel/build
$ cd libdatachannel/build
$ cmake -D USE_GNUTLS=0 -D BUILD_SHARED_LIBS=OFF -D CMAKE_BUILD_TYPE=Release ..
$ make
$ make install

Then you can import it with:

find_package(OpenSSL REQUIRED)
find_package(LibDataChannel REQUIRED)
[...]
target_link_libraries(${EXE_NAME} PRIVATE LibDataChannel::LibDataChannel)

You have to provide the same OpenSSL version as during the build since static libraries are linked only when linking the executable.

ClayJay3 commented 1 week ago

1287 seems to have fixed my issue with the static build. I think this issue can be closed once that PR is merged.