1

別の質問を投稿しても問題ないことを願っていますが、私の問題を理解することが重要だと思います。そのため、オープンソース プロジェクト Sumo に取り組んでおり、http 経由で json 形式でデータを送信するクラスを追加しようとしています。したがって、プロジェクトは、作業を開始する前にexeをコンパイルしてビルドしました。次に、次の 2 つのファイルを追加しました。

#ifndef connect_h
#define connect_h


#include <utils/common/SUMOTime.h>


namespace jsonc
{
    class connect
    {
    public:

        static void write(SUMOTime timestep);
    private:
        static std::wstring convert(const std::string& as);
    };
}
#endif

そして私のコード:

#include "jsonconnect.h"

#include <cpprest/http_client.h>
#include <cpprest/json.h>
#pragma comment(lib, "cpprest120_2_0.lib")
#pragma comment(lib, "C:/xerces-c-3.1.1-x86-windows-vc-10.0/lib/xerces-c_3.lib")

using namespace web;
using namespace web::http;
using namespace web::http::client;

#include <microsim/MSEdge.h>
#include <microsim/MSLane.h>
#include <microsim/MSNet.h>
#include <microsim/MSVehicle.h>
#include <utils/options/OptionsCont.h>
#include <utils/geom/GeoConvHelper.h>
#include <Windows.h>

namespace jsonc
{
    void connect::write(SUMOTime timestep) 
    {
        const bool signals = OptionsCont::getOptions().getBool("fcd-output.signals");
        MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl();
        MSVehicleControl::constVehIt it = vc.loadedVehBegin();
        MSVehicleControl::constVehIt end = vc.loadedVehEnd();

        json::value vehicles;
        json::value timestamp;
        timestamp[L"timestamp"] = json::value::string(convert(time2string(timestep)));
        vehicles[0] = timestamp;
        for (; it != end; ++it) {
            const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
            if (veh->isOnRoad()) {
                Position pos = veh->getPosition();
                MSLane* lane = veh->getLane();
                GeoConvHelper::getFinal().cartesian2geo(pos);

                json::value vehicle;
                vehicle[L"id"] = json::value::string(convert(veh->getID()));
                vehicle[L"x"] = json::value::number(pos.x());
                vehicle[L"y"] = json::value::number(pos.y());
                vehicle[L"angle"] = json::value::number(veh->getAngle());
                vehicle[L"speed"] = json::value::number(veh->getSpeed());
                vehicle[L"pos"] = json::value::number(veh->getPositionOnLane());
                vehicle[L"slope"] = json::value::number(lane->getShape().slopeDegreeAtOffset(veh->getPositionOnLane()));
                vehicle[L"lane"] = json::value::string(convert(lane->getID()));
                vehicle[L"type"] = json::value::string(convert(veh->getVehicleType().getID()));

                vehicles[1] = vehicle;

                //of.setPrecision(GEO_OUTPUT_ACCURACY);


                if (signals) {
                    //of.writeAttr("signals", toString(veh->getSignals()));
                }
            }

            http_client client(L"http://localhost:5140/");

            client.request(methods::POST, L"", vehicles.to_string().c_str(),
                L"application/json").then([](http_response response)
            {
                std::wcout << response.status_code() << std::endl;

                if (response.status_code() == status_codes::OK)
                {
                    auto body = response.extract_string();  
                }
            });
        }

    }
    std::wstring convert(const std::string& as)
    {
        // deal with trivial case of empty string
        if (as.empty())    return std::wstring();

        // determine required length of new string
        size_t reqLength = ::MultiByteToWideChar(CP_UTF8, 0, as.c_str(), (int)as.length(), 0, 0);

        // construct new string of required length
        std::wstring ret(reqLength, L'\0');

        // convert old string to new string
        ::MultiByteToWideChar(CP_UTF8, 0, as.c_str(), (int)as.length(), &ret[0], (int)ret.length());

        // return new string ( compiler should optimize this away )
        return ret;
    }
}

そのため、プロジェクトは以前に xerces を使用しており、私が言ったようにうまくいきました。nuget パッケージ casablanca を追加してビルドしようとすると、casblanca クラスと xerces で LNK エラーが発生しました。さて、プラグインをライブラリに追加すると、これらのエラーは消えました。代わりに、同じプロジェクトと別のプロジェクトでLNKエラーが再び発生し、さらに多くのエラーが発生しました.. C ++を初めて使用することに疑問を抱いていますが、そのプロジェクトを追加する前にすでに正常に構築したものに問題があるのはなぜですか?

std:basic_string LNKエラーのようなものさえあるので、今はエラーがあり、今は本当に失われています...

29>z_libmicrosim_output.lib(jsonconnect.obj) : error LNK2001: Nicht aufgelöstes externes Symbol ""__declspec(dllimport) public: class pplx::task<class web::http::http_response> __thiscall web::http::client::http_client::request(class web::http::http_request,class pplx::cancellation_token)" (__imp_?request@http_client@client@http@web@@QAE?AV?$task@Vhttp_response@http@web@@@pplx@@Vhttp_request@34@Vcancellation_token@6@@Z)".
29>z_libmicrosim_output.lib(jsonconnect.obj) : error LNK2001: Nicht aufgelöstes externes Symbol ""__declspec(dllimport) class std::shared_ptr<struct pplx::scheduler_interface> __cdecl pplx::get_ambient_scheduler(void)" (__imp_?get_ambient_scheduler@pplx@@YA?AV?$shared_ptr@Uscheduler_interface@pplx@@@std@@XZ)".
29>z_libmicrosim_output.lib(jsonconnect.obj) : error LNK2001: Nicht aufgelöstes externes Symbol ""__declspec(dllimport) long __cdecl pplx::details::platform::GetCurrentThreadId(void)" (__imp_?GetCurrentThreadId@platform@details@pplx@@YAJXZ)".
29>z_libmicrosim_output.lib(jsonconnect.obj) : error LNK2001: Nicht aufgelöstes externes Symbol ""__declspec(dllimport) public: void __thiscall pplx::details::critical_section_impl::unlock(void)" (__imp_?unlock@critical_section_impl@details@pplx@@QAEXXZ)".
29>z_libmicrosim_output.lib(jsonconnect.obj) : error LNK2001: Nicht aufgelöstes externes Symbol ""__declspec(dllimport) public: void __thiscall pplx::details::critical_section_impl::lock(void)" (__imp_?lock@critical_section_impl@details@pplx@@QAEXXZ)".
29>z_libmicrosim_output.lib(jsonconnect.obj) : error LNK2001: Nicht aufgelöstes externes Symbol ""__declspec(dllimport) public: __thiscall pplx::details::critical_section_impl::~critical_section_impl(void)" (__imp_??1critical_section_impl@details@pplx@@QAE@XZ)".
29>z_libmicrosim_output.lib(jsonconnect.obj) : error LNK2001: Nicht aufgelöstes externes Symbol ""__declspec(dllimport) public: __thiscall pplx::details::critical_section_impl::critical_section_impl(void)" (__imp_??0critical_section_impl@details@pplx@@QAE@XZ)".
29>z_libmicrosim_output.lib(jsonconnect.obj) : error LNK2001: Nicht aufgelöstes externes Symbol ""__declspec(dllimport) public: unsigned int __thiscall pplx::details::event_impl::wait(unsigned int)" (__imp_?wait@event_impl@details@pplx@@QAEII@Z)".
29>z_libmicrosim_output.lib(jsonconnect.obj) : error LNK2001: Nicht aufgelöstes externes Symbol ""__declspec(dllimport) public: void __thiscall pplx::details::event_impl::set(void)" (__imp_?set@event_impl@details@pplx@@QAEXXZ)".
29>z_libmicrosim_output.lib(jsonconnect.obj) : error LNK2001: Nicht aufgelöstes externes Symbol ""__declspec(dllimport) public: __thiscall pplx::details::event_impl::~event_impl(void)" (__imp_??1event_impl@details@pplx@@QAE@XZ)".
29>z_libmicrosim_output.lib(jsonconnect.obj) : error LNK2001: Nicht aufgelöstes externes Symbol ""__declspec(dllimport) public: __thiscall pplx::details::event_impl::event_impl(void)" (__imp_??0event_impl@details@pplx@@QAE@XZ)".
29>z_libmicrosim_output.lib(jsonconnect.obj) : error LNK2001: Nicht aufgelöstes externes Symbol ""private: static class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > __cdecl jsonc::connect::convert(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?convert@connect@jsonc@@CA?AV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@4@@Z)".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__accept@12".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__listen@8".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__send@16".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__gethostbyname@4".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__closesocket@4".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "___WSAFDIsSet@8".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__socket@12".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__bind@12".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__recv@16".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__WSACleanup@0".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__setsockopt@20".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__htons@4".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__WSAGetLastError@0".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__select@20".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__htonl@4".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__inet_addr@4".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__WSAStartup@8".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__connect@12".
29>zz_foreign_tcpip.lib(socket.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__ioctlsocket@12".

私はここで本当に迷っており、どんな助けも本当に感謝しています. そのプロジェクトを追加する前にビルドが機能したため、すべてが定義されているため、エラーに関する他のトピックで読んだものから、実際には取得できませんか? 円のようなものはありますか?

4

1 に答える 1

5

You may have resolved this issue since it's so old, but this might help others. The issue could be due to an error in the project properties in Visual Studio 2012.

If you head to:

Project Properties > C/C++ > Preprocessor > Preprocessor Definitions

And add the flag:

_NO_ASYNCRTIMP;

This prevents the compiler from trying to find the C++ REST SDK functions from a DLL. It should resolve your pplx linking errors.

Some other points that are important to note:

  • ensure that you are using the v110 binaries for your project, as those are intended for Visual Studio 2012
  • You may run across some ill-advised suggestions around the internet to "disable wchar_t" as a native type. This is generally a bad idea, because wchar_t IS a native type and the C++ REST SDK depends on it.
  • If the v110 binaries from the C++ REST SDK aren't linking properly, try the Windows XP version (v110_xp)
于 2015-01-13T21:27:28.647 に答える