简体   繁体   English

在C ++中使用Octave库(DLL等),而无需安装Octave(只需复制安装文件夹)

[英]Using Octave Libraries (DLLs and so on) in C++ without installing Octave (just copying the installation folder)

In my C++ solution I have a DLL. 在我的C ++解决方案中,我有一个DLL。 My DLL uses Octave C++ classes and also uses Octave as "embedded" engine. 我的DLL使用Octave C ++类,还使用Octave作为“嵌入式”引擎。 By using the following steps (1st method) I can build my C++ solution and run the built executable and get the output results in some text files without any problem: 通过使用以下步骤(第一种方法),我可以构建我的C ++解决方案并运行所构建的可执行文件,并在一些文本文件中获取输出结果而没有任何问题:

The steps of using Octave Libraries in Visual Studio C++ 2008:

1- Download Octave 3.6.1 for Visual Studio 2008 by the following link. 
Notice that there two options for download, one for VS2010 and one for VS2008, you should download the VS2008 option.

http://sourceforge.net/projects/octave/files/Octave%20Windows%20binaries/Octave%203.6.1%20for%20Windows%20Microsoft%20Visual%20Studio/

2- Install the Octave 3.6.1 on your system in the following directory (which contains NO white spaces):

C:\Software\Octave-3.6.1

3- While installing, on the installation wizard, select all the available packages of the Octave 3.6.1 to be installed, also select "gnuplot.exe" as graphics engine.

4- After installing Octave 3.6.1, include the Octave 3.6.1 header files in your C++ code like below: 

#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/config.h>

5- Configure your VS2008:
Add the following directories to "Property Pages > Configuration Properties > C/C++ > General >Additional Include Directories:"

C:\Software\Octave-3.6.1\include
C:\Software\Octave-3.6.1\include\octave-3.6.1

6- Configure your VS2008:
Add the following directories to "Property Pages > Configuration Properties > Linker > General > Additional Library Directories:"

C:\Software\Octave-3.6.1\lib
C:\Software\Octave-3.6.1\lib\octave\3.6.1

7- Configure your VS2008:
Change the "Property pages > Configuration Properties > Linker > General > Enable Incremental Linking" to "No."

8- Configure your VS2008:
Add the following files to "Property Pages > Configuration Properties > Linker > Input > Additional Dependencies:"

octave.lib
octinterp.lib
cruft.lib

9- Configure your VS2008:
Enter the following path to "Property Pages > Configuration Properties > Debugging > 

Environment:"

    PATH=%PATH%;C:\Software\Octave-3.6.1\bin;

10- Use the following command before loading my DLL: 

SetDllDirectory((LPCWSTR)L"C:\\Software\\Octave-3.6.1\\bin\\");

But when I try to use the following steps (2nd method), I have a problem (I'll explain the problem): 但是,当我尝试使用以下步骤(第二种方法)时,我遇到了问题(我将解释问题):

The steps of using Octave Libraries in Visual Studio C++ 2008:

1- Download Octave 3.6.1 for Visual Studio 2008 by the following link. 
Notice that there two options for download, one for VS2010 and one for VS2008, you should download the VS2008 option.

http://sourceforge.net/projects/octave/files/Octave%20Windows%20binaries/Octave%203.6.1%20for%20Windows%20Microsoft%20Visual%20Studio/

2- Install the Octave 3.6.1 on your system in the following directory (which contains NO white spaces):

C:\Software\Octave-3.6.1

3- While installing, on the installation wizard, select all the available packages of the Octave 3.6.1 to be installed, also select "gnuplot.exe" as graphics engine.

4- After installing Octave 3.6.1, copy the folder "Octave-3.6.1" at the address "C:\Software\Octave-3.6.1" next to the solution file of visual studio 2008 project (*.sln) and then include the Octave 3.6.1 header files in your C++ code like below: 

#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/config.h>

5- Configure your VS2008:
Add the following directories to "Property Pages > Configuration Properties > C/C++ > General >Additional Include Directories:"

..\Octave-3.6.1\include
..\Octave-3.6.1\include\octave-3.6.1

6- Configure your VS2008:
Add the following directories to "Property Pages > Configuration Properties > Linker > General > Additional Library Directories:"

..\Octave-3.6.1\lib
..\Octave-3.6.1\lib\octave\3.6.1

7- Configure your VS2008:
Change the "Property pages > Configuration Properties > Linker > General > Enable Incremental Linking" to "No."

8- Configure your VS2008:
Add the following files to "Property Pages > Configuration Properties > Linker > Input > Additional Dependencies:"

octave.lib
octinterp.lib
cruft.lib

9- Configure your VS2008:
Enter the following path to "Property Pages > Configuration Properties > Debugging > Environment:"

PATH=%PATH%;..\Octave-3.6.1\bin;

10- Use the following command before loading my DLL:
    SetDllDirectory((LPCWSTR)L"..\\Octave-3.6.1\\bin\\");

The problem is that when I use the 2nd method, it works perfect as far as I keep the installation folder of C:\\Software\\Octave-3.6.1 untouched. 问题是,当我使用第二种方法时,只要保持C:\\Software\\Octave-3.6.1的安装文件夹不变,它就可以完美地工作。 I mean when I rename the installation folder or when I remove it, or when I uninstall Octave, my solution compiles and builds the executable, but when running the executable, it would not generate any results (the results are a bunch of text files and the output files would be empty). 我的意思是,当我重命名安装文件夹或删除它时,或者当我卸载Octave时,我的解决方案会编译并生成可执行文件,但是在运行可执行文件时,它不会生成任何结果(结果是一堆文本文件,输出文件将为空)。 I was wondering if anybody knows the reason and a solution. 我想知道是否有人知道原因和解决方案。 Thanks. 谢谢。

@Andy As Andy requested, I showed below the section of my C++ code which uses Octave as embedded engine. @Andy按照Andy的要求,我在使用八度作为嵌入式引擎的C ++代码部分中进行了显示。 Variables with m_ prefix are members of the FDD class for which the getCPSD method is implemented below. 具有m_前缀的变量是FDD类的成员,下面将getCPSD实现getCPSD方法。

void FDD::getCPSD(){
    int argc=int(2);
    string_vector argv=string_vector(argc);
    argv(0)="embedded";
    argv(1)="-q";
    octave_main(argc,argv.c_str_vec(),1);//start Octave interpreter as embedded engine, because cpsd command is an "m file" not C++ class

    octave_value_list OVL_in_pkg;
    OVL_in_pkg(0)="load";
    OVL_in_pkg(1)="signal";
    feval("pkg",OVL_in_pkg,0);//load signal package of octave (cpsd command is inside signal package)

    octave_value_list OVL_in_hanning;
    octave_value_list OVL_out_hanning;
    OVL_in_hanning(0)=m_int_SamplingFreq * NFFT_FS_RATIO;//NFFT
    OVL_out_hanning=feval("hanning",OVL_in_hanning,1);
    octave_value_list OVL_in_cpsd;
    octave_value_list OVL_out_cpsd;
    OVL_in_cpsd(2)=OVL_out_hanning(0);//WINDOW (get from hanning)
    OVL_in_cpsd(3)=CPSD_OVERLAP;//OVERLAP
    OVL_in_cpsd(4)=m_int_SamplingFreq * NFFT_FS_RATIO;//NFFT
    OVL_in_cpsd(5)=m_int_SamplingFreq;//FS
    for(int i=int(0);i<m_int_NumChann;i++){
        OVL_in_cpsd(0)=m_MatrixOfChannels.column(i);
        for(int j=int(0);j<m_int_NumChann;j++){
            OVL_in_cpsd(1)=m_MatrixOfChannels.column(j);
            OVL_out_cpsd=feval("cpsd",OVL_in_cpsd,2);
            m_OVL_AmplitudesAndPhaseFromCPSD(i*m_int_NumChann+j)    =OVL_out_cpsd(0).complex_column_vector_value();
            m_OVL_FreqFromCPSD(i*m_int_NumChann+j)                  =OVL_out_cpsd(1).column_vector_value();
        }
    }
    m_ColumnVector_F = m_OVL_FreqFromCPSD(0).column_vector_value();
    m_int_NumOfFreqIndexPoints = m_OVL_FreqFromCPSD(0).column_vector_value().length();
    //clean_up_and_exit(0);//for closing the embedded Octave engine, but instead, it closes the whole program! Therefore, I skipped this line!
}

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

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