Home > Back-end >  C How to include a third party library into your library without compiling it?
C How to include a third party library into your library without compiling it?

Time:10-04

I am building my first C library (header-only) and I want to include a third party library like Crypto . Now I believe I am supposed to compile the Crypto library and put the compiled archive inside the ./lib directory and pass the linker commands when compiling my library. But since I do not want to compile the 3rd party library for every OS and I am building a header-only library I would like to include the 3rd party library without compiling it. Is there a way to I achieve this? BTW the Crypto library is not a header-only library.

The hierarchy of my project is as follows:

ProjectName/
    ./include/
        ./ProjectName/
           ...
        ./internal/
            ./cryptopp/
               ... source files ...
    ./lib/

When I try to include internal/cryptopp/sha.h without passing the linker arguments to the compiled library it throws the Undefined symbol ... errors as expected. Is there a way I can achieve this? Or is there no other way than compiling the library and putting the compiled archive inside the ./lib/ dir and passing the correct linker arguments?

I am aware that this is quite a newbie question, I have just never done this before and C is still quite new for me.

CodePudding user response:

You are developing a header-only, platform-independent C library that happens to depend on a traditional (i.e. not header-only) C library, but which is also portable to a wide range of target platforms.

What you need to tell your users is that your library depends on that other library (crypto in this case), and ask them to install this library before attempting to use your header-only library, and to also link against crypto when compiling executables or libraries that use your library.

Do not distribute the crypto source code yourself as part of your library, because your users should and will want to update the crypto library that they use whenever it receives an update, independent of your library. This is especially important with regard to cryptography libraries because of the possible security implications.

CodePudding user response:

As the accepted answer above says, the primary method is to make an advice that your project has a dependency on a 3rd party library.

That said, another option that most projects use when in need of 3rd party projects is to use git submodules. There is a nice tutorial from Github.

With git submodules, your git repository will only contain your own code but also will have a reference to an external git repository (to be placed in your 'lib' folder). Then when the user clones your repository, git will (optionally) bring into all the 3rd party libraries as well.

Another interesting mechanism to bring 3rd party projects at compile time is CMake ExternalProject. It will download and build a github project at compile time for you so only apply to very small dependencies.

One such example is a regex performance shootout that brings in tons of 3rd party, small libraries and uses both methods:

https://github.com/rust-leipzig/regex-performance

  •  Tags:  
  • c
  • Related