Finding a library for native software

Problem: I need to be able to compile a C++ project on Windows, Linux, or OSX and link in native/system libraries. For example, I need WS2_32.Lib on Windows, but not on Linux or OSX. This also needs to work across computers. So if someone has installed Windows on D: then I need to still be able to find WS2_32.Lib without changing the gradle.build file. Same goes for other operating systems.

Current work: from what I’ve seen, every single example or forum solution to a similar problem has the developer adding in a hard coded path to a PrebuiltLibraries section of the gradle.build file. Hard coding a path here will only work for me on one computer, not across many computers and operating systems. I’ve seen some solutions where it is suggested that multiple paths can be used, but this still doesn’t fix my problem where I need to be robust to different system configurations.

I think the typical solution to this problem would be to setup the default environment variables for paths (e.g., LD_LIBRARY_PATH, LIBPATH, LIB, etc.) with the paths to all of the libraries I need to find (i.e., push the ‘find’ portion of the task onto system configuration). The compiler/linker should then take over and be able to use the default path to find the appropriate libraries. This solution would work for me, even if it isn’t 100% ideal (100% ideal would be no configuration). However, I’ve tried that approach and it doesn’t seem to work with gradle. Does gradle overwrite those and have its own way of inputting those? Is there some other environment variable I should set to work with gradle? I haven’t been able to find a good answer to those questions so far.

My next hope is to try pulling in the environment variables myself and setting the library path via cppCompiler.args. I’m verifying that as a solution right now, but is there a better way of doing this that I’m just not seeing?

Thanks @seanwhitsitt for this question. It is completely possible to solve this issue with Gradle right now. First, we need to define what exactly you need to include as 3rd parties libraries. You mentioned WS2_32.lib which is part of the Windows SDK. You have four solution path for such libraries:

  1. you have everyone install their library however they want and provide configuration (for example in their ~/.gradle/gradle.properties file)
  2. you ask everyone to install their library in a known location
  3. you have everyone install their library however they want and build some logic to find the libraries on the system according to the knowledge of the 3rd party library bundle
  4. you build logic where Gradle is in charge of pulling the 3rd party library bundle and install it in a known location

I had success with option 4 as it make setting CI slave and onboarding new developers easier. But solution 3 or even 1 can be good for your case. It really depends how complicated it is to find the 3rd party libraries. With the work we are currently doing for native, we are starting to look into how to properly manage 3rd party libraries within Gradle. It will help for those use cases. At the moment, you need to write some more code for your needs.

Don’t hesitate to ask more questions,

Daniel