c++ - 无法在动态链接库中找到入口点 - C++

标签 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 控制台应用程序 来测试我创建的 DLL 项目。

我将 MyCpprestDll.dll、MyCpprestDll.lib 和 RestWrapper.hMycppestDll 项目 复制到 DllTest 项目

然后在 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 项目中的入口点。

最佳答案

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

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

关于c++ - 无法在动态链接库中找到入口点 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49812271/

相关文章:

c++ - C++ 的双对数函数的实现?

delphi - Delphi中exe和DLL之间的TobjectList

c++ - 使用 C++ 创建 MetaTrader Terminal 4 DLL 文件

c++ - 在 Windows dll 中使用 boost::asio::deadline_timer 时出现死锁

c++ - 高效计算随机误差

c++ - 以特定顺序扩展可变参数模板值的最有效方法是什么?

c++ - 如何为 C++ 代码制作 64 个共享 64 位 Linux 兼容库 (*.so)

c++ - 从命令行运行 nvcc 时出现问题

c++ - VC++ 上的访问冲突错误

c++ - 检查函数指针类型的调用约定