动态链接库中找不到入口点 - C++

3
我创建了一个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控制台应用程序”来测试我创建的DLL项目。 我将“MyCpprestDll.dll、MyCpprestDll.lib和RestWrapper.h”从“MyCpprestDll项目”复制到“DllTest项目”中。 然后在DllTest项目的“属性”中,在“链接器->输入->附加依赖项”中:我添加了“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
在您的RestWrapper.h头文件中,执行如下操作。请注意,您必须使用__declspec(dllimport)来使导入可执行文件能够访问DLL的公共数据符号和对象。此外,请确保在DLL项目属性的C/C++->预处理器->预处理器定义中定义宏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项目即可。

0

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

此答案向您展示了如何使用一些宏和预处理器定义来使其工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接