混合編程:error LNK2001: unresolved external symbol


Vs2006+matlab2010rb環境:

1:工具-選項-項目解決方案-VC++目錄設置include和lib的路徑

2:項目-屬性-屬性配置-鏈接器-輸入-附加依賴項把庫的名字添加進去

 

VISTA+MATLAB2009a+VS2010

以安裝路徑“E:\Program Files\MATLAB\R2009a\”為例

MATLAB外部支持文件夾:
E:\Program Files\MATLAB\R2009a\extern
matlab自帶的c例程:
E:\Program Files\MATLAB\R2009a\extern\examples\eng_mat

engine.h的位置:
E:\Program Files\MATLAB\R2009a\extern\include

各種lib的位置:
E:\Program Files\MATLAB\R2009a\extern\lib\win32\microsoft

在matlab幫助中輸入“C language”即可找到有關MATLAB Engine的一個頁面。
從這個頁面開始,學習各種關鍵詞,
就能夠找到一切你需要的資料。

使用MATLAB Engine一般用兩套函數就可以了。
1.engXXXX,關於Engine本身的操作,包括打開/關閉,設置/取得變量,執行語句等等。
2.mxXXXX,關於數據類型mxArray的操作,與MATLAB交互的左右類型全部為mxArray。

》》一個搭建實例

先在VS2010的 項目->屬性-> VC++目錄->包含目錄 下加上:

include files:
E:\Program Files\MATLAB\R2009a\extern\include

項目->屬性-> VC++目錄->庫目錄 下加上

library files:
E:\Program Files\MATLAB\R2009a\extern\lib\win32\microsoft

做好這些后,如果我們環境一樣,
下面的代碼應該能夠編通並且正常執行,
其中包含了常用的一些函數,
一般來說使用Engine的時候也就用這些了。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "engine.h"
#include "matrix.h"

#pragma comment(lib,"libeng.lib") 
#pragma comment(lib,"libmx.lib")

int main()
{
    Engine *ep;
    int i , j ;

    //show how to open MATLAB engine
    //for remote ones:
    //engOpen( ADDRESS OF REMOTE SYSTEM ) ;

    if (!(ep = engOpen("\0"))){
        fprintf(stderr, "\nCan't start MATLAB engine\n");
        return EXIT_FAILURE;
    }

    //show how to create matrix
    mxArray *Y = mxCreateDoubleMatrix(1 , 3 , mxREAL) ;
    
    //show how to put data in matrix
    double tmp[3] = {1.0 , 2.0 , 3.0} ;
    memcpy(mxGetPr(Y) , tmp , sizeof(tmp)) ;

    //show how to put variables in the Engine
    engPutVariable(ep , "Y" , Y) ;

    //show how to execute commands in MATLAB
    engEvalString(ep, "X = ones(5,1) * Y");
    
    //show how to get variables from the Engine
    mxArray *X = engGetVariable(ep , "X") ;
    
    //show how to manipulate dimensions
    int dims[10] ;
    int ndims ;
    ndims = mxGetNumberOfDimensions(X) ;
    printf("total number of dimensions is %d\n" , ndims) ;
    memcpy(dims , mxGetDimensions(X) , ndims * sizeof(int)) ;
    for ( i = 0 ; i < ndims ; i ++ ){
        printf("dimension %d : %d\n" , i , dims[i]) ;
    }
    printf("\n") ;

    //show how the data is stored in the memory
    double *p = (double*)mxGetData(X) ;    
    for ( i = 0 ; i < dims[0] ; i ++ ){
        for ( j = 0 ; j < dims[1] ; j ++ ){
            printf("%8.2f" , p[j * dims[0] + i]) ;
        }
        printf("\n") ;
    }

    //---important, to release resources
    mxDestroyArray(X) ;
    mxDestroyArray(Y) ;

    //show how to hide and unhide MATLAB command window
    printf("type RETURN to hide the MATLAB command window...\n") ;
    getchar() ;
    engSetVisible(ep , false) ;
    printf("type RETURN to unhide the MATLAB command window...\n") ;
    getchar() ;
    engSetVisible(ep , true) ;

    printf("type RETURN to END this program...\n") ;
    getchar() ;    
    //remembering to close it is important .
    //but if you are debugging your programs , 
    //annotate the following line will save you a lot of time ,
    //for you needn't to restart the Engine .
    engClose(ep) ;
    
    //when your work is accomplished , type "exit" in MATLAB command window

    return EXIT_SUCCESS;
}

 


》》某些問題

如果出現這個:

engdemo.obj : error LNK2001: unresolved external symbol _engClose
engdemo.obj : error LNK2001: unresolved external symbol _engSetVisible
engdemo.obj : error LNK2001: unresolved external symbol _mxDestroyArray
engdemo.obj : error LNK2001: unresolved external symbol _mxGetData
engdemo.obj : error LNK2001: unresolved external symbol _mxGetDimensions_730
engdemo.obj : error LNK2001: unresolved external symbol _mxGetNumberOfDimensions_730
engdemo.obj : error LNK2001: unresolved external symbol _engGetVariable
engdemo.obj : error LNK2001: unresolved external symbol _engEvalString
engdemo.obj : error LNK2001: unresolved external symbol _engPutVariable
engdemo.obj : error LNK2001: unresolved external symbol _mxGetPr
engdemo.obj : error LNK2001: unresolved external symbol _mxCreateDoubleMatrix_730
engdemo.obj : error LNK2001: unresolved external symbol _engOpen

其實就是lib沒有添加好。

在代碼中寫上:
#pragma comment(lib,"libeng.lib") 
#pragma comment(lib,"libmx.lib")
就可以了。

PS: #pragma comment( comment-type ,["commentstring"] )

comment-type是一個預定義的標識符,指定注釋的類型,應該是compiler,exestr,lib,linker之一。

commentstring是一個提供為comment-type提供附加信息的字符串。

 

我們經常用到的是#pragma comment(lib,"*.lib")這類的。#pragma comment(lib,"Ws2_32.lib")表示鏈接Ws2_32.lib這個庫。 和在工程設置里寫上鏈入Ws2_32.lib的效果一樣,不過這種方法寫的 程序別人在使用你的代碼的時候就不用再設置工程settings了

或者可以在工程的連接設置里面添加這兩個庫。
不過我傾向於前者,這樣在發布源碼的同時,
就盡最大可能地保證能夠編譯,
而不用其他人學習的時候再去設置。

當然,由於#pragma是由編譯器自己決定的,
所以代碼的可移植性存在一些問題。

如果還是報上面的錯誤,估計是沒有將lib的路徑添加對。
具體參考上面的那個實例,然后注意把路徑換成自己機器上的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM