1

I did a c++ program to send data from c++ to php using libcurl and i successfully compiled and run my file using linker option -lcurl .the code is given bellow

#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
  CURL *curl;
  CURLcode res;
  curl = curl_easy_init();
  if(curl) {
     curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/RTAnalyzer/recivecount.php");
     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
     res = curl_easy_perform(curl);
     if(res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n",
             curl_easy_strerror(res));
     else fprintf(stdout, "data sent to server");
      curl_easy_cleanup(curl);
  }
  return 0;
}`

i successfully compiled the file using command g++ sender.cpp -lcurl

and without -lcurl option it shows error

/tmp/cc3btuHe.o: In function main': sender.cpp:(.text+0x9): undefined reference tocurl_easy_init' sender.cpp:(.text+0x33): undefined reference to curl_easy_setopt' sender.cpp:(.text+0x4e): undefined reference tocurl_easy_setopt' sender.cpp:(.text+0x5a): undefined reference to curl_easy_perform' sender.cpp:(.text+0x6d): undefined reference tocurl_easy_strerror' sender.cpp:(.text+0xb5): undefined reference to `curl_easy_cleanup' collect2: error: ld returned 1 exit status

My problem is that When i tried to a successfully compiled project file with this code using cMake, it shows the same error that was shown when i didn't use -lcurl option

I added -lcurl option in my main project cmake.txt

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x -pthread -std=c++14 -lcurl ")


ADD_DEFINITIONS( -fPIC -Wno-variadic-macros -Wno-long-long -Wall -Wextra -Winit-self -Woverloaded-virtual -Wsign-promo -Wno-unused-parameter -pedantic -Woverloaded-virtual -Wno-unknown-pragmas -pthread -std=c++14 -lcurl )

when i searched about this i got that -lcurl option should come last while linking, i tried to do that and even then i am getting the error,

  /usr/bin/cmake -E cmake_link_script CMakeFiles/simple_vehicle_counting_bin.dir/link.txt --verbose=1
    /usr/bin/c++    -std=gnu++0x -pthread -std=c++14 -lcurl  -O3 -DNDEBUG   CMakeFiles/simple_vehicle_counting_bin.dir/Demo.cpp.o  -o simple_vehicle_counting -rdynamic libsimple_vehicle_counting.a /usr/local/lib/libopencv_stitching.so.3.3.1 /usr/local/lib/libopencv_superres.so.3.3.1 
/usr/local/lib/libopencv_videostab.so.3.3.1 /usr/local/lib/libopencv_photo.so.3.3.1 /usr/local/lib/libopencv_aruco.so.3.3.1 /usr/local/lib/libopencv_bgsegm.so.3.3.1 /usr/local/lib/libopencv_bioinspired.so.3.3.1 /usr/local/lib/libopencv_ccalib.so.3.3.1 
/usr/local/lib/libopencv_cvv.so.3.3.1 /usr/local/lib/libopencv_dpm.so.3.3.1 /usr/local/lib/libopencv_face.so.3.3.1 /usr/local/lib/libopencv_freetype.so.3.3.1 /usr/local/lib/libopencv_fuzzy.so.3.3.1 
/usr/local/lib/libopencv_hdf.so.3.3.1 /usr/local/lib/libopencv_img_hash.so.3.3.1 /usr/local/lib/libopencv_line_descriptor.so.3.3.1 /usr/local/lib/libopencv_optflow.so.3.3.1 /usr/local/lib/libopencv_reg.so.3.3.1 
/usr/local/lib/libopencv_rgbd.so.3.3.1 /usr/local/lib/libopencv_saliency.so.3.3.1 /usr/local/lib/libopencv_sfm.so.3.3.1 
/usr/local/lib/libopencv_stereo.so.3.3.1 /usr/local/lib/libopencv_structured_light.so.3.3.1 /usr/local/lib/libopencv_phase_unwrapping.so.3.3.1 /usr/local/lib/libopencv_surface_matching.so.3.3.1 /usr/local/lib/libopencv_tracking.so.3.3.1 /usr/local/lib/libopencv_datasets.so.3.3.1 /usr/local/lib/libopencv_plot.so.3.3.1 /usr/local/lib/libopencv_text.so.3.3.1 /usr/local/lib/libopencv_dnn.so.3.3.1 /usr/local/lib/libopencv_xfeatures2d.so.3.3.1 /usr/local/lib/libopencv_ml.so.3.3.1 /usr/local/lib/libopencv_shape.so.3.3.1 /usr/local/lib/libopencv_video.so.3.3.1 /usr/local/lib/libopencv_ximgproc.so.3.3.1 /usr/local/lib/libopencv_calib3d.so.3.3.1 /usr/local/lib/libopencv_features2d.so.3.3.1 /usr/local/lib/libopencv_flann.so.3.3.1 /usr/local/lib/libopencv_highgui.so.3.3.1 /usr/local/lib/libopencv_videoio.so.3.3.1 /usr/local/lib/libopencv_xobjdetect.so.3.3.1 /usr/local/lib/libopencv_imgcodecs.so.3.3.1 /usr/local/lib/libopencv_objdetect.so.3.3.1 /usr/local/lib/libopencv_xphoto.so.3.3.1 /usr/local/lib/libopencv_imgproc.so.3.3.1 /usr/local/lib/libopencv_core.so.3.3.1 -Wl,-rpath,/usr/local/lib 
    CMakeFiles/simple_vehicle_counting_bin.dir/Demo.cpp.o: In function `senddata()':
    Demo.cpp:(.text+0x33): undefined reference to `curl_easy_strerror'
    Demo.cpp:(.text+0x78): undefined reference to `curl_easy_cleanup'
    Demo.cpp:(.text+0x9d): undefined reference to `curl_easy_init'
    Demo.cpp:(.text+0xbb): undefined reference to `curl_easy_setopt'
    Demo.cpp:(.text+0xd1): undefined reference to `curl_easy_setopt'
    Demo.cpp:(.text+0xd9): undefined reference to `curl_easy_perform'
    collect2: error: ld returned 1 exit status
    CMakeFiles/simple_vehicle_counting_bin.dir/build.make:141: recipe for target 'simple_vehicle_counting' failed
    make[2]: *** [simple_vehicle_counting] Error 1
    make[2]: Leaving directory '/home/elisa/PROJECTS/work/simple_vehicle_counting/build'
    CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/simple_vehicle_counting_bin.dir/all' failed
    make[1]: *** [CMakeFiles/simple_vehicle_counting_bin.dir/all] Error 2
    make[1]: Leaving directory '/home/elisa/PROJECTS/work/simple_vehicle_counting/build'
    Makefile:83: recipe for target 'all' failed
    make: *** [all] Error 2

please tell me how can i ensure i used linker option last,how to do it or is there any other way to get rid of this problem

Jabir Ali
  • 568
  • 6
  • 11

0 Answers0