简体   繁体   English

如何从Ocaml中调用C ++代码自己使用共享库.so?

[英]How from Ocaml can I call C++ code using itself a shared library .so ?

I need to build an Ocaml/C++ module which calls a shared object (.so under linux) 我需要构建一个调用共享对象的Ocaml / C ++模块(linux下的.so)

As long as it is a question to compile a simple Ocaml/C++ stub, I manage the thing but when I need to link the .so with ocamlmklib or ocamlopt, it fails 只要编译一个简单的Ocaml / C ++存根是一个问题,我管理的东西,但是当我需要将.so与ocamlmklib或ocamlopt链接时,它会失败

I work under gcc 4.5 (c++0x) 我在gcc 4.5(c ++ 0x)下工作

files for the shared object : 共享对象的文件:

hello.hpp hello.hpp

#include <iostream>
#include <string>

using namespace std;

class HelloApplication
{
public :

    HelloApplication();
    ~HelloApplication();

    void say(string s);

};

typedef HelloApplication *(*create_hello)();

hello.cpp : hello.cpp:

#include "hello.hpp"

HelloApplication::HelloApplication(){}
HelloApplication::~HelloApplication(){}

void HelloApplication::say(string s)
{
     cout << "Hello : " << s << endl;
}

extern "C"
{
    HelloApplication *create()
    {
        return new HelloApplication();
    }

}

the CMake.txt file to compile the thing : 编译事物的CMake.txt文件:

cmake_minimum_required(VERSION 2.6)

project(testHello_proj)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Release" FORCE)
#set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Debug" FORCE)

set(LIBRARY_OUTPUT_PATH lib/${CMAKE_BUILD_TYPE})

## Compiler flags
if(CMAKE_COMPILER_IS_GNUCXX)
    set ( CMAKE_CXX_FLAGS "-O2 -std=c++0x" 
        CACHE STRING "g++ Compiler Flags for All Builds" FORCE)

    set ( CMAKE_CXX_FLAGS_DEBUG "-std=c++0x -O2 -g -Wall"
        CACHE STRING "g++ Compiler Flags for Debug Builds" FORCE)

    set ( CMAKE_CXX_FLAGS_RELEASE  "-O2 -fmessage-length=0 -std=c++0x"
    CACHE STRING "g++ Compiler Flags for Release Builds" FORCE)

    set ( CMAKE_CXX_FLAGS_MINSIZEREL  "-Os -std=c++0x"
    CACHE STRING "g++ Compiler Flags for Release minsize builds" FORCE)

    set ( CMAKE_CXX_FLAGS_RELWITHDEBINFO  "-O2 -g1 -std=c++0x"
    CACHE STRING "g++ Compiler Flags for Release with Debug Info builds" FORCE)

endif()

file(
    GLOB_RECURSE
    source_files
    src/*
)

add_library(
    testHello
    SHARED
    ${source_files}
)

I get a library named libtestHello.so 我得到了一个名为libtestHello.so的库

now files of the Ocaml/C++ module, named mymod: 现在名为mymod的Ocaml / C ++模块的文件:

*mymod_stubs.cpp :* * mymod_stubs.cpp:*

#include <cstdlib>
#include <dlfcn.h>
#include <string>

#include "hello.hpp"

extern "C" {
#include <memory.h>
#include <mlvalues.h>
}

using namespace std;

HelloApplication* hello;

extern "C" value initHello (value unit) {
    CAMLparam1 (unit);

    create_hello hello_pMaker;

    void* hello_hndl = dlopen("/path_to_cmake_dir/build/lib/Release/libtestHello.so", RTLD_LAZY);

    if(hello_hndl == NULL)
    {
        cerr << "dlopen : " << dlerror() << endl;
        exit(EXIT_FAILURE);
    }

    void *hello_mkr = dlsym(hello_hndl, "create");
    if (hello_mkr == NULL)
    {
      cerr << "dlsym : " << dlerror() << endl;
      exit(EXIT_FAILURE);
    }

    hello_pMaker = (create_hello)hello_mkr;

    HelloApplication* hello_ptr(hello_pMaker());
    hello = hello_ptr;

    CAMLreturn (Val_unit);
}

extern "C" value say (value v_str) {
  CAMLparam1 (v_str);

  string s = String_val(v_str);

  hello->say(s);

  CAMLreturn (Val_unit);
}

mymod.ml : mymod.ml:

external initHello : unit -> unit = "initHello"

external say : string -> unit = "say"

caller.ml (test file) : caller.ml(测试文件):

Mymod.initHello;;

Mymod.say "tout le monde";;

Makefile : Makefile:

CPPSRC=mymod_stubs.cpp
CPPOBJ=mymod_stubs.o
CPPINC=-I/usr/local/lib/ocaml/caml -I/path_to_cmake_dir/src
CPPLIB=-std=c++0x
MODSRC=mymod.ml
MODNAME=mymod
OPTOBJ=mymod.cmx
OPTLIB=mymod.cmxa
CALLERSRC=caller.ml
OPTCALLERFLAGS=-I . -cclib
CALLERLIB=-lstdc++
OPTCALLEREXEC=caller.opt

all: opttest

#g++

cppcompile:
    g++ -o ${CPPOBJ} ${CPPLIB} ${CPPINC} -c ${CPPSRC}

#native

optcompile: cppcompile
    ocamlopt -c ${MODSRC}

optmklib:   optcompile
    ocamlmklib -o ${MODNAME} -ccopt -L/path_to_cmake_dir/build/lib/Release -cclib -ltestHello ${CPPOBJ}
    ocamlmklib -o ${MODNAME} -ccopt -L/path_to_cmake_dir/build/lib/Release -cclib -ltestHello ${OPTOBJ}

opttest:    optmklib
    ocamlopt ${OPTCALLERFLAGS} ${CALLERLIB} ${OPTLIB} ${CALLERSRC} -o ${OPTCALLEREXEC}

#clean

clean :
    rm -f *.cma *.cmo *.cmx *.cmxa *.cmi *.so *.a *.o ${OPTCALLEREXEC}

it compiles but I can't open the shared object libtestHello.so: 它编译但我无法打开共享对象libtestHello.so:

$: ./caller.opt ./caller.opt: error while loading shared libraries: libtestHello.so: cannot open shared object file: No such file or directory $:./。caller.opt ./caller.opt:加载共享库时出错:libtestHello.so:无法打开共享对象文件:没有这样的文件或目录

thanks for your help :) 谢谢你的帮助 :)

You probably need to pass -rdynamic and -Wl,-rpath at link time. 您可能需要在链接时传递-rdynamic-Wl,-rpath

(and I am not sure you can call Ocaml code from C or C++ code without care; you'll need to initialize the Ocaml runtime system; likewise the std C++ library might not work from an unpatched ocaml program, eg because of constructors for static objects...) (我不确定你可以毫无顾虑地从C或C ++代码调用Ocaml代码;你需要初始化Ocaml运行时系统;同样std C ++库可能无法从未修补的ocaml程序中运行,例如因为静态构造函数对象...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM