在 Makefile 中链接 cURL

发布于 2024-11-18 19:00:47 字数 1628 浏览 1 评论 0原文

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

问题的更正

首先我发现-l必须出现在-L之前,然后发现我没有在makefile中输入变量。

.

获取cURL配置:

在我的终端上:

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

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

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


我的 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)

我的 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;
}

和错误:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤君无依 2024-11-25 19:00:47

这应该可以完成工作。您之前并没有真正链接到 cURL。

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

请注意添加 $(LDLIBS)

哦,我应该补充一点,基本上发生的情况是,您抛弃了 GNU make 的内置规则(请参阅 make -np 的输出)并定义您自己的规则。如果您想依靠相应的变量足以控制构建,我建议您要么使用内置变量,要么为了简洁起见,您仍然将其分为编译和链接步骤。

简要说明:GNU make 附带了一条规则,规定如何从 .cpp(或 .c)文件创建 .o 文件。因此,您的 make 文件可能会被重写为(大约)。

# 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))

这应该生成名为 test 的二进制文件(OUT 的内容),并以其他方式使用内置的规则。 Make 从 .o 文件的使用推断必须存在源文件,将查找它们并编译它们。因此,此构建将隐式地为每个 .cpp 文件运行一次调用,并为链接步骤运行一次调用。

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.

栖竹 2024-11-25 19:00:47

您在下面的路径开头缺少斜杠

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

You are missing slashes at the start of the paths below

-I/usr/local/include
-L/usr/local/lib
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文