能否将MAVLink C库转为DLL及用Eclipse生成.so以在myRio1900的Linux RT调用?
Hey Alex, let's break down your two MAVLink library questions one by one—both are totally doable with the right steps, so let's dive in:
1. Converting MAVLink C Library to a DLL (Windows)
Here's a straightforward workflow to turn your MAVLink C code into a usable DLL:
- Step 1: Prepare your MAVLink code
First, ensure you have the full set of MAVLink C files generated (use the official MAVLink generator to pick your target protocol/vehicle and export the C library). Gather all relevant.cand.hfiles for your use case. - Step 2: Set up a DLL project in Visual Studio
Open Visual Studio, create a new Dynamic-link Library (DLL) project. Add all your MAVLink.cfiles to the project, and configure the include directories to point to your MAVLink header files. - Step 3: Export functions for external access
DLLs require explicit function exports so other programs can call them. Choose one of these two methods:- Use the
__declspec(dllexport)attribute: Add this macro to your main MAVLink header:
Prepend#ifdef MAVLINK_EXPORTS #define MAVLINK_API __declspec(dllexport) #else #define MAVLINK_API __declspec(dllimport) #endifMAVLINK_APIto every function you need to expose (e.g.,MAVLINK_API void mavlink_send_message(mavlink_channel_t chan, const mavlink_message_t* msg);). - Use a
.deffile: Create amavlink.deftext file in your project, list exported functions under theEXPORTSsection:EXPORTS mavlink_send_message mavlink_parse_char // Add other required functions here
- Use the
- Step 4: Compile the DLL
Build your project—Visual Studio will generate a.dllfile (in the Debug/Release output folder) and a corresponding.libimport library. Use the.libwhen linking your application to the DLL, and keep the.dllin the same directory as your executable at runtime.
2. Creating a .so Library for myRio1900 Linux RT Using Eclipse
The myRio1900 runs an ARM-based NI Linux RT system, so you'll need cross-compilation tools. Here's how to set this up in Eclipse:
- Step 1: Prepare Eclipse and cross-compiler
Install Eclipse CDT (C/C++ Development Tools) if you don't have it. Then install the NI myRio Toolkit—it includes thearm-linux-gnueabihf-gcccross-compiler tailored for the myRio's architecture. - Step 2: Import your MAVLink project
Create a new C project in Eclipse, select Empty Project, and choose the Cross GCC toolchain. Add all your MAVLink.cfiles to the project, and configure include paths to point to your MAVLink headers. - Step 3: Add compiler flags for shared libraries
Shared libraries require position-independent code. Go to Project Properties > C/C++ Build > Settings > GCC C Compiler > Miscellaneous and add-fPICto the "Other flags" field. - Step 4: Configure linker to generate .so
In the linker settings (Project Properties > C/C++ Build > Settings > GCC C Linker > Shared Library Settings), check the "Shared" checkbox (this adds the-sharedflag). You can also set the output library name (e.g.,libmavlink.so). - Step 5: Compile and deploy to myRio1900
Build the project to generate the.sofile. Transfer it to your myRio1900 using SSH/SCP, or NI's built-in file transfer tools. Place it in a system-accessible directory like/usr/libor add its path to theLD_LIBRARY_PATHenvironment variable. - Step 6: Link against the library
When compiling your myRio application (either cross-compiling or on-device), use-lmavlinkto link against the library, and-L/path/to/your/libraryto specify its location.
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

