-->

如何解决这个错误:未定义的引用`__imp_InternetOpenA”(How to fix th

2019-10-19 19:07发布

我想使C ++程序,其中的信息到我的电脑发送.txt文件。 我需要收费这么多,在互联网,但不能找到一个合适的方法。 当我uusing开发的C ++给我这个错误:...:未定义的引用__imp_InternetOpenA' ...: undefined reference to __imp_InternetConnectA” ...:未定义参考__imp_FtpPutFileA' ...: undefined reference to __imp_HttpOpenRequestA”这里有三个例子在那里我发现,但都返回该错误。

  <pre>

   #include <windows.h>
   #include <wininet.h>
   #include <iostream>
   #include <stdio.h>
   #include <tchar.h>

  #pragma comment(lib,"wininet.lib")
  #define ERROR_OPEN_FILE       10
  #define ERROR_MEMORY          11
  #define ERROR_SIZE            12
  #define ERROR_INTERNET_OPEN   13
  #define ERROR_INTERNET_CONN   14
  #define ERROR_INTERNET_REQ    15
  #define ERROR_INTERNET_SEND   16

  using namespace std;

  int main()
  {
     // Local variables
     char filename[]   = "file";   //Filename to be loaded
     char filepath[]   = "d:\\a.jpg";   //Filename to be loaded
     char type[]       = "image/jpeg";
     char boundary[]  = "--BOUNDARY---";            //Header boundary
     char nameForm[]  = "formname";     //Input form name
     char iaddr[]     = "localhost";        //IP address
     char url[]       = "/http/file.php";         //URL

     char hdrs[512]={'-'};                  //Headers
     char * buffer;                   //Buffer containing file + headers
     char * content;                  //Buffer containing file
     FILE * pFile;                    //File pointer
     long lSize;                      //File size
     size_t result;                   


     // Open file
     pFile = fopen ( filepath , "rb" );
     if (pFile==NULL) 
     {
         printf("ERROR_OPEN_FILE");
         getchar();
         return ERROR_OPEN_FILE;
     }
     printf("OPEN_FILE\n");

     // obtain file size:
     fseek (pFile , 0 , SEEK_END);
     lSize = ftell (pFile);
     rewind (pFile);

     // allocate memory to contain the whole file:
     content = (char*) malloc (sizeof(char)*lSize);
     if (content == NULL) 
     {
         printf("ERROR_MEMORY");
         getchar();
         return ERROR_OPEN_FILE;
     }
     printf("MEMORY_ALLOCATED\t \"%d\" \n",&lSize);
     // copy the file into the buffer:
     result = fread (content,1,lSize,pFile);
     if (result != lSize) 
     {
         printf("ERROR_SIZE");
         getchar();
         return ERROR_OPEN_FILE;
     }
     printf("SIZE_OK\n");

     // terminate
     fclose (pFile);
     printf("FILE_CLOSE\n");
     //allocate memory to contain the whole file + HEADER
     buffer = (char*) malloc (sizeof(char)*lSize + 2048);

     //print header
     sprintf(hdrs,"Content-Type: multipart/form-data; boundary=%s",boundary);
     sprintf(buffer,"%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%       


----------


s\"\r\n",boundary,nameForm,filename);
     sprintf(buffer,"%sContent-Type: %s\r\n",buffer,type);
     sprintf(buffer,"%s%s",buffer,content);
     sprintf(buffer,"%s--%s--\r\n",buffer,boundary);



     //Open internet connection
     HINTERNET hSession = InternetOpen("WINDOWS",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
     if(hSession==NULL) 
     {
         printf("ERROR_INTERNET_OPEN");
         getchar();
         return ERROR_OPEN_FILE;
     }
     printf("INTERNET_OPENED\n");

     HINTERNET hConnect = InternetConnect(hSession, iaddr,INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
     if(hConnect==NULL) 
     {
         printf("ERROR_INTERNET_CONN");
         getchar();
         return ERROR_INTERNET_CONN;
     }
     printf("INTERNET_CONNECTED\n");

     HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T(url),NULL, NULL, NULL,INTERNET_FLAG_RELOAD, 1);
     if(hRequest==NULL) 
      {
         printf("ERROR_INTERNET_REQ");
         getchar();

     }
     printf("INTERNET_REQ_OPEN\n");

     BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));

     if(!sent) 
     {
         printf("ERROR_INTERNET_SEND");
         getchar();
         return ERROR_INTERNET_CONN;
     }
     printf("INTERNET_SEND_OK\n");

     //close any valid internet-handles
     InternetCloseHandle(hSession);
     InternetCloseHandle(hConnect);
     InternetCloseHandle(hRequest);



     getchar();
     return 0;
  }

<pre>
  #include <winsock2.h>
  #include <wininet.h>
  #include <tchar.h>
  #include <iostream>
  #include <stdlib.h>
  #include <windows.h>

//#pragma comment(lib,"wininet.lib")

using namespace std;

int main()
{

    static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\test.txt\"\nContent-Type: text/plain\n\nfile contents  here\n-----------------------------7d82751e2bc0858--"; 
    static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858"; 

    HINTERNET hSession = InternetOpen("MyAgent",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
     if(hSession==NULL)
    {
     cout<<"Error: InternetOpen";  
    }


    HINTERNET hConnect = InternetConnect(hSession, _T("localhost"),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
     if(hConnect==NULL)
    {
     cout<<"Error: InternetConnect";  
    }

    HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T("upload.php"), NULL, NULL, (const char**)"*/*\0", 0, 1);
    if(hRequest==NULL)
    {
     cout<<"Error: HttpOpenRequest";  
    }

    BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
    if(!sent)
    {
     cout<<"Error: HttpSendRequest";
     }

    //close any valid internet-handles
    InternetCloseHandle(hSession);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hRequest);

    return 0;
}

#include <windows.h> #include <wininet.h> #include <stdio.h> #include <iostream> #define ERROR_OPEN_FILE 10 #define ERROR_MEMORY 11 #define ERROR_SIZE 12 #define ERROR_INTERNET_OPEN 13 #define ERROR_INTERNET_CONN 14 #define ERROR_INTERNET_REQ 15 #define ERROR_INTERNET_SEND 16 using namespace std; int main() { // Local variables static char filename[] = "test.txt"; //Filename to be loaded static char type[] = "image/jpg"; static char boundary[] = "pippo"; //Header boundary static char nameForm[] = "uploadedfile"; //Input form name static char iaddr[] = "localhost"; //IP address static char url[] = "test.php"; //URL char hdrs[255]; //Headers char * buffer; //Buffer containing file + headers char * content; //Buffer containing file FILE * pFile; //File pointer long lSize; //File size size_t result; // Open file pFile = fopen ( filename , "rb" ); if (pFile==NULL) return ERROR_OPEN_FILE; // obtain file size: fseek (pFile , 0 , SEEK_END); lSize = ftell (pFile); rewind (pFile); // allocate memory to contain the whole file: content = (char*) malloc (sizeof(char)*lSize); if (content == NULL) return ERROR_MEMORY; // copy the file into the buffer: result = fread (content,1,lSize,pFile); if (result != lSize) return ERROR_SIZE; // terminate fclose (pFile); //allocate memory to contain the whole file + HEADER buffer = (char*) malloc (sizeof(char)*lSize + 2048); //print header sprintf(hdrs,"Content-Type: multipart/form-data; boundary=%s",boundary); sprintf(buffer,"--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n",boundary,nameForm,filename); sprintf(buffer,"%sContent-Type: %s\r\n\r\n",buffer,type); sprintf(buffer,"%s%s\r\n",buffer,content); sprintf(buffer,"%s--%s--\r\n",buffer,boundary); //Open internet connection HINTERNET hSession = InternetOpen("WinSock",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if(hSession==NULL) return ERROR_INTERNET_OPEN; HINTERNET hConnect = InternetConnect(hSession, iaddr,INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1); if(hConnect==NULL) return ERROR_INTERNET_CONN; HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",url, NULL, NULL, (const char**)"*/*\0", 0, 1); if(hRequest==NULL) return ERROR_INTERNET_REQ; BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer)); if(!sent) return ERROR_INTERNET_SEND; //close any valid internet-handles InternetCloseHandle(hSession); InternetCloseHandle(hConnect); InternetCloseHandle(hRequest); return 0; } </pre>

Answer 1:

我对我的第一个C ++程序的许多错误是这样的。 这是对图书馆的WinINet连接有问题。 如果您正在使用的MinGW添加“-lwininet”(不带引号)的额外commandling参数,它应该是固定的。 我不知道,如果你使用VC怎样做才能++。 此外,确保图书馆的WinINet的位置是在连接器的搜索路径。



Answer 2:

随着CMake的你只需要

target_link_libraries( MY_TARGET
                       ws2_32)


文章来源: How to fix this error: undefined reference to `__imp_InternetOpenA'