在Makefile中链接cURL [英] Linking cURL in Makefile

查看:1530
本文介绍了在Makefile中链接cURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过源代码安装cURL后在Ubuntu 11.04中链接cURL。





PROBLEM



首先我发现 -l <​​/ em>必须在 -L 之前,





获取cURL配置:



在我的termial:

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

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



没关系,这个目录下有cURL文件。






我的Makefile:



 #测试cURL 
#MAKEFILE

#C ++编译器(默认:g ++)
CXX = g ++
CFLAGS = -Wall -Werror

#库
INCLUDE = -Iusr / local / include
LDFLAGS = -Lusr / local / lib
LDLIBS = -lcurl

#详细信息
SOURCES = src / main.cpp
OUT = test

all:build

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






我的C ++源代码:



  #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;
}






/ h2>

 #make 
g ++ -o test -Iusr / local / include -Wall -Werror -Lusr / local / lib src /main.cpp
/tmp/ccli90i2.o:在函数`main'中:
main.cpp :( .text + 0xa):未定义引用`curl_easy_init'
main.cpp: (.text + 0x31):未定义的引用`curl_easy_setopt'
main.cpp :( .text + 0x3d):未定义引用`curl_easy_perform'
main.cpp :(。text + 0x4d):undefined引用`curl_easy_cleanup'
collect2:ld返回1退出状态
make:** [build] Erro 1

$ b b




我知道这是找不到库的错误,但对我来说一切都是正确的 $ b

解决方案

这应该做的工作。

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

注意添加 $(LDLIBS)



添加基本​​上发生的是,你抛出了GNU make的内置规则(见输出 make -np )并定义自己的规则。我建议你使用内置的,如果你想依靠相应的变量足以控制构建或者你仍然把它分为编译和链接步骤为了简洁。

简单说明:GNU make提供了一个规则,说明如何从文件中创建一个 .o .cpp (或 .c )文件。所以你的make文件可能会被重写为(约)

 #测试cURL 
#MAKEFILE

C ++编译器(默认:g ++)
CXX = g ++
CFLAGS = -Wall -Werror

#库
INCLUDE = -I / usr / local / include
LDFLAGS = -L / usr / local / lib
LDLIBS = -lcurl

#详细信息
SOURCES = src / main.cpp
OUT = test

.PHONY:all

all:build

$(OUT):$(patsubst%.cpp,%。o, $(SOURCES))

这将生成名为 test OUT 的内容),否则使用内置规则。使用 .o 文件的推论,必须有源文件,将寻找它们并编译它们。因此隐式地,这个构建将为每个 .cpp 文件和一个用于链接步骤的文件运行一次调用。


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

解决方案

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.

这篇关于在Makefile中链接cURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆