How to Compile & run in Command line consuming RESTSDK

Feb 11, 2015 at 10:08 AM
I am facing issue with compiling & running programs in command line.

I am having a restapi.cpp & restapi.h files, which works perfectly fine when run through VC++ console application after installing "C++ Rest SDK" from Nuget manager.

To Compile from command line I followed below steps::
  1. Downloaded casablanca using : git clone
  2. Then Using Visual Studio 2013, Build the cpprestsdk120 solution. which successfully created cpprest120d_2_4.dll under Binaries/Win32/Debug/
  3. Compiled the Program: ( In VS Native tools Command Prompt)
    C:\TestRest> cl /EHsc RestAPI.cpp /I C:\Users\Labs\Desktop\casablanca\Release\include C:\Users\Labs\Desktop\casablanca\Binaries\Win32\Debug\cpprest120d_2_4.lib
    This creates RestAPI.exe without any error or warning.
  4. Run the Program
    C:\TestRest> RestAPI.exe
    I get few prints from programs before crashing. I get run time error/crash with "RestAPI.exe has Stopped working".
  5. I also tried C:\TestRest> RestAPI.exe C:\Users\Labs\Desktop\casablanca\Binaries\Win32\Debug\cpprest120d_2_4.dll
    Still same error.
Sample Part of Code:
void myrestfunc() {
        std::string urllink;
        urllink = "";
        cout << "I am here 1" << endl;
         uri_builder builder(utility::conversions::to_string_t(urllink));
         cout << "I am here 2" << endl; 

I get "I am here 1" output before crash..

I am sure, i am doing mistake during compilation or running b'çoz of which dll is not getting loaded.

Please help.
Feb 11, 2015 at 10:25 PM
Hi Savit,

Because you're manually calling cl.exe to compile your application, you are likely not passing in the same arguments used to create cpprest120d_2_4.lib (such as DEBUG_ITERATOR and other macros). This causes ABI issues which definitely could cause the behavior you're seeing.

Create a project for your console application, adding the lib and include path from your compiled copy of cpprest. Make sure to copy cpprest120d_2_4.dll to your output directory. If you want to compile from the command line, you can use "msbuild.exe" directly on the created ".vcxproj" to build.

If this doesn't solve the problem, please post the full code for RestAPI.cpp.

Feb 16, 2015 at 5:35 AM

I need to add these compilation steps into a MAKEFILE. So creating a project solution/using msbuild.exe is not feasible for our requirement.
The requirement is, will be provided with set of CPP files which uses C++ REST SDK.
Need to compile all of them in a MAKEFILE, to create a executable.

Below is the link to access to the video recording of the issue faced, and it also has complete REST_HTTPGet.cpp

(My voice is not so audible, sorry for tht)

Do i need to do add dllimport in the code? I am new to VC++, let me know if the issue is with REST_HTTPGet.cpp file.
Feb 16, 2015 at 6:59 PM
Thank you for the video, it was very illustrative :).

From inside the VS Command Tools prompt, you can rebuild your .vcxproj using msbuild.exe MyC++Project.vcxproj /t:Rebuild. In the output, you should see the actual command line being passed to CL, something like
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\CL.exe /c /ID:\casablanca\Release\include /ID:\casablanca\Release\src\pch /ID:\casablanca\Release\libs\websocketpp /ID:\casablanca\Release\src\build\ /ID:\casablanca\Release\src\build\ /ZI /nologo /W4 /WX /Od /Oy- /D CPPREST_EXCLUDE_WEBSOCKETS /D _ASYNCRT_EXPORT /D _PPLX_EXPORT /D WIN32 /D _MBCS /D _USRDLL /D _DEBUG /D _WINDLL /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Yc"stdafx.h" /Fp"D:\casablanca\Intermediate\casablanca140\Win32\Debug\cpprest140d_2_4.pch" /Fo"D:\casablanca\Intermediate\casablanca140\Win32\Debug\\" /Fd"D:\casablanca\Intermediate\casablanca140\Win32\Debug\vc140.pdb" /Gd /TP /analyze- /errorReport:queue -Zm300 /bigobj  /d2notypeopt D:\casablanca\Release\src\build\..\pch\stdafx.cpp
You can copy that line into your Makefile. It should display the call to LINK as well.