在 Makefile 中链接 cURL
通过源代码安装 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以完成工作。您之前并没有真正链接到 cURL。
请注意添加
$(LDLIBS)
。哦,我应该补充一点,基本上发生的情况是,您抛弃了 GNU make 的内置规则(请参阅
make -np
的输出)并定义您自己的规则。如果您想依靠相应的变量足以控制构建,我建议您要么使用内置变量,要么为了简洁起见,您仍然将其分为编译和链接步骤。简要说明:GNU make 附带了一条规则,规定如何从
.cpp
(或.c
)文件创建.o
文件。因此,您的 make 文件可能会被重写为(大约)。这应该生成名为
test
的二进制文件(OUT
的内容),并以其他方式使用内置的规则。 Make 从.o
文件的使用推断必须存在源文件,将查找它们并编译它们。因此,此构建将隐式地为每个 .cpp 文件运行一次调用,并为链接步骤运行一次调用。This should do the job. You didn't really link to cURL before.
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.)This should generate the binary with the name
test
(contents ofOUT
) 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.您在下面的路径开头缺少斜杠
You are missing slashes at the start of the paths below