Belajar hybrid-programming C# dan C++ dengan Haversine
This project is a hybrid-powered Haversine calculator built with:
- 💻 C# → for the OOP structure, terminal output, and clean logic
- ⚙️ C++ → for math-heavy functions like
Haversine::Hav_deg()andcalculate_distance()to boost performance
We're blending the beauty of .NET and the raw speed of C++ using DllImport.
. # C# files (flatted)
├── Program.cs # Main entry point
├── Location.cs # Represents a coordinate (lat, lon)
├── Distance.cs # Distance calculator (calls C++ backend)
├── Haversine.cs # \[Replaced by native C++]
├── Symbols.cs # Greek and math symbols
├── Misc.cs # Terminal formatting, etc.
└── NativeInterop.cs # DllImport for calling C++ functions
.\test # Testing files
├── Program_test.cs # Testing för Main entry point
├── Hello.cpp # Testing för Represents a coordinate (lat, lon)
├── Hello.ll # Compiled to LLVM Intermediate Level
└── Hello.dll # Comepiled thing
.\SRC_CPP # C++ Files
├── Distance.h
├── Haversine.h # C++ haversine functions (deg2rad, hav, etc)
├── Includes.h # Shared includes
├── Location.h # Location object stuffs
├── Main.cpp # Main file
├── Misc.h # Greek and math symbols
└── Symbols.h # Unicode + ANSI codes
- 🌐 Convert degrees to radians
- 📏 Calculate Haversine value (in degrees or radians)
- 🧠 Move heavy calculations to C++ for better performance
- 📦 Pure DLL-based interop (no C++/CLI, no COM hell)
With Clang (recommended):
clang++ -shared -o CPP_Main.dll .\CPP_SRC\CPP_Main.cpp -O3 -static -fPICWith MSVC (Visual Studio Command Prompt):
cl /LD CPP_Main.cpp /I. /O2 /FeCPP_Main.dllMake sure to place the resulting
CPP_Main.dllin your C# project’s output folder (bin/Debug/...).
Inside Program.cs:
[DllImport("CPP_Main.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern double hav_deg(double angle);Then call it in your app:
double result = hav_deg(60.0);
Console.WriteLine($"Hav(60°) = {result}");- Make sure architecture matches (x64 vs x86)
- Don't use C++
std::string,fmt, or classes directly through interop — keep exported functionsextern "C"and C-style - If you use
fmt::println()in C++, make surefmtis linked properly (or comment it out during interop)
- Export full
calculate_distance(lat1, lon1, lat2, lon2) - Benchmark
C# onlyvsC# + C++ hybrid - Create a GUI (e.g., WinForms/WPF) for interactive geo distance calculator
- Add multi-threaded batch distance calculations in C++
Built with caffeine, a love for both high-level structure and low-level control, and pure curiosity for hybrid programming.
MIT