Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the dynamic link library generated by CAccord +

Shulou Source: shulou.com Published: 2022-06-02 02:00:01 10月04日 Update

This article mainly explains "how to use the dynamic link library generated by CAccord +". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the dynamic link library generated by CAccord +".

Operating environment: Visual Studio 2017

Although .NET Core is cross-platform, it is impossible to develop using C # everywhere, just as no one uses SQL to develop Android APP, and each language has its own advantages and limitations.

For sensors, 32-bit, 16-bit embedded devices, there may be only a few KB memory, those serial ports, pins, circuits, signals, can only be developed in C language. As far as Linux is concerned, Linux itself is developed in C language, which has rich interface support and compatibility for C language, and there are many places that can only use C language.

The underlying application can be completed in C language, what if you want to develop a website or a large application?

We can first write the underlying API interface in C language, generate .so dynamic link library, and then use C # to introduce C language dynamic link library. Use C language for underlying interface development, use .NET Core/C# to achieve cross-platform application development, through pre-defined API, you can control the overall situation.

First, install CAccord Category + support on Visual Studio 2017

The purpose of this step is to develop a set of programs on VS 2017. You can also use other tools to develop C language programs, or even write and compile them in vim in Linux.

Second, write a simple C language program and generate a dynamic link library

After creating the project, click Properties and select generate .so

Then create a new test.cpp file, right-create a new item, and the C language code is as follows

Extern "C" {}

Indicates that the method functions within {} can be used externally

Extern "C" {int Sum (int aline int b) {return a + b;}}

Right-generate

You will then be asked to add a Linux host connection. Actually, it is useless to add here, but vs must be added by you to generate and compile, which actually has no effect on our later operations.

Next, look at the picture operation.

Click generate menu bar-for projects only-link xxx only

Then the following output column will report an error, regardless of him, and then click on the project-right-generate

After that, the .so file will be generated, copied and stored.

Third, C# projects use this dynamic link library

Create a .NET Core project, copy the .so file in, and remember to "always copy"

Create a new class, the class name is not important, the main thing is that the method name and parameters should be consistent.

Head introduction

Using System.Runtime.InteropServices

The code for the class is as follows

Then use this type of library

Public class Test {[DllImport (". / libProject2.so", CallingConvention = CallingConvention.Cdecl)] public static extern int Sum (int apenint b);} class Program {static void Main (string [] args) {Console.WriteLine (Test.Sum (666666)) While (true) {Console.WriteLine ("enter two numbers separated by spaces, such as 666 666"); string [] vs = Console.ReadLine () .Split (""); int a = Convert.ToInt32 (vs [0]); int b = Convert.ToInt32 (vs [1]) Console.WriteLine (Test.Sum (afort b));}

Package and run in Linux after release

Thank you for your reading, the above is the content of "how to use the dynamic link library generated by Cpicket +". After the study of this article, I believe you have a deeper understanding of the problem of how to use the dynamic link library generated by Cplink +, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Build language link development dynamic project file bottom layer interface program programming learning application consistency code content place method language program support Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno Huawei Shulou Tech Info Redmi