显式链接错误 - 语法错误:缺少 ';'由 bazel 编译时在 '*' 之前
Posted
技术标签:
【中文标题】显式链接错误 - 语法错误:缺少 \';\'由 bazel 编译时在 \'*\' 之前【英文标题】:Errors on explicit link - syntax error : missing ';' before '*' while compiling by bazel显式链接错误 - 语法错误:缺少 ';'由 bazel 编译时在 '*' 之前 【发布时间】:2018-04-06 04:13:35 【问题描述】:我试图建立一个明确的链接来导出一个类。代码中不应该有语法错误。我认为可能是 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() ;
非常感谢您给我的所有回复。希望我们可以轻松解决。
【问题讨论】:
您需要将您的ExpDetector
类声明移动到一个expdtctlib.h 文件并# 将其包含在expdetect.cpp 中。
@MFisherKDX 谢谢!我已经解决了这个问题!
【参考方案1】:
ExpDetector
类在 expdetect.cpp
中不可见。
您应该将类声明放在头文件中,将实现放在源文件中。然后在main.cpp
中包含头文件。
如果链接为 DLL,则无需在 main.cpp
中使用 #include "expdtctlib.h"
,但需要单独构建 DLL。 Here is an example.
标头包含声明
// 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 中包含标题
//expdetect.cpp
#include "expdtctlib.h" // If not DLL
int main()
....
【讨论】:
非常感谢!但是为什么显式链接需要包含头文件?我在这里阅读了一个显式链接示例Linking Explicitly。 要作为 dll 链接,您需要先单独构建 dll。在构建 dll 时,您仍然需要将代码拆分为头文件/源文件。 Here is a guid one creating a DLL.以上是关于显式链接错误 - 语法错误:缺少 ';'由 bazel 编译时在 '*' 之前的主要内容,如果未能解决你的问题,请参考以下文章
查询包含多个 JOIN 时访问 SQL 语法错误(缺少运算符)
vs2008编译错误:error C2146: 语法错误 : 缺少“;”(在标识符“name”的前面)