0

When I try to compile

$ gcc -lcurl try.cpp

/tmp/ccJs0k9m.o: In function `main':
try.cpp:(.text+0x2d): undefined reference to `getURL::fetch(char*, char*)'
collect2: ld returned 1 exit status

The method appears in the header and the class body. What is wrong exactly?

try.cpp

#include <curl/curl.h>
#include <curl/easy.h>
#include "getURL.h"

int main(void) {

getURL my_getURL;

my_getURL.fetch("http://stackoverflow.com/", "file");
}

getURL.h

#ifndef _getURL
#define _getURL

class getURL {
public:
    void fetch(char *url, char *filename);
};
#endif 

getURL.cpp

#include <curl/curl.h>
#include <curl/easy.h>
#include <stdio.h>


class getURL {

private CURL *curl;

public getURL() {
    //code
}

public void fetch(char *url, char *filename) {
    //code
}

private size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    //code
}

public ~getURL() {
    //code
}

} //end class
4
  • 2
    $ gcc try.cpp getURL.cpp -lcurl Commented Sep 1, 2012 at 17:19
  • 1
    @jrok: Libraries have to come at the end of the line. Commented Sep 1, 2012 at 17:20
  • @KerrekSB a fact that has caused me much headache. Commented Sep 1, 2012 at 17:22
  • 2
    How does getURL.cpp even compile? Doesn't look like C++. Where are the colons after the access specifiers? Semicolon after the class definition? And if it could somehow be convinced to compile, then getURL.cpp and try.cpp have completely different ideas about what a getURL object looks like. [edit: ninja'd] Commented Sep 1, 2012 at 17:33

2 Answers 2

3

You are not using the correct implementation syntax; it's

getURL::getURL() {
    //code
}

void getURL::fetch(char *url, char *filename) {
    //code
}

size_t getURL::write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    //code
}

getURL::~getURL() {
    //code
}

Note that you must not repeat the class part in the implementation (just include the header instead). Note also that you're not allowed to have private members in the implementation that are not visible in the declaration... this is unfortunate but it's how the language is defined.

Sign up to request clarification or add additional context in comments.

3 Comments

They also need to lose a lot of "public" and "private" in the function definitions.
I added getURL:: to the start of the method names, removed public and private before them. I added the private method to the header file (why?). And removed class {} from getURL.cpp. I think that sums up what you said. Exact same error.
Also to compile a C++ program you need to call g++, not gcc.
0

You are only compiling one of your source files.

Change:

gcc -lcurl try.cpp

to:

gcc -lcurl try.cpp getURL.cpp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.