2016-01-25 22 views
1

我試圖創建一個帶液體流量和介質的OpenModelica模型。 我在Matlab中有一個函數來計算pdetool中的PDE(偏微分方程)。 我想通過MCR在Matlab中創建一個共享動態庫(.so文件)並將其加載到模型中。 我的平臺:Linux或Mac OS上使用的OpenModelica。已安裝Linux和Mac OS上的MCR。如何加載在Matlab中創建的共享庫

在Matlab中,我只能生成DLL(可能會生成libmyfunc.so?)。

當我嘗試編譯外部C函數模型我得到了一個錯誤:

#omc +s test_matlab_so.mo func_mathlab.mo 
#make -f test_matlab_so.makefile 
/usr/bin/clang -Wimplicit-function-declaration -O0 -falign-functions -march=native  -I"/opt/openmodelica/include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -c -o test_matlab_so_functions.o test_matlab_so_functions.c 
clang: warning: optimization flag '-falign-functions' is not supported 
clang: warning: argument unused during compilation: '-falign-functions' 
In file included from test_matlab_so_functions.c:7: 
In file included from ./test_matlab_so_includes.h:4: 
./shared_train/src/lib_summ.c:90:8: warning: implicit declaration of function 'GetModuleFileName' is invalid in C99 [-Wimplicit-function-declaration] 
    if (!GetModuleFileName(GetModuleHandle("lib_summ"), path_to_dll, _MAX_PATH)) 
    ^
./shared_train/src/lib_summ.c:90:26: warning: implicit declaration of function 'GetModuleHandle' is invalid in C99 [-Wimplicit-function-declaration] 
    if (!GetModuleFileName(GetModuleHandle("lib_summ"), path_to_dll, _MAX_PATH)) 
         ^
./shared_train/src/lib_summ.c:90:55: error: use of undeclared identifier 'path_to_dll' 
    if (!GetModuleFileName(GetModuleHandle("lib_summ"), path_to_dll, _MAX_PATH)) 
                ^
./shared_train/src/lib_summ.c:90:68: error: use of undeclared identifier '_MAX_PATH' 
    if (!GetModuleFileName(GetModuleHandle("lib_summ"), path_to_dll, _MAX_PATH)) 
                   ^
./shared_train/src/lib_summ.c:94:37: error: use of undeclared identifier 'path_to_dll' 
      mclGetEmbeddedCtfStream(path_to_dll); 
            ^
test_matlab_so_functions.c:19:16: warning: implicit declaration of function '_mlfSumm' is invalid in C99 [-Wimplicit-function-declaration] 
    _v_out_ext = _mlfSumm(_v_a_ext, _v_b_ext); 
      ^
3 warnings and 3 errors generated. 
make: *** [test_matlab_so_functions.o] Error 1 

有人可以幫我整合OpenModelica和Matlab?

回答

1

我對Linux沒有多少經驗,但是在使用MCR的Windows中做了同樣的事情。看看這個: http://de.mathworks.com/matlabcentral/answers/94471-how-do-i-create-a-c-c-shared-library-with-matlab-compiler-that-can-be-used-in-a-microsoft-visual-c

當你這樣做,你會得到一個DLL以及一個靜態庫。您需要將它們都複製到您的modelica代碼所在的文件夾中,然後將該庫設置爲調用外部函數的modelica函數的「庫」屬性中。

請記住,如果您使用64位或32位Dymola,則應該相應地使用相同版本的matlab創建DLL。

+0

謝謝你的回答。根據Matlab文檔和關於使用extern C函數的OpenModelica文檔,我需要做1)在Matlab中創建共享庫。 2)編寫簡單的c包裝器,以初始化MCR例程並調用我的共享庫。 3)從OpenModelica調用我的C包裝器。這是對的嗎? – vserge

+0

是的,確切地說。我創建了兩個共享庫,一個來自我的matlab文件,另一個來自我在Dymola中使用的c wrapper。 – Shaga

+0

我謝謝你!我們這樣做了,但得到了下一個問題:在Linux上加載dll;) – vserge