0

I want to get a file via http using curl. My environment is GCC (Mac OS X). I've created a simple class Shared to use it smth like this:

#include "Shared.h"
...
std::string str = Shared::getHTTPFile( "http://google.com" );

When I try to compile it with the following command:

g++ -o test -l curl test.cpp

I get an error message:

Undefined symbols for architecture x86_64:
"Shared::curlBuffer", referenced from:
 Shared::getHTTPFile(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in test-a10048.o
 Shared::writer(char*, unsigned long, unsigned long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) in test-a10048.o
"Shared::errorBuffer", referenced from:
 Shared::getHTTPFile(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in test-a10048.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

My code of Shared class is just simple:

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

class Shared {

public:
    static char errorBuffer[ CURL_ERROR_SIZE ];
    static std::string curlBuffer;
    static int writer( char *data, size_t size, size_t nmemb, std::string *buffer ) {
        curlBuffer.append( data, size * nmemb );
        int result = size * nmemb;
        return result;
    }

    static std::string getHTTPFile( std::string url ) {
        CURL *curl = curl_easy_init();
        CURLcode result;
        if (curl) {
            curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
            curl_easy_setopt(curl, CURLOPT_URL, "http://google.com" );
            curl_easy_setopt(curl, CURLOPT_HEADER, 1);
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer );
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &curlBuffer );
            result = curl_easy_perform( curl );
        }
        curl_easy_cleanup(curl);
        return 0;
    }
};

What do I do wrong here?

JavaRunner
  • 2,455
  • 5
  • 38
  • 52

1 Answers1

2

g++ -o test -l curl test.cpp

Looks like you only compile "test.cpp". You should also compile "Shared.cpp". Furthermore, it should be "-lcurl", not "-l curl"

IsaacKleiner
  • 425
  • 2
  • 13
  • I don't have "Shared.cpp" :) I have only "Shared.h" and its realization there. Should I compile Shared.h first? I tried to remove the space to get: "-lcurl" but it didn't solve the problem. – JavaRunner Jan 11 '15 at 12:20
  • 1
    Ah, I can see it now. You declared some static members "curlBuffer" and "errorBuffer" in your "Shared" class, but you didn't initialize them yet. You can do that in a separate "Shader.cpp" or right after your class definition. Check out this: http://www.tutorialspoint.com/cplusplus/cpp_static_members.htm – IsaacKleiner Jan 11 '15 at 12:41