Errors on explicit link - syntax error : missing ';' before '*' while compiling by bazel
Errors on explicit link - syntax error : missing ';' before '*' while compiling by bazel
我试图明确 link 导出 class.There 不应该是 code.I 中的语法错误,认为 cl.exe 可能有问题。
我希望这些只是一些愚蠢的语法错误,而不是由 cl.exe 引起的。
我需要你的帮助。
错误如下:
tensorflow/detector0405/expdetect.cpp(25): error C2143: syntax error : missing ';' before '*'
tensorflow/detector0405/expdetect.cpp(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
tensorflow/detector0405/expdetect.cpp(25): error C2065: 'pDtctor' undeclared identifier
tensorflow/detector0405/expdetect.cpp(26): error C2065: 'pDtctor' undeclared identifier
tensorflow/detector0405/expdetect.cpp(26): error C2146: syntax error : missing ';' before identifier 'Get'
tensorflow/detector0405/expdetect.cpp(26): error C2065: 'Get' undeclared identifier
tensorflow/detector0405/expdetect.cpp(26): error C2146: syntax error : missing ';' before identifier 'GetProcAddress'
tensorflow/detector0405/expdetect.cpp(27): error C2065: 'Get' undeclared identifier
tensorflow/detector0405/expdetect.cpp(32): error C2065: 'Get' undeclared identifier
tensorflow/detector0405/expdetect.cpp(38): error C2227: left of '->createGraph' must point to class/struct/union/generic type
tensorflow/detector0405/expdetect.cpp(38): note: type is ‘ExpDetector *’
tensorflow/detector0405/expdetect.cpp(39): error C2227: left of '->setInput' must point to class/struct/union/generic type
tensorflow/detector0405/expdetect.cpp(39): note: type is ‘ExpDetector *’
tensorflow/detector0405/expdetect.cpp(40): error C2227: left of '->detection' must point to class/struct/union/generic type
tensorflow/detector0405/expdetect.cpp(40): note: type is ‘ExpDetector *’
tensorflow/detector0405/expdetect.cpp(43): error C2227: left of '->getOutput' must point to class/struct/union/generic type
tensorflow/detector0405/expdetect.cpp(43): note: type is ‘ExpDetector *’
Target //tensorflow/detector0405:expdetect failed to build
INFO: Elapsed time: 9.515s, Critical Path: 3.54s
FAILED: Build did NOT complete successfully
这是主要功能:
expdetect.cpp
//expdetect.cpp
#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
HINSTANCE dtctDll ;
dtctDll = LoadLibrary(TEXT("expdtctlib.dll")) ;
if(dtctDll == NULL)
{
FreeLibrary(dtctDll) ;
std::cout << "Load Dll Failed!" << std::endl ;
return 0 ;
}
typedef ExpDetector* (*pDtctor)() ; //(25)Here the first error occured.
pDtctor Get = (pDtctor)GetProcAddress(dtctDll, TEXT("createDtctor")) ; //(26)
if(Get == NULL)//(27)
{
std::cout <<"Load Address Failed!"<< std::endl ;
return 0;
}
ExpDetector *detector = (Get)() ; //(32)
char* model_path = "models/graph.pb" ;
double a = 100.0;
double b = 23.33;
detector->createGraph(model_path) ; //(38)
detector->setInput(a, b) ;//(39)
detector->detection() ;//(40)
float output_ ;
output_ = detector->getOutput() ;//(43)
std::cout << output_ << "\n" ;
delete detector ;
FreeLibrary(dtctDll) ;
return 0 ;
}
这是 DLL 定义:
expdtctlib.cpp
//expdtctlib.cpp
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/platform/env.h"
#include <iostream>
using namespace tensorflow;
class ExpDetector
{
private:
Session* session ;
Status status ;
std::vector<std::pair<string, tensorflow::Tensor>> inputs ;
std::vector<tensorflow::Tensor> outputs;
public:
ExpDetector() ;
virtual int createGraph(char* graph_path) ;
~ExpDetector() ;
virtual void setInput(double a0,double b0) ;
virtual int ExpDetector::detection() ;
virtual double ExpDetector::getOutput() ;
};
ExpDetector::ExpDetector()
{
std::cout << "Detector was created." << std::endl ;
}
int ExpDetector::createGraph(char* graph_path)
{
std::cout << "Detector Initiating..." << std::endl ;
status = NewSession(SessionOptions(), &session) ;
if (!status.ok()) {
std::cout << status.ToString() << "\n";
return 1;
}
GraphDef graph_def;
status = ReadBinaryProto(Env::Default(), graph_path, &graph_def);
if (!status.ok()) {
std::cout << status.ToString() << "\n";
return 1;
}
// Add the graph to the session
status = session->Create(graph_def);
if (!status.ok()) {
std::cout << status.ToString() << "\n";
return 1;
}
std::cout << "Detector Initiated." << std::endl ;
}
ExpDetector::~ExpDetector()
{
session->Close();
std::cout << "Detector Destroied !" << std::endl ;
}
void ExpDetector::setInput(double a0,double b0)
{
std::cout << "Feeding Data to Detector..." << std::endl ;
Tensor a(DT_FLOAT, TensorShape());
a.scalar<float>()() = a0;
Tensor b(DT_FLOAT, TensorShape());
b.scalar<float>()() = b0;
inputs = {
{ "a", a },
{ "b", b },
};
std::cout << "Done Feeding Data." << std::endl ;
}
int ExpDetector::detection()
{
status = session->Run(inputs, {"c"}, {}, &outputs);
if (!status.ok()) {
std::cout << status.ToString() << "\n";
return 1;
}
}
double ExpDetector::getOutput()
{
auto output_c = outputs[0].scalar<float>();
std::cout << outputs[0].DebugString() << "\n";
std::cout << output_c() << "\n";
return output_c() ;
}
extern "C" __declspec(dllexport) ExpDetector* createDtctor(void)
{
return new ExpDetector() ;
}
非常感谢大家的回复我会get.Hope我们可以轻松解决。
ExpDetector
class 在 expdetect.cpp
中不可见。
您应该将 class 声明放在 header 文件中,并将实现放在源文件中。然后在 main.cpp
.
中包含 header 文件
如果链接为 DLL,则无需在 main.cpp
中使用 #include "expdtctlib.h"
,但需要单独构建 DLL。 Here is an example.
Header 包含声明
// expdtctlib.h
#ifndef expdtctlib_h_
#define expdtctlib_h_
namespace tensorflow {
class ExpDetector {
Session* session;
...
public:
ExpDetector();
...
};
}
#endif
来源包含定义
// expdtctlib.cpp
#include "expdtctlib.h"
ExpDetector::ExpDetector()
{
std::cout << "Detector was created." << std::endl ;
}
....
在 main
中包含 header
//expdetect.cpp
#include "expdtctlib.h" // If not DLL
int main() {
....
我试图明确 link 导出 class.There 不应该是 code.I 中的语法错误,认为 cl.exe 可能有问题。 我希望这些只是一些愚蠢的语法错误,而不是由 cl.exe 引起的。 我需要你的帮助。 错误如下:
tensorflow/detector0405/expdetect.cpp(25): error C2143: syntax error : missing ';' before '*'
tensorflow/detector0405/expdetect.cpp(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
tensorflow/detector0405/expdetect.cpp(25): error C2065: 'pDtctor' undeclared identifier
tensorflow/detector0405/expdetect.cpp(26): error C2065: 'pDtctor' undeclared identifier
tensorflow/detector0405/expdetect.cpp(26): error C2146: syntax error : missing ';' before identifier 'Get'
tensorflow/detector0405/expdetect.cpp(26): error C2065: 'Get' undeclared identifier
tensorflow/detector0405/expdetect.cpp(26): error C2146: syntax error : missing ';' before identifier 'GetProcAddress'
tensorflow/detector0405/expdetect.cpp(27): error C2065: 'Get' undeclared identifier
tensorflow/detector0405/expdetect.cpp(32): error C2065: 'Get' undeclared identifier
tensorflow/detector0405/expdetect.cpp(38): error C2227: left of '->createGraph' must point to class/struct/union/generic type
tensorflow/detector0405/expdetect.cpp(38): note: type is ‘ExpDetector *’
tensorflow/detector0405/expdetect.cpp(39): error C2227: left of '->setInput' must point to class/struct/union/generic type
tensorflow/detector0405/expdetect.cpp(39): note: type is ‘ExpDetector *’
tensorflow/detector0405/expdetect.cpp(40): error C2227: left of '->detection' must point to class/struct/union/generic type
tensorflow/detector0405/expdetect.cpp(40): note: type is ‘ExpDetector *’
tensorflow/detector0405/expdetect.cpp(43): error C2227: left of '->getOutput' must point to class/struct/union/generic type
tensorflow/detector0405/expdetect.cpp(43): note: type is ‘ExpDetector *’
Target //tensorflow/detector0405:expdetect failed to build
INFO: Elapsed time: 9.515s, Critical Path: 3.54s
FAILED: Build did NOT complete successfully
这是主要功能:
expdetect.cpp
//expdetect.cpp
#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
HINSTANCE dtctDll ;
dtctDll = LoadLibrary(TEXT("expdtctlib.dll")) ;
if(dtctDll == NULL)
{
FreeLibrary(dtctDll) ;
std::cout << "Load Dll Failed!" << std::endl ;
return 0 ;
}
typedef ExpDetector* (*pDtctor)() ; //(25)Here the first error occured.
pDtctor Get = (pDtctor)GetProcAddress(dtctDll, TEXT("createDtctor")) ; //(26)
if(Get == NULL)//(27)
{
std::cout <<"Load Address Failed!"<< std::endl ;
return 0;
}
ExpDetector *detector = (Get)() ; //(32)
char* model_path = "models/graph.pb" ;
double a = 100.0;
double b = 23.33;
detector->createGraph(model_path) ; //(38)
detector->setInput(a, b) ;//(39)
detector->detection() ;//(40)
float output_ ;
output_ = detector->getOutput() ;//(43)
std::cout << output_ << "\n" ;
delete detector ;
FreeLibrary(dtctDll) ;
return 0 ;
}
这是 DLL 定义:
expdtctlib.cpp
//expdtctlib.cpp
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/platform/env.h"
#include <iostream>
using namespace tensorflow;
class ExpDetector
{
private:
Session* session ;
Status status ;
std::vector<std::pair<string, tensorflow::Tensor>> inputs ;
std::vector<tensorflow::Tensor> outputs;
public:
ExpDetector() ;
virtual int createGraph(char* graph_path) ;
~ExpDetector() ;
virtual void setInput(double a0,double b0) ;
virtual int ExpDetector::detection() ;
virtual double ExpDetector::getOutput() ;
};
ExpDetector::ExpDetector()
{
std::cout << "Detector was created." << std::endl ;
}
int ExpDetector::createGraph(char* graph_path)
{
std::cout << "Detector Initiating..." << std::endl ;
status = NewSession(SessionOptions(), &session) ;
if (!status.ok()) {
std::cout << status.ToString() << "\n";
return 1;
}
GraphDef graph_def;
status = ReadBinaryProto(Env::Default(), graph_path, &graph_def);
if (!status.ok()) {
std::cout << status.ToString() << "\n";
return 1;
}
// Add the graph to the session
status = session->Create(graph_def);
if (!status.ok()) {
std::cout << status.ToString() << "\n";
return 1;
}
std::cout << "Detector Initiated." << std::endl ;
}
ExpDetector::~ExpDetector()
{
session->Close();
std::cout << "Detector Destroied !" << std::endl ;
}
void ExpDetector::setInput(double a0,double b0)
{
std::cout << "Feeding Data to Detector..." << std::endl ;
Tensor a(DT_FLOAT, TensorShape());
a.scalar<float>()() = a0;
Tensor b(DT_FLOAT, TensorShape());
b.scalar<float>()() = b0;
inputs = {
{ "a", a },
{ "b", b },
};
std::cout << "Done Feeding Data." << std::endl ;
}
int ExpDetector::detection()
{
status = session->Run(inputs, {"c"}, {}, &outputs);
if (!status.ok()) {
std::cout << status.ToString() << "\n";
return 1;
}
}
double ExpDetector::getOutput()
{
auto output_c = outputs[0].scalar<float>();
std::cout << outputs[0].DebugString() << "\n";
std::cout << output_c() << "\n";
return output_c() ;
}
extern "C" __declspec(dllexport) ExpDetector* createDtctor(void)
{
return new ExpDetector() ;
}
非常感谢大家的回复我会get.Hope我们可以轻松解决。
ExpDetector
class 在 expdetect.cpp
中不可见。
您应该将 class 声明放在 header 文件中,并将实现放在源文件中。然后在 main.cpp
.
如果链接为 DLL,则无需在 main.cpp
中使用 #include "expdtctlib.h"
,但需要单独构建 DLL。 Here is an example.
Header 包含声明
// expdtctlib.h
#ifndef expdtctlib_h_
#define expdtctlib_h_
namespace tensorflow {
class ExpDetector {
Session* session;
...
public:
ExpDetector();
...
};
}
#endif
来源包含定义
// expdtctlib.cpp
#include "expdtctlib.h"
ExpDetector::ExpDetector()
{
std::cout << "Detector was created." << std::endl ;
}
....
在 main
中包含 header//expdetect.cpp
#include "expdtctlib.h" // If not DLL
int main() {
....