CURL binding in Makefile

I need a cURL link in Ubuntu 11.04 after installing cURL in the source code.

.

CORRECTION PROBLEMS

First, I discovered that -l should arrive before -L, and then I discovered that I had not entered the variable in the makefile.

.

Get cURL configurations:

On my terminal:

# curl-config --libs -L/usr/local/lib -lcurl # curl-config --cflags -I/usr/local/include 

Everything is in order where there are cURL files in this directory.


My Makefile:

 # Testing cURL # MAKEFILE # C++ Compiler (Default: g++) CXX = g++ CFLAGS = -Wall -Werror # Librarys INCLUDE = -Iusr/local/include LDFLAGS = -Lusr/local/lib LDLIBS = -lcurl # Details SOURCES = src/main.cpp OUT = test all: build build: $(SOURCES) $(CXX) -o $(OUT) $(INCLUDE) $(CFLAGS) $(LDFLAGS) $(SOURCES) 

My C ++ source code:

 #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); curl_easy_cleanup(curl); } return 0; } 

AND ERROR:

 # make g++ -o test -Iusr/local/include -Wall -Werror -Lusr/local/lib src/main.cpp /tmp/ccli90i2.o: In function `main': main.cpp:(.text+0xa): undefined reference to `curl_easy_init' main.cpp:(.text+0x31): undefined reference to `curl_easy_setopt' main.cpp:(.text+0x3d): undefined reference to `curl_easy_perform' main.cpp:(.text+0x4d): undefined reference to `curl_easy_cleanup' collect2: ld returned 1 exit status make: ** [build] Erro 1 

I know that this is a mistake when you do not find the library, but for me everything is correct.

+4
source share
2 answers

That should do the job. You have not referenced cURL before.

 build: $(SOURCES) $(CXX) -o $(OUT) $(INCLUDE) $(CFLAGS) $(LDFLAGS) $(LDLIBS) $(SOURCES) 

Note the added $(LDLIBS) .

Oh, I have to add that basically what happens is that you throw the GNU make built-in rules overboard (see make -np ) and define your own. I would suggest that you either use the built-in if you want to rely on the appropriate variables to be sufficient to control the assembly, or that you still split it into a compilation step and links for short.

Short explanation: GNU make comes with a rule that says how to make a .o file from a .cpp (or .c ) file. This way your file can be rewritten (approximately)

 # Testing cURL # MAKEFILE # C++ Compiler (Default: g++) CXX = g++ CFLAGS = -Wall -Werror # Librarys INCLUDE = -I/usr/local/include LDFLAGS = -L/usr/local/lib LDLIBS = -lcurl # Details SOURCES = src/main.cpp OUT = test .PHONY: all all: build $(OUT): $(patsubst %.cpp,%.o,$(SOURCES)) 

This should generate a binary called test (contents OUT ) and otherwise use the built-in rules. Create files from .o files, which should be source files, will search for them and compile them. Thus, implicitly, this assembly will trigger one call for each .cpp file and one for the build phase.

+3
source

You are missing the slash at the beginning of the paths below.

 -I/usr/local/include -L/usr/local/lib 
0
source

All Articles