c++ - QtCreator 和 Poco 无法在 Linux 上编译项目

标签 c++ smtp qt-creator poco-libraries

我正在尝试使用 Poco 库来发送电子邮件。 但是,当我尝试使用该库时,出现未定义的错误。 我认为这与我用于 Poco 的编译器有关,但我不确定我应该使用哪个编译器以便 QT 和 Poco 使用相同的东西编译。

Mass_Mailer.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Mass_Mailer
TEMPLATE = app
SOURCES += main.cpp \
    mainwindow.cpp
HEADERS  += mainwindow.h
FORMS    += mainwindow.ui
INCLUDEPATH += /usr/local/include/

主要.cpp

#include "mainwindow.h"
#include <QApplication>
#include <iostream>
#include <Poco/Net/MailMessage.h>
#include <Poco/Net/MailRecipient.h>
#include <Poco/Net/SMTPClientSession.h>
#include <Poco/Net/NetException.h>

using namespace std;
using namespace Poco::Net;
using namespace Poco;

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}

int sendmail()
{
string host = "mail.domain.com";
UInt16 port = 25;
string user = "xxx";
string password = "xxx";
string to = "xxx@domain.com";
string from = "xxx@domain.com";
string subject = "Your first e-mail message sent using Poco Libraries";
subject = MailMessage::encodeWord(subject.c_str(), "UTF-8");
string content = "Well done! You've successfully sent your first message using Poco SMTPClientSession";
MailMessage message;
message.setSender(from);
message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, to));
message.setSubject(subject);
message.setContentType("text/plain; charset=UTF-8");
message.setContent(content, MailMessage::ENCODING_8BIT);
try {
    SMTPClientSession session(host, port);
    session.open();
    try {
        session.login(SMTPClientSession::AUTH_LOGIN, user, password);
        session.sendMessage(message);
        cout << "Message successfully sent" << endl;
        session.close();
    } catch (SMTPException &e) {
        cerr << e.displayText() << endl;
        session.close();
        return 0;
    }
} catch (NetException &e) {
    cerr << e.displayText() << endl;
    return 0;
}
return 0;
}

错误

main.o: In function `sendmail()':
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:31: undefined reference to `Poco::Net::MailMessage::encodeWord(std::string const&, std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:33: undefined reference to `Poco::Net::MailMessage::MailMessage()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:34: undefined reference to `Poco::Net::MailMessage::setSender(std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:35: undefined reference to `Poco::Net::MailRecipient::MailRecipient(Poco::Net::MailRecipient::RecipientType, std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:35: undefined reference to `Poco::Net::MailMessage::addRecipient(Poco::Net::MailRecipient const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:35: undefined reference to `Poco::Net::MailRecipient::~MailRecipient()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:36: undefined reference to `Poco::Net::MailMessage::setSubject(std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:37: undefined reference to `Poco::Net::MailMessage::setContentType(std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:38: undefined reference to `Poco::Net::MailMessage::setContent(std::string const&, Poco::Net::MailMessage::ContentTransferEncoding)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:40: undefined reference to `Poco::Net::SMTPClientSession::SMTPClientSession(std::string const&, unsigned short)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:41: undefined reference to `Poco::Net::SMTPClientSession::open()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:43: undefined reference to `Poco::Net::SMTPClientSession::login(Poco::Net::SMTPClientSession::LoginMethod, std::string const&, std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:44: undefined reference to `Poco::Net::SMTPClientSession::sendMessage(Poco::Net::MailMessage const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:46: undefined reference to `Poco::Net::SMTPClientSession::close()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:46: undefined reference to `Poco::Net::SMTPClientSession::~SMTPClientSession()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:33: undefined reference to `Poco::Net::MailMessage::~MailMessage()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:35: undefined reference to `Poco::Net::MailRecipient::~MailRecipient()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:48: undefined reference to `Poco::Exception::displayText() const'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:49: undefined reference to `Poco::Net::SMTPClientSession::close()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:51: undefined reference to `Poco::Net::SMTPClientSession::~SMTPClientSession()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:53: undefined reference to `Poco::Exception::displayText() const'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:33:     undefined reference to `Poco::Net::MailMessage::~MailMessage()'
main.o:(.gcc_except_table+0x120): undefined reference to `typeinfo for Poco::Net::SMTPException'
main.o:(.gcc_except_table+0x124): undefined reference to `typeinfo for Poco::Net::NetException'
collect2: error: ld returned 1 exit status
make: *** [Mass_Mailer] Error 1
11:45:01: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project Mass_Mailer (kit: Desktop)
When executing step 'Make'11:45:01: Elapsed time: 00:02.

最佳答案

您需要将 Poco 库链接到您的项目。在项目文件中,添加如下内容:

LIBS += -lPocoNet -lPocoFoundation

关于c++ - QtCreator 和 Poco 无法在 Linux 上编译项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19010769/

相关文章:

c++ - 在验证字符串时将字符串 append 到字符串数组。 C++

wordpress - "No Such User Here"从 Wordpress 发送到 G Suite 电子邮件

c++ - 无法在 Linux 中使用 OpenCV 打开相机

qt - 在 QTCreator 中自定义部署

c++ - 为什么键入三个非整数会导致此函数无限递归?

c++ - C++ 是否支持 'finally' block ? (我一直听到的这个 'RAII' 是什么?)

c++ - 是否允许在 std::declval<T> 上使用 decltype (函数本身,而不是调用它的结果)?

smtp - 在 GitLab 11.4.4-ee 中更改通知邮件发件人名称

authentication - 无法登录 SMTP 服务器 Postfix/Imap Courier/Plesk

c++ - Qt C++ 使用连接语句查找用法