简体   繁体   中英

Calling C function (Linux) from C# (Windows)

I found lots of information on calling C functions from C# on the interwebs. Simple solution is creating a .so file and importing it using a DllImport Function is C#.

using System;

using System.Runtime.InteropServices;

public class Tester 
{
        [DllImport("libtest.so", EntryPoint="cfunction")]

        static extern void cfunction(string message);

        public static void Main(string[] args)
        {

                cfunction("Hello World C# => C++");
        }
}

My question is, does it matter if my .so file was created in a Linux environment? Can I just copy the .so file to a Windows environment and have C# use it without any issues? I believe it shouldn't matter because .so is a dynamic library and is filled with symbols not dependent on the platform it's created on.

Thanks for the help.

does it matter if my .so file was created in a Linux environment ?

Yes, it matters.

Shared Object (so) files are generated by linkers and contain code. They are platform specific. Even two versions of Linux running on two different CPUs are quite likely to be unable to use the same .so file.

Can I just copy the .so file to a Windows environment and have C# use it without any issues ?

A Windows OS will not use an .so file. It requires .dll files and they are a completely different format ( and in general require completely different code to do the same job ).

I believe it shouldn't matter because .so is a dynamic library and is filled with symbols not dependent on the platform it's created on.

It is not simply a file full of symbols. It is code that will be executed as well. And that code is platform specific.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM