入口点无法位于动态链接库中 - c ++

时间:2018-04-13 08:13:41

标签: c++ visual-c++ dll entry-point cpprest-sdk

我在visual c ++中创建了一个DLL项目,我想使用cpprestsdk/casablanca

然后我创建了一个RestWrapper.h头文件:

#pragma once

namespace mycpprest
{
    class RestWrapper
    {
    public:
        static __declspec(dllexport) void TestApi();
    };
}

RestWrapper.cpp源文件:

#include "stdafx.h"
#include "RestWrapper.h"

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/json.h>

using namespace utility;
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace concurrency::streams;

namespace mycpprest
{
    void RestWrapper::TestApi()
    {
        auto fileStream = std::make_shared<ostream>();

        // Open stream to output file.
        pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
        {
            *fileStream = outFile;

            // Create http_client to send the request.
            http_client client(U("http://13.231.231.252:3000/api/individual_employment_setting/detail/172"));

            // Build request URI and start the request.
            //uri_builder builder(U("/search"));
            //builder.append_query(U("q"), U("cpprestsdk github"));
            return client.request(methods::GET);
        })

        // Handle response headers arriving.
        .then([=](http_response response)
        {
            printf("Received response status code:%u\n", response.status_code());

            // Write response body into the file.
            // return response.body().read_to_end(fileStream->streambuf());
            stringstreambuf buffer;
            response.body().read_to_end(buffer).get();

            //show content in console
            printf("Response body: \n %s", buffer.collection().c_str());

            //parse content into a JSON object:
            //json::value jsonvalue = json::value::parse(buffer.collection());

            return  fileStream->print(buffer.collection()); //write to file anyway
        })

        // Close the file stream.
        .then([=](size_t)
        {
            return fileStream->close();
        });

        // Wait for all the outstanding I/O to complete and handle any exceptions
        try
        {
            requestTask.wait();
        }
        catch (const std::exception &e)
        {
            printf("Error exception:%s\n", e.what());
        }
    }
}

当我构建它时,它已经成功。

然后我在visual c ++中创建Windows Console Application来测试我创建的DLL项目。

我将MyCpprestDll.dll, MyCpprestDll.lib and RestWrapper.hMycppestDll project复制到DllTest project

然后在DllTest项目properties中,Linker->input->Additional Dependencies:我添加了MyCpprestDll.lib

这里是DllTest.cpp:

的代码
#include "stdafx.h"
#include "RestWrapper.h"
#include <iostream>

using namespace mycpprest;

int main()
{
    RestWrapper::TestApi();
    system("PAUSE");
    return 0;
}

它没有编译错误,但在运行错误时说:

The procedure entry point ?TestApi@RestWrapper@mycpprest@@SAXXZ could not be located in the dynamic link library

enter image description here

我试图搜索相关问题,但我不知道如何在我的dll项目中设置到我的入口点。

2 个答案:

答案 0 :(得分:0)

在构建DLL时需要使用dllexport,但在将其包含到另一个项目中时需要dllimport

This answer表示您必须使用一些宏和预处理器定义才能使其正常工作。

答案 1 :(得分:0)

在RestWrapper.h头文件中执行如下所示的操作。 请注意,必须使用__declspec(dllimport)导入可执行文件才能访问DLL的公共数据符号和对象。 此外,请确保在DLL项目属性中的C / C ++ - &gt;预处理器 - &gt;预处理器定义中定义宏RestWrapper_EXPORTS。

#ifdef RestWrapper_EXPORTS
#define RestWrapper_APIS __declspec(dllexport)
#else
#define RestWrapper_APIS __declspec(dllimport)
#endif

namespace mycpprest
{
 // This class is exported from the RestWrapper.dll
 class RestWrapper 
 {
   public:
    static RestWrapper_APIS void TestApi();     
 };
}

重建您的DLL项目。您的DllTest项目无需更改,只需使用更新的RestWrapper.h和RestWrapper.lib文件编译DllTest项目。