2009-02-25 13 views
2

SDK를 사용하여 샘플 http 클래스를 컴파일하고 일부 이상한 링크 오류가 발생했습니다. 누락 된 옵션 또는 디렉토리와 관련이 있습니다.C++ WinHttp/Windows SDK로 컴파일 문제

나는 C + + 전문가로서 당신이 볼 수 있지만 어떤 도움을 찾고있다.

샘플 클래스를 포함 시켰습니다. Windows SDK도 설치했습니다. 내 설정 등에 관한 다른 정보가 필요하면 질문하십시오. 누군가가 작업 WinHttp SDK 샘플 프로젝트를 가리 키길 바랍니다.

//START OF utils.cpp 
#pragma once 
#include "stdafx.h" 

class http 
{ 
public: 
    http(); 
    ~http(); 

    std::string getText(); 

}; 
//END OF utils.cpp 

//START OF utils.cpp 
#include "stdafx.h" 
#include "utils.h" 

http::http() 
{ 
} 

http::~http() 
{ 
} 

std::string http::getText() 
{ 
    DWORD dwSize = 0; 
    DWORD dwDownloaded = 0; 
    LPSTR pszOutBuffer; 
    BOOL bResults = FALSE; 
    HINTERNET hSession = NULL, 
     hConnect = NULL, 
     hRequest = NULL; 

    // Use WinHttpOpen to obtain a session handle. 
    hSession = WinHttpOpen(L"WinHTTP Example/1.0", 
     WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, 
     WINHTTP_NO_PROXY_NAME, 
     WINHTTP_NO_PROXY_BYPASS, 0); 

    // Specify an HTTP server. 
    if(hSession) 
     hConnect = WinHttpConnect(hSession, L"www.microsoft.com", 
     INTERNET_DEFAULT_HTTPS_PORT, 0); 

    // Create an HTTP request handle. 
    if(hConnect) 
     hRequest = WinHttpOpenRequest(hConnect, L"GET", NULL, 
     NULL, WINHTTP_NO_REFERER, 
     WINHTTP_DEFAULT_ACCEPT_TYPES, 
     WINHTTP_FLAG_SECURE); 

    // Send a request. 
    if(hRequest) 
     bResults = WinHttpSendRequest(hRequest, 
     WINHTTP_NO_ADDITIONAL_HEADERS, 0, 
     WINHTTP_NO_REQUEST_DATA, 0, 
     0, 0); 


    // End the request. 
    if(bResults) 
     bResults = WinHttpReceiveResponse(hRequest, NULL); 

    // Keep checking for data until there is nothing left. 
    if(bResults) 
    { 
     do 
     { 
      // Check for available data. 
      dwSize = 0; 
      if(!WinHttpQueryDataAvailable(hRequest, &dwSize)) 
       printf("Error %u in WinHttpQueryDataAvailable.\n", 
       GetLastError()); 

      // Allocate space for the buffer. 
      pszOutBuffer = new char[dwSize+1]; 
      if(!pszOutBuffer) 
      { 
       printf("Out of memory\n"); 
       dwSize=0; 
      } 
      else 
      { 
       // Read the data. 
       ZeroMemory(pszOutBuffer, dwSize+1); 

       if(!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, 
        dwSize, &dwDownloaded)) 
        printf("Error %u in WinHttpReadData.\n", GetLastError()); 
       else 
        printf("%s", pszOutBuffer); 

       // Free the memory allocated to the buffer. 
       delete [] pszOutBuffer; 
      } 
     } while(dwSize > 0); 
    } 


    // Report any errors. 
    if(!bResults) 
     printf("Error %d has occurred.\n", GetLastError()); 

    // Close any open handles. 
    if(hRequest) WinHttpCloseHandle(hRequest); 
    if(hConnect) WinHttpCloseHandle(hConnect); 
    if(hSession) WinHttpCloseHandle(hSession); 

    return ""; 
} 
//END OF utils.cpp 
당신은

변경 프로젝트 설정을 winhttp.lib 또는 .cpp 파일에이 줄을 추가 링크해야


1>------ Build started: Project: winagent, Configuration: Debug Win32 ------ 
1>Compiling... 
1>utils.cpp 
1>Linking... 
1> Creating library C:\winagent\Debug\winagent.lib and object C:\winagent\Debug\winagent.exp 
1>utils.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@XZ) 
1>utils.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@XZ) 
1>utils.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected][email protected]@@[email protected]@XZ) 
1>utils.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@XZ) 
1>utils.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@XZ) 
1>utils.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@XZ) 
1>utils.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@XZ) 
1>utils.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@XZ) 
1>C:\winagent\Debug\winagent.exe : fatal error LNK1120: 8 unresolved externals 
1>Build log was saved at "file://c:\winagent\Debug\BuildLog.htm" 
1>winagent - 9 error(s), 0 warning(s) 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

답변

9

WinHttp * 함수에 대한 MSDN 참조를 확인하면 Winhttp.lib 라이브러리에 연결해야합니다.

프로젝트 설정을 열고 링커 옵션을 선택한 다음 '입력'을 선택하고 WinHttp.lib를 '추가 종속성'목록에 추가하십시오.

아니면 넣을 수있는 소스 코드에서

#pragma comment(lib, "winhttp.lib") 

(앞서 언급 한 바와 같이).

4

#pragma comment(lib, "winhttp") 
+0

Dang. 너 빠르구나. –

1

Winhttp.lib에 링크되어 있는지 확인하십시오.

1

링크 목록에 WinHttp 라이브러리를 추가하지 않았습니다.