4 ms·
In theory, yes. In practice, no. The C emitted from compilers is usually an awful mess that looks like Assembly code and it might not have sane function entry p
by exDM69 14y ago
In theory, yes. In practice, no. The C emitted from compilers is usually an awful mess that looks like Assembly code and it might not have sane function entry points so it could be used as a library. Then you need to get the runtime system (garbage collector, etc) up and running to run your code. It's by no means impossible but it's very impractical.
Historically, C was used to implement many (functional) programming languages because it provided a convenient compiler back end and a well-defined intermediate representation between the front end and the back end. Not because you could compile to C and use your code from C.
- danieldk 14y agoFortunately, GHC can build shared libraries on some platforms, which makes it fairly easy to expose Haskell functions to C: - Using the FFI one can specify foreign exports. - The runtime and garbage collector can be initialized/deinitialized by adding an object file that uses gcc's constructor/destructor attributes. - Multiple calls to hs_init (one for each library) are allowed, as long as they are balanced by hs_exits. - It's ok to have multiple root modules.
- exDM69 14y agoIndeed, the FFI in GHC is one of the best ones out there. It makes it convenient to call C from Haskell and vice versa(!). I wish every language had an FFI and RTS that good!