Problème de compilation d'un projet C++/Qt avec la librairie
Mourad2009B
Messages postés104Date d'inscriptionlundi 23 août 2010StatutMembreDernière intervention 2 juillet 2024
-
Modifié le 8 juin 2024 à 18:26
mamiemando
Messages postés33258Date d'inscriptionjeudi 12 mai 2005StatutModérateurDernière intervention30 août 2024
-
10 juin 2024 à 14:31
Problème de compilation d'un projet C++/Qt avec la librairie LIBCURLJ'essaye de faire excuter un progmme en c++/qt en utilisant la librairie CURL
voici mon fichier "main.cpp"
"
#include <QCoreApplication>
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <sstream>
#include <fstream>
#include <iostream>
/* The libcurl options want plain addresses, the viewable headers in the mail
* can get a full name as well.
*/
#define FROM_ADDR "m.mourad@gmail.com"
#define TO_ADDR "moha@gmail.com"
#define CC_ADDR "moha@outlook.com"
#define FROM_MAIL "Sender Person <" FROM_ADDR ">"
#define TO_MAIL "A Receiver <" TO_ADDR ">"
#define CC_MAIL "John CC Smith <" CC_ADDR ">"
struct UploadStatus {
int lines_read;
};
static const std::string payload_text[] = {
"To: moha@gmail.com\r\n",
"From: m.mourad@gmail.com\r\n",
"Subject: Test Email with Attachment\r\n",
"\r\n", /* empty line to divide headers from body */
"This is a test email with an attachment.\r\n",
nullptr // Change from NULL to nullptr
};
size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp) {
struct UploadStatus *upload_ctx = (struct UploadStatus *)userp;
const char *data;
if((size == 0) || (nmemb == 0) || ((size * nmemb) < 1)) {
return 0;
}
data = payload_text[upload_ctx->lines_read].c_str();
if(data) {
size_t len = strlen(data);
memcpy(ptr, data, len);
upload_ctx->lines_read++;
return len;
}
return 0;
}
std::string read_file(const std::string &file_path) {
std::ifstream file(file_path, std::ios::binary);
std::ostringstream oss;
oss << file.rdbuf();
return oss.str();
}
int main(int argc, char *argv[]) {
try
{
QCoreApplication a(argc, argv);
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *recipients = nullptr;
struct UploadStatus upload_ctx;
upload_ctx.lines_read = 0;
curl = curl_easy_init();
if(curl)
{
std::string attachment = read_file("messagetest.txt");
curl_easy_setopt(curl, CURLOPT_USERNAME, FROM_ADDR);
curl_easy_setopt(curl, CURLOPT_PASSWORD, "Mohamedmourad2024");
curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:465");
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_ADDR);
recipients = curl_slist_append(recipients, TO_ADDR);
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
struct curl_slist *headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: multipart/mixed; boundary=frontier");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
std::string body =
"--frontier\r\n"
"Content-Type: text/plain\r\n\r\n"
"This is a test email with an attachment.\r\n"
"--frontier\r\n"
"Content-Type: text/plain\r\n"
"Content-Disposition: attachment; filename=\"messagetest.txt\"\r\n\r\n" +
attachment + "\r\n"
"--frontier--";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
res = curl_easy_perform(curl);
if(res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
curl_slist_free_all(recipients);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
return a.exec();
}
catch (const std::logic_error& e)
{
std::cerr << "Logic error: " << e.what() << std::endl;
return 1;
}
catch (const std::exception& e)
{
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}
catch (...)
{
std::cerr << "Unknown error occurred." << std::endl;
return 1;
}
}
"
et voici mon fichier pro
"
QT = core
CONFIG += c++17 cmdline
SOURCES += \
main.cpp
# Indiquez le chemin vers les bibliothèques libcurl et les fichiers d'en-tête
LIBS += -L "C:/Data/Fichiers_applications/C++/LibrairiesExternes/curl-8.8.0_1-win64-mingw/lib" -lcurl
INCLUDEPATH += "C:/Data/Fichiers_applications/C++/LibrairiesExternes/curl-8.8.0_1-win64-mingw/include"
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
"
et quand je le lance ça provoque l'erreur suivante
"
16:06:05: Débogage de C:\Data\Fichiers_applications\C++\Projets_QtCreator\Tests\testCurlConsole\testCurlConsole\build\Desktop_Qt_6_2_4_MinGW_64_bit-Debug\debug\testCurlConsole.exe…
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
16:06:05: La débogage de « C:\Data\Fichiers_applications\C++\Projets_QtCreator\Tests\testCurlConsole\testCurlConsole\build\Desktop_Qt_6_2_4_MinGW_64_bit-Debug\debug\testCurlConsole.exe » s’est terminé avec le code de sortie 3.
"
pourquoi cette erreur et comment la résoudre?
Merci d'avance pour votre aide
A voir également:
Problème de compilation d'un projet C++/Qt avec la librairie