[Haskell-cafe] FFI, C/C++ and undefined references

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Wed Jan 13 01:11:50 EST 2010


On Jan 13, 2010, at 00:57 , DNM wrote:
> ------------- srilm.c ----------------
> // Initialize and read in the ngram model
> Ngram* bldLM(int order, const char* filename) { ... }
> ...
> // Delete the ngram model
> void deleteLM(Ngram* ngram) {
>  delete srilm_vocab;
>  delete ngram;
> }
> ...
> // Get the ngram probability of the given string, given n-gram order  
> 'order'
> and string length
> // 'length'.
> float getSeqProb(Ngram* ngram, const char* ngramstr, unsigned order,
> unsigned length) { ...}
> -----------------------------


I think you need to `#include "srilm.h"' in the above, so C++ knows to  
export the functions with names callable from outside of C++.

When you define a function in C++, the actual function symbol defined  
contains parameter type information unless previously declared in an  
extern "C" declaration.)  While you've named the file with a .c  
extension, you have used C++-specific content (// comments, "delete"  
keyword) so I expect the file was compiled in C++ mode; otherwise it  
should have produced a syntax error from "delete" ("//" comments are a  
common enough extension that by themselves they probably work in much  
C code) and as a result the function symbols are mangled.

If you add the #include, you bring the extern "C" declarations in  
scope, and C++ should produce non-mangled function definitions, which  
should be callable from Haskell.

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20100113/35cef4ef/PGP.bin


More information about the Haskell-Cafe mailing list