Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to use a header-only C++ library in Unity WITHOUT using a dll?

Discussion in 'Editor & General Support' started by wechat_os_Qy08O4uD4os_Q3aM60rRiZU7Y, Oct 12, 2022.

  1. wechat_os_Qy08O4uD4os_Q3aM60rRiZU7Y

    wechat_os_Qy08O4uD4os_Q3aM60rRiZU7Y

    Joined:
    Sep 29, 2022
    Posts:
    7
    Hello, I have a very specific problem. I need to implement a header-only C++ library named Eigen for my WebGL unity build. I have found this solution https://longqian.me/2017/02/24/eigen-in-unity/. Which does not apply to my case, as dynamic library is not supported with WebGL.
    I have also attempted to use the whole Eigen library as a plugin, and reference it in another native plugin cpp file, then calling the cpp functions from c#. However attempting to compile with the library in Unity caused an error of "Plugins colliding with each other" Can anyone give me some ideas or solutions for similar problems? Thanks a lot!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    I think for WebGL you would need to compile the C++ code with something like Emscripten, then include the resulting javascript into your app and call it from Unity.
     
  3. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,516
    On any platform that uses IL2CPP (like WebGL), you can just drop the C++ files inside the Unity project and Unity will compile them for you with correct settings for the target platform when you build your project. You can then call those functions using the "[DllImport("__Internal")]" attribute.

    That should be fairly straightforward to fix. You likely copied multiple versions of files that are named the same. The error message should contain the files that collide so you can fix them.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    WHAAAAAT!!!!? Are you serious?! This didn't use to be a thing for WebGL.

    Time to see if I can get my old MS-DOS, PalmOS native C/C++ project (KurtMaster2D) to build for WebGL.

    I already have all games 100% operational on PC, Mac, iOS and Android and Android TV thanks to Unity's awesomeness... if I can stand this up in WebGL my target circle will be complete.
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,516
  6. wechat_os_Qy08O4uD4os_Q3aM60rRiZU7Y

    wechat_os_Qy08O4uD4os_Q3aM60rRiZU7Y

    Joined:
    Sep 29, 2022
    Posts:
    7
    Thanks a lot for the speedy reply! Indeed that was what I have been trying to do, I managed to get it working after trimming the files which are intended for different platforms. However this manually trimming seems somewhat problematic since I will be working with others in this project in the future, is there a cleaner way for me to pack them into something like a .lib? Again, thanks a lot!
     
    Kurt-Dekker likes this.
  7. wechat_os_Qy08O4uD4os_Q3aM60rRiZU7Y

    wechat_os_Qy08O4uD4os_Q3aM60rRiZU7Y

    Joined:
    Sep 29, 2022
    Posts:
    7
    Sorry, scratch my previous question. I have found a simpler way to present my problem. Since Unity somehow compiles my cpp files, is there anyway to interface this process? Via perhaps a cmake file or something of the sort to solve the dependency problem without placing everything in the Unity folder.
     
  8. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    If this is a header-only library, it can't be directly compiled into a dll. Headers aren't compiled by themselves, they must be included into C/CPP files which call the functions in the header.

    You need to create a .cpp file with exported functions that wrap the header library functions you want to call from C#. These wrapper functions also must be exported as C functions, you cannot pass C++ classes through the interop layer. If the library API is C++ (using templates, inheritance, etc), you need to wrap a C API on top of it too.
     
  9. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,516
    The only way to configure how the .cpp files are compiled is by pushing the knobs in the plugin inspector, which you can access by clicking on individual .cpp files. There unfortunately aren't many configuration options but you can select which platforms the .cpp file should be included to.

    You could technically precompile your plugin into a ".bc" file (".lib" equivalent for WebGL), but getting compiler flags right might be tricky. You could introduce a compilation error in your .cpp file before exporting the Unity project: the build will fail but it will tell you exactly how Unity invokes the Emscripten compiler.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Could you speak more to this process? Is a .bc simply another form of .lib or .a file?

    I actually would prefer to combine all my C/C++ files into a library (the way I do for Mac, PC, Android and iOS presently) rather than restructuring to include the files directly.

    Is it this process: ?

    https://groups.google.com/g/llvm-dev/c/Jl_SXbgXcWk

    Or could you perhaps point me to whatever it actually is?
     
  11. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,516
    Yup exactly, that is my understanding of them. You can compile individual .cpp files into .o files with Emscripten and then "bundle"/"archive" them into a .bc file that can be used as a plugin in Unity (or linked against other Emscripten apps).
     
    Kurt-Dekker likes this.