我們知道,TensorFlow官網是提供了C語言的函數庫,Windows版本、Linux版本的動態庫都有提供,所有我們不必自己去編譯TensorFlow源碼來生成動態庫。
TensorFlow官網C語言庫
打開TensorFlow官網鏈接,頁面如下
在這個頁面下部有各種版本的下載鏈接
根據自己的需要,我下載Windows版本CPU版的dll庫。
下載完成後我們解壓這個壓縮包得到C語言庫的頭文件和dll文件,這個動態庫是沒有lib文件的,因此我們在C++調用dll庫是採用動態庫動態庫調用的方式。
這個TensorFlow c語言庫是64位動態庫,因此在vs2015中新建64位C++控制檯程序,代碼如下
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
using namespace std;
int main()
{
/*
動態調用 不需要lib文件
*/
// 運行時加載DLL庫
//TensorFlow庫爲64位dll庫
HMODULE module = LoadLibrary(L"tensorflow.dll");
if (module == NULL)
{
cout << "動態加載dll失敗!" << endl;
return 1;
}
typedef char*(*TF_VersionFunc)(); // 定義函數指針類型
TF_VersionFunc TF_Version;
// 導出函數地址
TF_Version = (TF_VersionFunc)GetProcAddress(module, "TF_Version");
if (TF_Version == NULL)
{
cout << "動態加載函數失敗!" << endl;
return 2;
}
cout << "調用的TensorFlow動態庫版本爲:"<<TF_Version() << endl;
getchar();
return 0;
}
tensorflow.dll這個文件需要放到x64/Release目錄下
程序運行結果如下:
可見,TensorFlow官網上提供的C語言動態庫在vs2015中調用成功。