在我的C程序中调用C ++函数

时间:2016-10-02 11:51:59

标签: c++ c extern

我的C ++函数example.cpp

#include <sapi.h>
#include <sphelper.h>
#include <atlcomcli.h>
#include <stdio.h>

int texttospeech()
{
    HRESULT                 hr = S_OK;
    CComPtr <ISpVoice>      cpVoice;
    CComPtr <ISpStream>     cpStream;
    CSpStreamFormat         cAudioFmt;

    if (FAILED(::CoInitialize(NULL)))
        return 0;
    //Create a SAPI Voice
    hr = cpVoice.CoCreateInstance( CLSID_SpVoice );
    printf("%d",hr);
    //Set the audio format
    if(SUCCEEDED(hr))
    {
        hr = cAudioFmt.AssignFormat(SPSF_22kHz16BitMono);
        printf("set audio format");
    }
    printf("%d",hr);

    //Call SPBindToFile, a SAPI helper method,  to bind the audio stream to the file
    if(SUCCEEDED(hr))
    {
        hr = SPBindToFile( L"d:\\test.mp3",  SPFM_CREATE_ALWAYS,
            &cpStream, & cAudioFmt.FormatId(),cAudioFmt.WaveFormatExPtr() );

        printf("set audio format");
    }
    printf("%d",hr);
    //set the output to cpStream so that the output audio data will be stored in cpStream
    if(SUCCEEDED(hr))
    {
        hr = cpVoice->SetOutput( cpStream, TRUE );
    }
    printf("%d",hr);    
    //Speak the text "hello world" synchronously
    if(SUCCEEDED(hr))
    {
        hr = cpVoice->Speak( L"Hello!! How are you?",  SPF_DEFAULT, NULL );
    }
    printf("%d",hr);
    //close the stream
    if(SUCCEEDED(hr))
    {
        hr = cpStream->Close();
    }
    printf("%d",hr);

    //Release the stream and voice object
    cpStream.Release ();
    cpVoice.Release();

    ::CoUninitialize();
    return 0;
}

我的C代码,如example1.c

#include <stdio.h>
#include "example.h"

int main(void) {
    texttospeech();
}

创建标题为example.h

#ifdef__cplusplus
    extern "C" {
#endif
int texttospeech();
#ifdef__cplusplus
    }
#endif

获得:

  

函数_main

中引用的未解析的外部符号_texttospeech

不确定如何在C中使用C ++调用函数,我是一个尝试集成C和C ++的初学者。我还有什么方法可以做到这一点?有没有办法创建共享库?

0 个答案:

没有答案