question

FrankNatoli-2507 avatar image
0 Votes"
FrankNatoli-2507 asked SimpleSamples edited

Can C# for Linux [Centos8] call other languages

I understand C# is available for Linux specifically Centos8:
https://docs.microsoft.com/en-us/dotnet/core/install/linux-centos
But can an application developed in C# for Linux/Centos8 call into libraries compiled for other languages, e.g., C++ or C?

dotnet-csharpdotnet-ad
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

SimpleSamples avatar image
1 Vote"
SimpleSamples answered SimpleSamples edited

It depends on the definition of call. Originally computer programs were only capable of static linking where multiple source files were compiled (by a compiler) then combined into a single executable file by a linker. That was how programming was originally done in Unix. About 40 years ago (before Windows) compilers (then operating systems) began to support dynamic linking. When Windows was developed it relied heavily on dynamic linking, right from the initial design. I believe that Windows supported dynamic linking before Unix did.

The .Net Framework does not support static linking. It is not possible to combine multiple computer languages in a single managed (.Net) executable file. It is possible to create a dynamic link library using a computer language such as C or C++ and call functions/methods in that library from a managed executable. I hope the following help.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'll have to try it, but I believe your first and third options above, "Cross Platform P/Invoke" and/or "Interop with Native Libraries | Mono", will do what I need to do.
I was not intent on static linking, just anything that would permit a Linux C# main program to make use of [call] a C++ derived library without having to involve multiple processes.
Many thanks.

0 Votes 0 ·

Many languages have managed versions, especially C++ but not C. I described Platform Invoke since you asked about languages in general. It is possible to write a managed C++ class library that can be called by C# in the same way as if the C++ class library is a C# class library, but I am not totally sure about the details of that for Linux. Managed C++ code can call unmanaged C++ and C, even using static linking.

0 Votes 0 ·