Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Library path for C++. Fail?

Im trying to execute this code on a mac. I have installed the curl. When i search for curl, i can find it under /usr/include/curl/curl.h. Below is the program i want to run. Taken from here.

#include <iostream>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}

when i try to compile the program. see below.

g++ simple.cpp 
Undefined symbols:
  "_curl_easy_perform", referenced from:
      _main in cciFNPkt.o
  "_curl_easy_init", referenced from:
      _main in cciFNPkt.o
  "_curl_easy_setopt", referenced from:
      _main in cciFNPkt.o
  "_curl_easy_cleanup", referenced from:
      _main in cciFNPkt.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

What s going on? why doesnt the program finds the path of this file?

How can i fix it?

like image 308
DarthVader Avatar asked Mar 25 '26 10:03

DarthVader


1 Answers

You’ve forgotten to link the libcurl library:

g++ simple.cpp -lcurl

A header file such as <curl/curl.h> (typically) only declares that a group of functions exist but does not contain the actual functions. In the case of curl on OS X Lion, those functions are in libcurl (/usr/lib/libcurl.dylib, which points to /usr/lib/libcurl.4.dylib). You need to tell the linker (via the compiler frontend) about that library, which can be done via -lcurl.