简体   繁体   English

显式链接上的错误-语法错误:缺少';' 由bazel编译时在“ *”之前

[英]Errors on explicit link - syntax error : missing ';' before '*' while compiling by bazel

I am tried to make explicit link to export a class.There should't be syntax errors in the code.I think maybe there is something wrong with cl.exe. 我试图做一个明确的链接来导出一个类。代码中应该没有语法错误。我认为cl.exe可能有问题。 I hope these are just some stupid syntax errors instead of causing by cl.exe. 我希望这些只是一些愚蠢的语法错误,而不是由cl.exe引起的。 I need your help please. 我需要你的帮助。 The errors are as follows: 错误如下:

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

This is the main function: 这是主要功能:

expdetect.cpp 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 ;
}

This is the DLL definition: 这是DLL的定义:

expdtctlib.cpp 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() ;
}

Thank you so much for all the replies I'll get.Hope we can solve it easily. 非常感谢您收到的所有答复。希望我们能够轻松解决。

The ExpDetector class is not visible from expdetect.cpp . ExpDetector类从expdetect.cpp不可见。

You should put your class declarations in a header file and the implementation in a source file. 您应该将类​​声明放在头文件中,并将实现放在源文件中。 Then include the header file in main.cpp . 然后在main.cpp包含头文件。

If linking as a DLL you will not need to use #include "expdtctlib.h" in main.cpp but you will need to build the DLL separately. 如果作为DLL链接,则不需要在main.cpp使用#include "expdtctlib.h" ,但是您将需要分别构建DLL。 Here is an example . 这是一个例子

Header contains declarations 标头包含声明

// expdtctlib.h
#ifndef expdtctlib_h_
#define expdtctlib_h_

namespace tensorflow {
    class ExpDetector {
        Session* session;
        ...

    public:
        ExpDetector();
        ...
    };
}

#endif

Source contains definitions 来源包含定义

// expdtctlib.cpp
#include "expdtctlib.h"

ExpDetector::ExpDetector()
{
    std::cout << "Detector was created." << std::endl ;
}
 ....

Include header in main 在主头中包含标题

//expdetect.cpp  
#include "expdtctlib.h" // If not DLL

int main() {
....

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

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