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的路径添加对。
具体参考上面的那个实例,然后注意把路径换成自己机器上的。

混合编程:error LNK2001: unresolved external symbol的更多相关文章

  1. [异常] VC6.0 error LNK2001: unresolved external symbol _main解决办法

    来自:http://www.douban.com/note/65638800/ 学习VC++时经常会遇到链接错误LNK2001,该错误非常讨厌,因为对于编程者来说,最好改的错误莫过于编译错误,而一般说 ...

  2. VC6.0 error LNK2001: unresolved external symbol _main解决办法

    学习VC++时经常会遇到链接错误LNK2001,该错误非常讨厌,因为对于编程者来说,最好改的错误莫过于编译错误,而一般说来发生连接错误时,编译都已通过.产生连接错误的原因非常多,尤其LNK2001错误 ...

  3. (转载)浅析error LNK2001: unresolved external symbol "public: __thisc...

    学习VC++时经常会遇到链接错误LNK2001,该错误非常讨厌,因为对于      编程者来说,最好改的错误莫过于编译错误,而一般说来发生连接错误时,      编译都已通过.产生连接错误的原因非常多 ...

  4. VC++ : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall std::basic_string<wchar_t,struct std::char_traits<wchar_t>

    最近学习Google Breakpad,将其用在了自己的项目中,编译的版本为VS2010,没有什么问题.但是为了和之前的程序兼容,需要使用VS2008版本的程序,于是又编译了VS2008版本的代码,但 ...

  5. MASMPlus编译出错:error LNK2001: unresolved external symbol _WinMainCRTStartup

    初学汇编,感觉很多不懂.不过那也是,如果懂了的话就不用学了,从无到有学习一门编程语言果然不是那么容易的一件事. 学习汇编总得要有一款汇编软件才行,没理由只是使用Windows自带的DEBUG.于是上了 ...

  6. error LNK2001: unresolved external symbol _main

    想运行一个网上下载的opengl离屏渲染(渲染到纹理FBO)的程序,然后一直报错 error LNK2001: unresolved external symbol _main 解决了一下午终于弄明白 ...

  7. VC6.0 error LNK2001: unresolved external symbol __imp__ntohl@4

    --------------------Configuration: oxToint1 - Win32 Debug-------------------- Linking... main.obj : ...

  8. error LNK2001: unresolved external symbol @__security_check_cookie

    Q:VS2005编译的静态库, 在vc++6.0中连接出现错误 error LNK2001: unresolved external symbol @__security_check_cookie@l ...

  9. error LNK2001: unresolved external symbol "int g_cTemplates" (?g_cTemplates@@3HA)(转)

    原文转自:http://blog.sina.com.cn/s/blog_639a2ad70101kpen.html 编译directshow若干问题的解决 1.安装好windows sdk,进入dir ...

随机推荐

  1. Erlang中atom的实现

    Erlang的原子(atom)在匹配中有着重要作用,它兼顾了可读性和运行效率. 通过atom,可以实现很多灵活高效的应用. atom可以看作是给字符串生成了一个ID,内部使用的是ID值,必要时可以取出 ...

  2. HDUOJ----A Computer Graphics Problem

    A Computer Graphics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  3. WordPress网站搬家的问题

    老邢的博客搬家全过程(wordpress搬家知识总结)   网站搬家过程中的几个问题   WordPress网站搬家的方法   WORDPRESS.ORG - zh-cn:WordPress 博客搬家 ...

  4. 【js】js中的||和&&

    逻辑与&&和逻辑或||操作符可以应用于任何类型的操作数,而不仅仅是布尔值. 几乎所有语言中||和&&都遵循“短路”原理, 如&&中第一个表达式为假就不会 ...

  5. bash 基本功能

    1 shell概述 shell是一个命令解释器,为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序.用户可以用shell启动.挂起.停止甚至是编写一些程序. shell是一个功能强大 ...

  6. Android多点触摸放大缩小图片

    1.Activity package com.fit.touchimage; import android.app.Activity; import android.graphics.Bitmap; ...

  7. Linux内存使用方法详细解析

    我是一名程序员,那么我在这里以一个程序员的角度来讲解Linux内存的使用. 一提到内存管理,我们头脑中闪出的两个概念,就是虚拟内存,与物理内存.这两个概念主要来自于linux内核的支持. Linux在 ...

  8. Python isnumeric() 方法

    描述 Python isnumeric() 方法检测字符串是否只由数字组成.这种方法是只针对unicode对象. 注:定义一个字符串为Unicode,只需要在字符串前添加 'u' 前缀即可,具体可以查 ...

  9. ORM是什么?

    对象关系映射(Object Relational Mapping,简称ORM)是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用描述对象和数据库之间映射的元数据 ...

  10. unity save layout

    调整完布局,点Save Layout,并命名,就得到一个新的layout,以后可以直接在下拉菜单中选择并切换到此布局.