使用C ++编译和链接curl easy接口 [英] Compiling and Linking curl easy interface with C++
问题描述
上下文
尝试运行此 curl_easy_init-在我的C ++程序中启动libcurl简易会话。 curl库提供了大量的API,包括简单帖子和许多其他的API。 ,但我坚持正确配置编译和链接步骤。我查看了卷曲网站教程,以获取有关编译和链接此页面的指南库到我的程序,但仍然会遇到此错误。
Attempting to run this curl_easy_init - Start a libcurl easy session in my C++ program. The curl library has a large selection of APIs including simple post and many others for, but I'm stuck on getting my compiling and linking step configured correctly. I looked to the curl websites tutorial for guidance on compiling and linking this library to my program, but still still facing this error.
curl位于此处
ᴾᴋᴹɴ Master Red ▰ ◓ ◓ ◓ ◓ ◓ ◓
> curl-config --cflags
-I/usr/local/Cellar/curl/7.64.1/include
curl库包括
ᴾᴋᴹɴ 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
,您就很好了
Use a makefile instead call make
on the command line and you're good to go
# *****************************************************
# 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
这篇关于使用C ++编译和链接curl easy接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!