[英]Compiling and Linking curl easy interface with C++
上下文
尝试运行此curl_easy_init -在我的C ++程序中启动libcurl easy会话 。 curl库有很多选择的API,包括简单的帖子和许多其他的,但我仍然坚持正确配置我的编译和链接步骤。 我查看了curl网站教程,获取有关编译和链接此库到我的程序的指导,但仍然面临此错误。
卷曲位于这里
ᴾᴋᴹɴ Master Red ▰ ◓ ◓ ◓ ◓ ◓ ◓
> curl-config --cflags
-I/usr/local/Cellar/curl/7.64.1/include
卷曲图书馆包括
ᴾᴋᴹɴ Master Red ▰ ◓ ◓ ◓ ◓ ◓ ◓
> curl-config --libs
-L/usr/local/Cellar/curl/7.64.1/lib -lcurl -lldap -lz
即使添加到我的包含路径后,仍然会得到相同的错误。
# c++ curl support for the g++ compiler
# curl-config --cflags
CPLUS_INCLUDE_PATH="/usr/local/Cellar/curl/7.64.1/include:${CPLUS_INCLUDE_PATH}"
# curl-config --libs which include -lcurl -lldap -lz
CPLUS_INCLUDE_PATH="/usr/local/Cellar/curl/7.64.1/lib:${CPLUS_INCLUDE_PATH}"
export CPLUS_INCLUDE_PATH
错误
ᴾᴋᴹɴ Master Red ▰ ◓ ◓ ◓ ◓ ◓ ◓
> g++ -std=c++17 main.cpp && ./a.out
Undefined symbols for architecture x86_64:
"_curl_easy_cleanup", referenced from:
_main in main-9d2f7a.o
"_curl_easy_init", referenced from:
_main in main-9d2f7a.o
"_curl_easy_perform", referenced from:
_main in main-9d2f7a.o
"_curl_easy_setopt", referenced from:
_main in main-9d2f7a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
资源
#include <curl/curl.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
int main()
{
CURL *curl = curl_easy_init();
if(curl) {
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
}
使用makefile而不是在命令行上调用make
,你就可以了
# *****************************************************
# Variables to control Makefile operation
CXX = g++
# ****************************************************
# Targets needed to bring the executable up to date
main: main.cpp
$(CXX) main.cpp -I/usr/local/Cellar/curl/7.64.1/include -L/usr/local/Cellar/curl/7.64.1/lib -lcurl -lldap -lz && ./a.out
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.