How to make existing legacy C++ dlls cross platform? (Run on linux)

Anonymous
2023-07-28T12:02:59.52+00:00

Hi Team,

We are trying to make our existing application(Say, ExtractorApp) which was originally build for windows to now run in linux. Extractor App was initially built using framework4.7.2 is now cross-platform. We converted the console project of the app to net6.0 and other dependent projects to netstandard2.0 and it is working in linux after publishing it onto ubuntu using the dotnet publish command.

This application uses a c++ dll and we are facing an issue with CPP code not being able to run on Linux. We need your suggestion on any existing tools we can use to achieve this if the cpp code needs to be re-written to make it crossplatform

  • We have a legacy CPP code (say, B.dll) which is inside a C# library(say, A.dll). The A.dll is used inside the main ExtractorApp
  • We have converted C# library(that is A.dll) to netstandard2.0 using the try-convert command to make it cross platform. However the managed cpp code(B.dll file) when run on linux machine is giving segmentation fault error
  • if we try to publish the A.dll which is in netstandard2.0 now(which also has reference to the cpp code), it is giving error "The imported project 'C:\Microsoft.cpp.default.props' was not found. Confirm that the expression in the import declaration '/Microsoft.cpp.default.props' is correct and the file exists on the disk"
  • Can you please let us know if the managed CPP code can also be converted to a cross platform code using any tool or command like we did for c# libraries so that it can run on both Windows and Linux
  • Or can you please suggest any other way to make this existing csharp+cpp code, cross platform.

Thanks & Regards, Surabhi

Developer technologies | C++
{count} votes

1 answer

Sort by: Most helpful
  1. Penchala Nihar 230 Reputation points
    2023-07-28T12:17:02.42+00:00

    To make your existing C++ DLL cross-platform and run on Linux, you'll need to perform several steps:

    1. Verify C++ Code Compatibility: Ensure that your C++ code is platform-independent and does not rely on Windows-specific APIs or libraries. Make sure that you are using standard C++ and avoid any Windows-specific calls.
    2. Build for Linux: You'll need to compile your C++ code on Linux to create a Linux-compatible shared library (.so file). To do this, you can use the GCC (GNU Compiler Collection) or another C++ compiler on Linux.
    3. Use P/Invoke or C++/CLI (Windows): In your C# project, if you are using P/Invoke or C++/CLI to interact with the C++ DLL, ensure that any platform-specific code is properly wrapped or conditionally compiled so that the appropriate platform-specific code is used based on the operating system.
    4. Use Cross-Platform Libraries: Replace any Windows-specific libraries or APIs used in your C++ code with cross-platform alternatives that work on both Windows and Linux.
    5. Handle Path and File System Differences: Be aware of path and file system differences between Windows and Linux. Ensure that your code properly handles file paths and file I/O operations in a cross-platform manner.
    6. Test Extensively: Thoroughly test your cross-platform application on both Windows and Linux to identify and fix any platform-specific issues or inconsistencies.

    Regarding the error you mentioned: "The imported project 'C:\Microsoft.cpp.default.props' was not found," this suggests that the build configuration or project files for the C++ code are referencing a Windows-specific location. You'll need to adjust the build configurations and paths to be cross-platform compatible.

    Keep in mind that achieving full cross-platform compatibility may require some code changes and adjustments to accommodate the differences between Windows and Linux environments. Not all C++ code that runs on Windows will work seamlessly on Linux, especially if it relies heavily on Windows-specific APIs.

    Additionally, consider using cross-platform libraries and tools, such as CMake, that can help simplify the build process and handle platform-specific configurations in a more unified manner.

    Ultimately, making a complex C++ codebase cross-platform may require some effort and testing, but with careful adjustments and adherence to cross-platform coding practices, it is feasible to achieve compatibility on both Windows and Linux.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.