0

I've been searching around and have come to a stop with this thing.

I basically get these errors here:

1>Client.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl HTTP_POST_REQUEST(char *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?HTTP_POST_REQUEST@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PADV12@1@Z) referenced in function "public: virtual void __thiscall Client::Send(void)" (?Send@Client@@UAEXXZ)
1>G:\Development\C++\Client\Debug\Client.exe : fatal error LNK1120: 1 unresolved externals

To my knowledge I think it's that Client.obj is not getting compiled with the program (don't quote me on this)? But I have read that it might be some linking problems or something like that. To me it seems that I've done everythign right (but clearly I haven't).

Client.cpp

#include <iostream>
#include <string>
using namespace std;

#include "HttpUtils.h"
#include "Client.h"

Client::Client() {
    cout << "Works!" << endl;
}

void Client::Send() {
    string res = HTTP_POST_REQUEST("localhost", "/test.php", "data1=data&data2=data");
    cout << res << endl;
}

Client.h

#pragma once

#include <string>
using namespace std;

#ifndef CLIENT__H
#define CLIENT__H

class Client {

public:
    Client();

    virtual void Send();
};
#endif

main.cpp

#include <iostream>
#include <string>
using namespace std;

#include "Client.h"

int main(int argc, char *argv[]) {
    Client mainClient;
    Client* client1 = &mainClient;

    client1->Send();
    cin.get();
}

Any and all help is great! Thanks!

EXTRA CODE

HttpUtils.cpp

#include <stdlib.h>
#include <winsock.h>
#include <sstream>
#pragma comment (lib, "wsock32.lib")

#include <iostream>
#include <string>
using namespace std;

#include "HttpUtils.h"

void die_with_error(char *errorMessage) {
    cerr << errorMessage << endl;
    cin.get();
    exit(1);
}

void die_with_wserror(char *errorMessage) {
    cerr << errorMessage << ": " << WSAGetLastError() << endl;
    cin.get();
    exit(1);
}

string HTTP_POST_REQUEST(char *hostname, string path, string data) {
    string request;
    string response;
    int resp_leng;

    char buffer[BUFFERSIZE];
    struct sockaddr_in serveraddr;
    int sock;

    WSADATA wsaData;
    hostent *remoteHost;
    int port = 80;

    stringstream ss;
    ss << data.length();

    stringstream request2;
    request2 << "POST " << path << " HTTP/1.1" << endl;
    request2 << "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" << endl;
    request2 << "Host: " << hostname << endl;
    request2 << "Content-Length: " << data.length() << endl;
    request2 << "Content-Type: application/x-www-form-urlencoded" << endl;
    request2 << "Accept-Language: en-uk" << endl;
    request2 << endl;
    request2 << data;
    request = request2.str();

    cout << request << endl << "###################################################" << endl << endl;

    //  Init WinSock.
    if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
        die_with_wserror("WSAStartup() failed");

    //  Open socket.
    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        die_with_wserror("socket() failed");


    //  Convert hostname into IP
    remoteHost = gethostbyname(hostname);
    char *temp;
    if (remoteHost != NULL) {
        temp = remoteHost->h_addr_list[0];
    } else {
        temp = NULL;
        return "Error: Invalid hostname passed to HTTP_POST().";
    }


    //  Connect.
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;

    int i = 0;
    if (remoteHost->h_addrtype == AF_INET)
    {
        while (remoteHost->h_addr_list[i] != 0) {
            serveraddr.sin_addr.s_addr = *(u_long *)remoteHost->h_addr_list[i++];
        }
    }

    serveraddr.sin_port = htons((unsigned short)port);
    if (connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
        die_with_wserror("connect() failed");

    //  Send request.
    if (send(sock, request.c_str(), request.length(), 0) != request.length())
        die_with_wserror("send() sent a different number of bytes than expected");

    //  Get response.
    response = "";
    resp_leng = BUFFERSIZE;
    while (resp_leng == BUFFERSIZE)
    {
        resp_leng = recv(sock, (char*)&buffer, BUFFERSIZE, 0);
        if (resp_leng>0)
            response += string(buffer).substr(0, resp_leng);
        //note: download lag is not handled in this code
    }

    //  Disconnect
    closesocket(sock);

    //  Cleanup
    WSACleanup();

    //response.erase(0, 184);
    return  response;
}

HttpUtils.h

#pragma once

#include <iostream>
#include <string>
using namespace std;

#ifndef HTTP_UTILS__H
#define HTTP_UTILS__H
#define BUFFERSIZE 1024
void die_with_error(char *errorMessage);
void die_with_wserror(char *errorMessage);

string HTTP_POST_REQUEST(char *hostname, string path, string data);
#endif
mikkel1156
  • 71
  • 3
  • 10
  • 1
    Do you really have `Client->Send();` in your `main`? Shouldn't it be `client1->Send();`? – Ahmad Khan Aug 29 '16 at 16:14
  • That was just a typo when copying over the code, but the errors are still there. – mikkel1156 Aug 29 '16 at 16:17
  • Where is `HTTP_POST_REQUEST()` implemented? Is there some library that you forgot to link? – drescherjm Aug 29 '16 at 16:17
  • I think, you need to provide `HttpUtils.h` too. – Ahmad Khan Aug 29 '16 at 16:18
  • It's in HttpUtils.h (included in Client.cpp), but I haven't gotten any errors about not being able to access it, think it might be the cause though? – mikkel1156 Aug 29 '16 at 16:19
  • The problem most likely is whatever implements `HTTP_POST_REQUEST()` is not in your project or you are not linking to the library that implements this function. – drescherjm Aug 29 '16 at 16:20
  • Your error is obviously mentioning `HTTP_POST_REQUEST()`,so without it's implementation,it's almost impossible to guess the error. – Ahmad Khan Aug 29 '16 at 16:22
  • There really is not much we can do to help. The problem likely does not have anything to do with header files. – drescherjm Aug 29 '16 at 16:23
  • I just did a test by commenting out HTTP_POST_REQUEST() and then just use cout to debug, and it works with no errors. But why isn't it getting implemented? I can add the code for HttpUtils(where HTTP_POST_REQUEST() is) if you guys would like (which I'm sure you would if I want help). – mikkel1156 Aug 29 '16 at 16:24
  • ***But why isn't it getting implemented?*** How would/could we possibly know? We don't have access to your PC. – drescherjm Aug 29 '16 at 16:25
  • Yes, add the code of `HTTP_POST_REQUEST()` here too. – Ahmad Khan Aug 29 '16 at 16:25
  • Is HttpUtils.cpp part of your project? Perhaps it is not being compiled as part of your project. Just including a header file does not make it defined. Well unless the definition is in the header. – drescherjm Aug 29 '16 at 16:26
  • @drescherjm To my knowledge they are yes (even have it under Additional Include Directories). Extra code added as well. – mikkel1156 Aug 29 '16 at 16:32
  • Your code works fine for me, I can't reproduce the error. – Ahmad Khan Aug 29 '16 at 16:34
  • I haven't been touching the properties of the project (other than Additional Include Directories, but this was an attempt to fix the error, so it was before i did anything like that) so nothing should be wrong in there. – mikkel1156 Aug 29 '16 at 16:36
  • `Additional Include Directories` should have nothing to do with including `HttpUtils.cpp` to your project. – drescherjm Aug 29 '16 at 16:36
  • I just read around that it might have been something like. Changing it back now. – mikkel1156 Aug 29 '16 at 16:38
  • 1
    `Unresolved External Symbol` error often comes when it finds the prototype of the function which you're calling, but can't find the implementation, so double check whether `HttpUtils.cpp` is included in your project or not. – Ahmad Khan Aug 29 '16 at 16:39
  • Remember in Visual Studio you need to explicitly add each cpp file to your project. It does not help to put them in the same folder as other cpp files. And it also does not help to just include the header for the class. That will not pull in the cpp file into your project. – drescherjm Aug 29 '16 at 16:43
  • Thank you so much @MuhammadAhmad and @drescherjm!!! I double checked the Solution Explorer panel and the file wasn't there. Thanks a lot for all helping me out in this stupid question. – mikkel1156 Aug 29 '16 at 16:47
  • 4
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – drescherjm Aug 29 '16 at 16:50

1 Answers1

2

make sure you add HttpUtils.cpp to your make file/command .. it sounds like HttpUtils.cpp is not compiled. That's why Client can't find HttpUtils symbols !

HazemGomaa
  • 1,620
  • 2
  • 14
  • 21