Search code examples
c#linuxvisual-studiotizennui

Tried accessing my custom shared object, but permission is denied


Created a basic C++ shared objects file using g++:

// calculate.hpp
#ifdef __cplusplus
extern "C"
{
#endif

  // typedef struct ...;
  // function decls
  int Add(int x, int y);

#ifdef __cplusplus
}
#endif

with a C++ file that implements the functions:

// calculate.cpp    
#include <iostream>
#include "calculate.hpp"
    
int Add(int x, int y) {
    return x + y; 
}

and I basically created the shared library via

g++ -fPIC -shared calculate.cpp -o calculate.so

My C# app on my Linux system can run it flawlessly:

// sandbox.cs
using System.Runtime.InteropServices;

namespace sandbox;

class Program
{
    // (changed the directory due to privacy stuff)
    [DllImport("/.../shared-objects/calculate/libcalculate.so")]
    static extern int Add(int x, int y);

    static void Main(string[] args)
    {
        int result = Add(15, 20);
        Console.WriteLine($"Result of so is: {result}");
    }
}

However, when I try to run it via a Tizen application package (a C# app for the TV) through my Smart TV, this is what I get:

System.DllNotFoundException: Unable to load shared library '/home/guest/libtest/libcalculate.so' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: /home/guest/libtest/libcalculate.so: cannot open shared object file: Permission denied

As you can see, I have the .so file in /home/guest/, but I also tested it in other places: /lib/, /opt/data, /home/. Just anywhere I can think of. Still the same exception: permission denied.

I modified the file through chmod too (currently -rwxrwxrwx) and made sure my ls -ld of / /usr and /usr/lib was all good, and still permission denied. Now the question is whether this actually is an access-related issue, or more to do with how app packages are handled on a TV?


Solution

    1. Your binary must be built for the armel architecture, not the architecture of your Linux PC (x64). You can cross compile your code by installing g++-arm-linux-gnueabi (replace g++ with arm-linux-gnueabi-g++ in your build command). You can alternatively use Tizen Studio to create a shared library project but I wouldn't recommend it (because there is no C++ SDK available for the TV profile).

    2. You should put the built binary in the right place. Go to your app project directory (where the .csproj file exists), create the lib directory if it doesn't exist, and copy the libcalculate.so file to either lib/ or lib/armel/. Then you can DllImport("calculate.so") to load the library in your C# code.