在 Makefile 中鏈接 cURL (Linking cURL in Makefile)


問題描述

在 Makefile 中鏈接 cURL (Linking cURL in Makefile)

I need link cURL in Ubuntu 11.04 after installed cURL by source code.

.

Correction of the PROBLEM

First I discovered that the -l must come before the -L and then discovered that I was not entering a variable in the makefile.

.

Get cURL Configs:

On my termial:

# curl-config --libs
-L/usr/local/lib -lcurl

# curl-config --cflags
-I/usr/local/include

It's all right, where this directory there are files cURL.


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 the 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 this is a error of not finding the library, but for me everything is correct


參考解法

方法 1:

This should do the job. You didn't really link to cURL before.

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

Notice the added $(LDLIBS).

Oh, I should add that basically what happens is that you throw overboard the built-in rules of GNU make (see output of make -np) and define your own. I would suggest that you either use the built-in ones if you want to rely on the respective variables to be sufficient to control the build or that you still split it up into compilation and link step for the sake of brevity.

Brief explanation: GNU make comes with a rule that states how to make a .o file from a .cpp (or .c) file. So your make file could perhaps be rewritten to (approx.)

# 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 the binary with the name test (contents of OUT) and makes otherwise use of the built-in rules. Make infers from the use of .o files that there must be source files, will look for them and compile them. So implicitly this build will run one invocation for each .cpp file and one for the linking step.

方法 2:

You are missing slashes at the start of the paths below

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

(by Bruno Alano0xC0000022LEelke)

參考文件

  1. Linking cURL in Makefile (CC BY-SA 3.0/4.0)

#curl #C++ #linker #makefile






相關問題

如何在所有 PayPal 交易中退還費用 (How to return fee in all PayPal transactions)

Cú pháp lệnh curl để đăng ký device_token trên Urban Airship là gì? (What is curl command syntax for device_token registration on Urban Airship?)

Nhận thời lượng video trên youtube từ URL với bash (Get youtube video duration from URL with bash)

đăng dữ liệu json với php curl cho đa phần / dữ liệu biểu mẫu để tải lên tệp nếu cakephp 2 (post json data with php curl for multipart/form-data for file upload vía cakephp 2)

ApacheMonitor 上的捲曲錯誤 (Curl error on ApacheMonitor)

從 ASP.net 使用 PHP 中的 POST 數據抓取數據 (Scraping data with POST data in PHP from ASP.net)

收聽推送消息? (Listen to a push message?)

在 PHP 中使用 cURL 的 RAW POST (RAW POST using cURL in PHP)

使用 php 和 curl 更新 mediawiki (using php and curl to update mediawiki)

在 Makefile 中鏈接 cURL (Linking cURL in Makefile)

將我的 .php 輸出發送到 curl 命令,該命令使用機器人在電報上向我發送消息 (Send my .php output to a curl command that messages me on telegram using a bot)

使用 Google My Business API 和 PHP 更新/修補本地帖子 (Updating/Patching a Local Post with Google My Business API and PHP)







留言討論