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

时间:2018-04-06 04:13:35

标签: c++ tensorflow dll bazel cl

我试图明确链接导出一个类。代码中不应该有语法错误。我想也许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() ;
}

非常感谢你收到的所有回复。希望我们能够轻松解决。

1 个答案:

答案 0 :(得分:0)

ExpDetector无法看到expdetect.cpp课程。

您应该将类​​声明放在头文件中,并将实现放在源文件中。然后在main.cpp中包含头文件。

如果作为DLL链接,则不需要在#include "expdtctlib.h"中使用main.cpp,但您需要单独构建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 ;
}
 ....

在主

中加入标题
//expdetect.cpp  
#include "expdtctlib.h" // If not DLL

int main() {
....
相关问题